From 7f349677372d4df4dbc9df6466fa94b7a1170612 Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 28 Feb 2007 00:25:43 +0000 Subject: [PATCH] This should really be automagic but for now we'll have a manual migration. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2063 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../jdbc/depot/EntityMigration.java | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) 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.