db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r517522 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/platform/SqlBuilder.java test/org/apache/ddlutils/io/TestAlteration.java
Date Tue, 13 Mar 2007 04:23:31 GMT
Author: tomdz
Date: Mon Mar 12 21:23:30 2007
New Revision: 517522

URL: http://svn.apache.org/viewvc?view=rev&rev=517522
Log:
Added a check as to whether data can be migrated when a table is altered

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?view=diff&rev=517522&r1=517521&r2=517522
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Mon Mar 12 21:23:30
2007
@@ -915,17 +915,49 @@
 
         if (!changes.isEmpty())
         {
-            Table tempTable       = getTemporaryTableFor(desiredModel, targetTable);
+            // we can only copy the data if no required columns without default value and
+            // non-autoincrement have been added
+            boolean canMigrateData = true;
+
+            for (Iterator it = changes.iterator(); canMigrateData && it.hasNext();)
+            {
+                TableChange change = (TableChange)it.next();
+
+                if (change instanceof AddColumnChange)
+                {
+                    AddColumnChange addColumnChange = (AddColumnChange)change;
+
+                    if (addColumnChange.getNewColumn().isRequired() &&
+                        !addColumnChange.getNewColumn().isAutoIncrement() &&
+                        (addColumnChange.getNewColumn().getDefaultValue() == null))
+                    {
+                        _log.warn("Data cannot be retained in table " + change.getChangedTable().getName()
+ 
+                                  " because of the addition of the required column " + addColumnChange.getNewColumn().getName());
+                        canMigrateData = false;
+                    }
+                }
+            }
+
             Table realTargetTable = getRealTargetTableFor(desiredModel, sourceTable, targetTable);
 
-            createTemporaryTable(desiredModel, tempTable, parameters);
-            writeCopyDataStatement(sourceTable, tempTable);
-            // Note that we don't drop the indices here because the DROP TABLE will take
care of that
-            // Likewise, foreign keys have already been dropped as necessary
-            dropTable(sourceTable);
-            createTable(desiredModel, realTargetTable, parameters);
-            writeCopyDataStatement(tempTable, targetTable);
-            dropTemporaryTable(desiredModel, tempTable);
+            if (canMigrateData)
+            {
+                Table tempTable = getTemporaryTableFor(desiredModel, targetTable);
+    
+                createTemporaryTable(desiredModel, tempTable, parameters);
+                writeCopyDataStatement(sourceTable, tempTable);
+                // Note that we don't drop the indices here because the DROP TABLE will take
care of that
+                // Likewise, foreign keys have already been dropped as necessary
+                dropTable(sourceTable);
+                createTable(desiredModel, realTargetTable, parameters);
+                writeCopyDataStatement(tempTable, targetTable);
+                dropTemporaryTable(desiredModel, tempTable);
+            }
+            else
+            {
+                dropTable(sourceTable);
+                createTable(desiredModel, realTargetTable, parameters);
+            }
         }
     }
 

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java
URL: http://svn.apache.org/viewvc/db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java?view=diff&rev=517522&r1=517521&r2=517522
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java (original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/io/TestAlteration.java Mon Mar 12 21:23:30
2007
@@ -2187,13 +2187,13 @@
 
         createDatabase(model1Xml);
 
-        // note that we cannot do this test with values in the table because
-        // the primary key will be re-created and thus won't have values in it
-        // (no renaming)
+        insertRow("roundtrip", new Object[] { new Integer(1), "test" });
 
         alterDatabase(model2Xml);
 
         assertEquals(getAdjustedModel(),
                      readModelFromDatabase("roundtriptest"));
+
+        assertTrue(getRows("roundtrip").isEmpty());
     }
 }



Mime
View raw message