Wednesday, March 19, 2014

Applying all migration scripts sql or not from tagged releases : an svn cat tag versions example

Let us say your project goes from version 0.2.2 to 2.67. You have correctly tagged your project version after version and now you want to collect all migration scripts from one version to another. This is really achievable in a bash one liner. Below is one example with subversion: The one liner first gets a list from a classical tags directory. It then sorts based on the "period" as separator all tokens using alphabetical order for the first part (name of the package which we assume major version won't go over 9) and numerical order for the last two parts. It filters the results (hopefully less than a million versions ;-) between versions (If you need to exclude the from and to versions just use head/tail with "-n +2"). Finally for each version we concatenate the content of the migration to be performed.

Here is how the scripts invocation looks for a sql migration script:
$ ./svn-cat-tag-versions.sh \
http://subversion.sample.com/path/to/sample-core/tags \
src/main/resources/db/migration.sql sample-core-2.56.1 \
sample-core-2.58
/*****sample-core-2.56.1/*****/
ALTER TABLE contact DROP COLUMN email;
ALTER TABLE contact DROP COLUMN email_notification;
ALTER TABLE contact ADD COLUMN email_notification BIT(1) NOT NULL AFTER VERSION;

/*****sample-core-2.57/*****/
ALTER TABLE contact DROP COLUMN notification;

/*****sample-core-2.58/*****/
Note there was nothing to do as part of 2.58 release and yet the script presents the record making sure it is explicit that there is nothing to do for it.

No comments:

Followers