From c4ddf1853cd767e14715721d9839ec7516fb4568 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 10 Sep 2008 02:00:19 +0000 Subject: [PATCH] Created an explicit EntityMigration.Add for when you want to add a column to a database and need to provide a defaultValue at the time that you add it, but don't want to have to define a permanent default value on the field. --- .../jdbc/depot/EntityMigration.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/java/com/samskivert/jdbc/depot/EntityMigration.java b/src/java/com/samskivert/jdbc/depot/EntityMigration.java index 41fc15c..b802f02 100644 --- a/src/java/com/samskivert/jdbc/depot/EntityMigration.java +++ b/src/java/com/samskivert/jdbc/depot/EntityMigration.java @@ -150,6 +150,52 @@ public abstract class EntityMigration extends Modifier protected ColumnDefinition _newColumnDef; } + /** + * A convenient migration for adding a new column that requires a default value to be specified + * during the addition. Normally Depot will automatically handle column addition, but if you + * have a column that normally does not have a default value but needs one when it is added to + * a table with existing rows, you can use this migration. + * + * @see Column#defaultValue + */ + public static class Add extends EntityMigration + { + public Add (int targetVersion, String fieldName, String defaultValue) { + super(targetVersion); + _fieldName = fieldName; + _defaultValue = defaultValue; + } + + @Override public int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { + // override the default value in the column definition with the one provided + ColumnDefinition defColumnDef = new ColumnDefinition( + _newColumnDef.getType(), _newColumnDef.isNullable(), + _newColumnDef.isUnique(), _defaultValue); + // first add the column with the overridden default value + if (liaison.addColumn(conn, _tableName, _fieldName, defColumnDef, true)) { + // then change the column to the permanent default value + liaison.changeColumn(conn, _tableName, _fieldName, _newColumnDef.getType(), + null, null, _newColumnDef.getDefaultValue()); + return 1; + } + return 0; + } + + @Override public boolean runBeforeDefault () { + return true; + } + + @Override + protected void init (String tableName, Map> marshallers) { + super.init(tableName, marshallers); + _columnName = marshallers.get(_fieldName).getColumnName(); + _newColumnDef = marshallers.get(_fieldName).getColumnDefinition(); + } + + protected String _fieldName, _columnName, _defaultValue; + protected ColumnDefinition _newColumnDef; + } + /** * If this method returns true, this migration will be run before the default * migrations, if false it will be run after.