Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Ant task that prefixes new SQL migration files with a timestamp precise to milliseconds. The following usage will add a prefix to any SQL file in a hardcoded directory that does not begin with an number and double leading underscore: $ ant prefix-new-migrations
<project name="migrations">
<target name="prefix-new-migrations">
<foreach target="rename-file" param="the-file">
<path>
<!-- The hardcoded directory containing the migrations -->
<fileset dir="./src/db/migrations" casesensitive="no" includes="*.sql">
<!-- Exclude any migration files which have already been prefixed -->
<not>
<filename regex="\d+__.*" casesensitive="true"/>
</not>
</fileset>
</path>
</foreach>
</target>
<target name="rename-file">
<tstamp>
<format property="time.stamp" pattern="yyyyMMddHHmmssSSS"/>
</tstamp>
<dirname property="dir.name" file="${the-file}"/>
<basename property="file.name" file="${the-file}"/>
<!-- Renames my_migration.sql to 20130704144750766__my_migration.sql -->
<move file="${the-file}" tofile="${dir.name}${file.separator}${time.stamp}__${file.name}"/>
<!-- Sleep to prevent prefix conflicts when renaming multiple files -->
<sleep seconds="1"/>
</target>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment