db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r399036 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/platform/SqlBuilder.java test/org/apache/ddlutils/alteration/TestAlterationAlgorithm.java
Date Tue, 02 May 2006 20:19:05 GMT
Author: tomdz
Date: Tue May  2 13:19:04 2006
New Revision: 399036

URL: http://svn.apache.org/viewcvs?rev=399036&view=rev
Log:
Finished initial implementation of the new alteration algorithm

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
    db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestAlterationAlgorithm.java

Modified: db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java?rev=399036&r1=399035&r2=399036&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/platform/SqlBuilder.java Tue May  2 13:19:04
2006
@@ -27,6 +27,7 @@
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.commons.collections.Closure;
 import org.apache.commons.collections.CollectionUtils;
@@ -616,6 +617,9 @@
             }
             changesForTable.add(change);
         }
+        // we also need to drop the foreign keys of the unchanged tables referencing the
changed tables
+        addRelevantFKsFromUnchangedTables(currentModel, desiredModel, changesPerTable.keySet(),
unchangedFKs);
+
         // we're dropping the unchanged foreign keys
         for (Iterator tableFKIt = unchangedFKs.entrySet().iterator(); tableFKIt.hasNext();)
         {
@@ -677,6 +681,60 @@
             }
         }
         return unchangedFKs;
+    }
+
+    /**
+     * Adds the foreign keys of the unchanged tables that reference changed tables
+     * to the given map.
+     * 
+     * @param currentModel              The current model
+     * @param desiredModel              The desired model
+     * @param namesOfKnownChangedTables The known names of changed tables
+     * @param fksPerTable               The map table name -> foreign keys to which
+     *                                  found foreign keys will be added to
+     */
+    private void addRelevantFKsFromUnchangedTables(Database currentModel,
+                                                   Database desiredModel,
+                                                   Set      namesOfKnownChangedTables,
+                                                   Map      fksPerTable)
+    {
+        boolean caseSensitive = getPlatform().isDelimitedIdentifierModeOn();
+
+        for (int tableIdx = 0; tableIdx < desiredModel.getTableCount(); tableIdx++)
+        {
+            Table  targetTable = desiredModel.getTable(tableIdx);
+            String name        = targetTable.getName();
+            Table  sourceTable = currentModel.findTable(name, caseSensitive);
+            List   relevantFks = null;
+
+            if (!caseSensitive)
+            {
+                name = name.toUpperCase();
+            }
+            if ((sourceTable != null) && !namesOfKnownChangedTables.contains(name))
+            {
+                for (int fkIdx = 0; fkIdx < targetTable.getForeignKeyCount(); fkIdx++)
+                {
+                    ForeignKey targetFk = targetTable.getForeignKey(fkIdx);
+                    ForeignKey sourceFk = sourceTable.findForeignKey(targetFk, caseSensitive);
+                    String     refName  = targetFk.getForeignTable().getName();
+
+                    if (!caseSensitive)
+                    {
+                        refName = refName.toUpperCase();
+                    }
+                    if ((sourceFk != null) && namesOfKnownChangedTables.contains(refName))
+                    {
+                        if (relevantFks == null)
+                        {
+                            relevantFks = new ArrayList();
+                            fksPerTable.put(name, relevantFks);
+                        }
+                        relevantFks.add(targetFk);
+                    }
+                }
+            }
+        }
     }
     
     /**

Modified: db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestAlterationAlgorithm.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestAlterationAlgorithm.java?rev=399036&r1=399035&r2=399036&view=diff
==============================================================================
--- db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestAlterationAlgorithm.java
(original)
+++ db/ddlutils/trunk/src/test/org/apache/ddlutils/alteration/TestAlterationAlgorithm.java
Tue May  2 13:19:04 2006
@@ -1320,6 +1320,7 @@
             "CREATE TABLE \"TableA_\"\n"+
             "(\n"+
             "    \"ColPK\" INTEGER NOT NULL,\n"+
+            "    \"Col\" DOUBLE,\n"+
             "    PRIMARY KEY (\"ColPK\")\n"+
             ");\n"+
             "INSERT INTO \"TableA_\" (\"ColPK\") SELECT \"ColPK\" FROM \"TableA\";\n"+
@@ -2464,22 +2465,6 @@
             ");\n"+
             "INSERT INTO \"TableA\" (\"ColPK\") SELECT \"ColPK\" FROM \"TableA_\";\n"+
             "DROP TABLE \"TableA_\";\n"+
-            "CREATE TABLE \"TABLEB_\"\n"+
-            "(\n"+
-            "    \"COLPK\" INTEGER NOT NULL,\n"+
-            "    \"COLFK\" INTEGER,\n"+
-            "    PRIMARY KEY (\"COLPK\")\n"+
-            ");\n"+
-            "INSERT INTO \"TABLEB_\" (\"COLPK\",\"COLFK\") SELECT \"COLPK\",\"COLFK\" FROM
\"TABLEB\";\n"+
-            "DROP TABLE \"TABLEB\";\n"+
-            "CREATE TABLE \"TABLEB\"\n"+
-            "(\n"+
-            "    \"COLPK\" INTEGER NOT NULL,\n"+
-            "    \"COLFK\" INTEGER,\n"+
-            "    PRIMARY KEY (\"COLPK\")\n"+
-            ");\n"+
-            "INSERT INTO \"TABLEB\" (\"COLPK\",\"COLFK\") SELECT \"COLPK\",\"COLFK\" FROM
\"TABLEB_\";\n"+
-            "DROP TABLE \"TABLEB_\";\n"+
             "ALTER TABLE \"TABLEB\" ADD CONSTRAINT \"TESTFK\" FOREIGN KEY (\"COLFK\") REFERENCES
\"TableA\" (\"ColPK\");\n",
             getAlterDatabaseSQL(MODEL1, MODEL2));
     }



Mime
View raw message