From bfc55f66eb1e898aa282162102fad481a06f9bfc Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 9 Feb 2007 20:41:30 +0000 Subject: [PATCH] Make our Rename migration more lenient about having already been performed. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2046 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../jdbc/depot/EntityMigration.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/EntityMigration.java b/src/java/com/samskivert/jdbc/depot/EntityMigration.java index 9d21c1ee..6f394261 100644 --- a/src/java/com/samskivert/jdbc/depot/EntityMigration.java +++ b/src/java/com/samskivert/jdbc/depot/EntityMigration.java @@ -3,7 +3,7 @@ // // samskivert library - useful routines for java programs // Copyright (C) 2006 Michael Bayne -// +// // This library is free software; you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as published // by the Free Software Foundation; either version 2.1 of the License, or @@ -25,6 +25,8 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; +import com.samskivert.jdbc.JDBCUtil; + import static com.samskivert.jdbc.depot.Log.log; /** @@ -75,9 +77,27 @@ public abstract class EntityMigration extends Modifier try { log.info("Changing '" + _oldColumnName + "' to '" + _newColumnDef + "' in " + _tableName); - return stmt.executeUpdate( - "alter table " + _tableName + " change column " + _oldColumnName + " " + - _newColumnDef); + + if (!JDBCUtil.tableContainsColumn(conn, _tableName, _oldColumnName)) { + if (JDBCUtil.tableContainsColumn(conn, _tableName, _newColumnName)) { + // we'll accept this inconsistency + log.warning("Column rename appears already performed."); + return 0; + } + // but this is not OK + throw new IllegalArgumentException( + _tableName + " does not contain '" + _oldColumnName + "'"); + } + + // nor is this + if (JDBCUtil.tableContainsColumn(conn, _tableName, _newColumnName)) { + throw new IllegalArgumentException( + _tableName + " already contains '" + _newColumnName + "'"); + } + + return stmt.executeUpdate("alter table " + _tableName + " change column " + + _oldColumnName + " " + _newColumnDef); + } finally { stmt.close(); }