OpenACS - Package drop script

Maintaining the create and drop scripts for your package might be a pain, but I find it is always more useful to keep them up to date as you make changes to the data model, rather than leaving it until the end.

Assuming you have a working create script that creates tables, this one-liner will generate the drop statements for you.

grep -iE "create[ \\t]+table" yourpackage-create.sql |tac|awk '{printf "drop table %s;\n",$3}'

What's going on there? Well we grep the file for the relevant lines, reverse the order, then output just the table name with the right SQL around it.

In more detail:

grep -iE "create[ \\t]+table" yourpackage-create.sql |tac|awk '{printf "drop table %s;\n",$3}'

-iE means case-insensitive matching (the "i") and using an extended regular expression (the "E").

grep -iE "create[ \\t]+table" yourpackage-create.sql |tac|awk '{printf "drop table %s;\n",$3}'

Within the double quotes is our regular expression. This matches the word "create", followed by white space (one or more of a space or tab), followed by the word table.

grep -iE create[ \\t]+table" yourpackage-create.sql |tac|awk '{printf "drop table %s;\n",$3}'

Next is the file we're processing.

grep -iE create[ \\t]+table" yourpackage-create.sql |tac|awk '{printf "drop table %s;\n",$3}'

Then we pipe the output from grep into tac, which is the same as cat but backwards. Obviously we want to drop the tables in the opposite order they were created to avoid problems with referential integrity.

grep -iE create[ \\t]+table" yourpackage-create.sql |tac|awk '{printf "drop table %s;\n",$3}'

Then we pipe the output from tac to awk, which outputs "drop table blah;" where blah is the third column of the output from tac, i.e. the table name.

I've found this useful during the early stages of package development. You could use similar commands to do the same for sequences and packages, but these are less bothersome since you can normally drop them in any order. Unfortunately if your package creates objects, or makes calls to other APIs during installation, you have to write the drop for these manually.

If you want to learn more about OpenACS packages, there is a lot of reading here.