diff --git a/src/java/com/samskivert/jdbc/depot/EntityMigration.java b/src/java/com/samskivert/jdbc/depot/EntityMigration.java index 847ea777..7b5270be 100644 --- a/src/java/com/samskivert/jdbc/depot/EntityMigration.java +++ b/src/java/com/samskivert/jdbc/depot/EntityMigration.java @@ -108,8 +108,7 @@ public abstract class EntityMigration extends Modifier } } - protected void init (String tableName, HashMap marshallers) - { + protected void init (String tableName, HashMap marshallers) { super.init(tableName, marshallers); _newColumnDef = marshallers.get(_newColumnName).getColumnDefinition(); } @@ -117,6 +116,35 @@ public abstract class EntityMigration extends Modifier protected String _oldColumnName, _newColumnName, _newColumnDef; } + /** + * A convenient migration for changing the type of an existing column. + */ + public static class Retype extends EntityMigration + { + public Retype (int targetVersion, String columnName) { + super(targetVersion); + _columnName = columnName; + } + + public int invoke (Connection conn) throws SQLException { + Statement stmt = conn.createStatement(); + try { + log.info("Updating type of '" + _columnName + "' in " + _tableName); + return stmt.executeUpdate("alter table " + _tableName + " change column " + + _columnName + " " + _newColumnDef); + } finally { + stmt.close(); + } + } + + protected void init (String tableName, HashMap marshallers) { + super.init(tableName, marshallers); + _newColumnDef = marshallers.get(_columnName).getColumnDefinition(); + } + + protected String _columnName, _newColumnDef; + } + /** * If this method returns true, this migration will be run before the default * migrations, if false it will be run after.