Skip to content

Instantly share code, notes, and snippets.

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">
<!-- The hardcoded directory containing the migrations -->
<fileset dir="./src/db/migrations" casesensitive="no" includes="*.sql">
<!-- Exclude any migration files which have already been prefixed -->
<filename regex="\d+__.*" casesensitive="true"/>
<target name="rename-file">
<format property="time.stamp" pattern="yyyyMMddHHmmssSSS"/>
<dirname property="" file="${the-file}"/>
<basename property="" file="${the-file}"/>
<!-- Renames my_migration.sql to 20130704144750766__my_migration.sql -->
<move file="${the-file}" tofile="${}${file.separator}${time.stamp}__${}"/>
<!-- Sleep to prevent prefix conflicts when renaming multiple files -->
<sleep seconds="1"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.