db-ddlutils-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r398713 - in /db/ddlutils/trunk/src: java/org/apache/ddlutils/alteration/ModelComparator.java java/org/apache/ddlutils/platform/SqlBuilder.java test/org/apache/ddlutils/alteration/TestAlterationAlgorithm.java
Date Mon, 01 May 2006 22:26:39 GMT
Author: tomdz
Date: Mon May  1 15:26:30 2006
New Revision: 398713

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

Modified:
    db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java
    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/alteration/ModelComparator.java
URL: http://svn.apache.org/viewcvs/db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java?rev=398713&r1=398712&r2=398713&view=diff
==============================================================================
--- db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java (original)
+++ db/ddlutils/trunk/src/java/org/apache/ddlutils/alteration/ModelComparator.java Mon May
 1 15:26:30 2006
@@ -372,7 +372,7 @@
      */
     private Index findCorrespondingIndex(Table table, Index index)
     {
-        for (int indexIdx = 0; indexIdx < table.getForeignKeyCount(); indexIdx++)
+        for (int indexIdx = 0; indexIdx < table.getIndexCount(); indexIdx++)
         {
             Index curIndex = table.getIndex(indexIdx);
 

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=398713&r1=398712&r2=398713&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 Mon May  1 15:26:30
2006
@@ -590,6 +590,7 @@
                                                 Collection changes) throws IOException
     {
         ListOrderedMap changesPerTable = new ListOrderedMap();
+        ListOrderedMap unchangedFKs    = new ListOrderedMap();
         boolean        caseSensitive   = getPlatform().isDelimitedIdentifierModeOn();
 
         // we first sort the changes for the tables
@@ -611,9 +612,21 @@
             {
                 changesForTable = new ArrayList();
                 changesPerTable.put(name, changesForTable);
+                unchangedFKs.put(name, getUnchangedForeignKeys(currentModel, desiredModel,
name));
             }
             changesForTable.add(change);
         }
+        // we're dropping the unchanged foreign keys
+        for (Iterator tableFKIt = unchangedFKs.entrySet().iterator(); tableFKIt.hasNext();)
+        {
+            Map.Entry entry       = (Map.Entry)tableFKIt.next();
+            Table     targetTable = desiredModel.findTable((String)entry.getKey(), caseSensitive);
+
+            for (Iterator fkIt = ((List)entry.getValue()).iterator(); fkIt.hasNext();)
+            {
+                writeExternalForeignKeyDropStmt(targetTable, (ForeignKey)fkIt.next());
+            }
+        }
         for (Iterator tableChangeIt = changesPerTable.entrySet().iterator(); tableChangeIt.hasNext();)
         {
             Map.Entry entry = (Map.Entry)tableChangeIt.next();
@@ -623,9 +636,50 @@
                                          (String)entry.getKey(),
                                          (List)entry.getValue());
         }
+        // and finally we're re-creating the unchanged foreign keys
+        for (Iterator tableFKIt = unchangedFKs.entrySet().iterator(); tableFKIt.hasNext();)
+        {
+            Map.Entry entry       = (Map.Entry)tableFKIt.next();
+            Table     targetTable = desiredModel.findTable((String)entry.getKey(), caseSensitive);
+
+            for (Iterator fkIt = ((List)entry.getValue()).iterator(); fkIt.hasNext();)
+            {
+                writeExternalForeignKeyCreateStmt(desiredModel, targetTable, (ForeignKey)fkIt.next());
+            }
+        }
     }
 
     /**
+     * Determines the unchanged foreign keys of the indicated table.
+     * 
+     * @param currentModel The current model
+     * @param desiredModel The desired model
+     * @param tableName    The name of the table
+     * @return The list of unchanged foreign keys
+     */
+    private List getUnchangedForeignKeys(Database currentModel,
+                                         Database desiredModel,
+                                         String   tableName)
+    {
+        ArrayList unchangedFKs  = new ArrayList();
+        boolean   caseSensitive = getPlatform().isDelimitedIdentifierModeOn();
+        Table     sourceTable   = currentModel.findTable(tableName, caseSensitive);
+        Table     targetTable   = desiredModel.findTable(tableName, caseSensitive);
+
+        for (int idx = 0; idx < targetTable.getForeignKeyCount(); idx++)
+        {
+            ForeignKey targetFK = targetTable.getForeignKey(idx);
+            ForeignKey sourceFK = sourceTable.findForeignKey(targetFK, caseSensitive);
+
+            if (sourceFK != null)
+            {
+                unchangedFKs.add(targetFK);
+            }
+        }
+        return unchangedFKs;
+    }
+    
+    /**
      * Processes the changes to the structure of a single table.
      * 
      * @param currentModel The current database schema
@@ -653,13 +707,18 @@
         }
 
         // TODO: where to get the parameters from ?
-        Table sourceTable = currentModel.findTable(tableName, getPlatform().isDelimitedIdentifierModeOn());
-        Table targetTable = desiredModel.findTable(tableName, getPlatform().isDelimitedIdentifierModeOn());
-        Table tempTable   = createTemporaryTable(desiredModel, targetTable, null);
+        Map   parameters      = null;
+        Table sourceTable     = currentModel.findTable(tableName, getPlatform().isDelimitedIdentifierModeOn());
+        Table targetTable     = desiredModel.findTable(tableName, getPlatform().isDelimitedIdentifierModeOn());
+        Table tempTable       = createTemporaryTable(desiredModel, targetTable);
+        Table realTargetTable = createRealTargetTable(desiredModel, sourceTable, targetTable);
 
+        createTable(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, targetTable);
+        createTable(desiredModel, realTargetTable, parameters);
         writeCopyDataStatement(tempTable, targetTable);
         dropTable(tempTable);
     }
@@ -673,22 +732,21 @@
      * 
      * @param targetModel The target database
      * @param targetTable The target table
-     * @param parameters  Table creation parameters
      * @return The temporary table
      */
-    protected Table createTemporaryTable(Database targetModel, Table targetTable, Map parameters)
throws IOException
+    protected Table createTemporaryTable(Database targetModel, Table targetTable) throws
IOException
     {
-        Table tempTable = new Table();
+        Table table = new Table();
 
-        tempTable.setCatalog(targetTable.getCatalog());
-        tempTable.setSchema(targetTable.getSchema());
-        tempTable.setName(targetTable.getName() + "_");
-        tempTable.setType(targetTable.getType());
+        table.setCatalog(targetTable.getCatalog());
+        table.setSchema(targetTable.getSchema());
+        table.setName(targetTable.getName() + "_");
+        table.setType(targetTable.getType());
         for (int idx = 0; idx < targetTable.getColumnCount(); idx++)
         {
             try
             {
-                tempTable.addColumn((Column)targetTable.getColumn(idx).clone());
+                table.addColumn((Column)targetTable.getColumn(idx).clone());
             }
             catch (CloneNotSupportedException ex)
             {
@@ -696,9 +754,56 @@
             }
         }
 
-        writeTableCreationStmt(targetModel, tempTable, parameters);
-        writeTableCreationStmtEnding(tempTable, parameters);
-        return tempTable;
+        return table;
+    }
+
+    /**
+     * Creates the target table that differs from the given target table only in the
+     * indices. More specifically, only those indices are used that have not changed.
+     * 
+     * @param targetModel The target database
+     * @param sourceTable The source table
+     * @param targetTable The target table
+     * @return The table
+     */
+    protected Table createRealTargetTable(Database targetModel, Table sourceTable, Table
targetTable) throws IOException
+    {
+        Table table = new Table();
+
+        table.setCatalog(targetTable.getCatalog());
+        table.setSchema(targetTable.getSchema());
+        table.setName(targetTable.getName());
+        table.setType(targetTable.getType());
+        for (int idx = 0; idx < targetTable.getColumnCount(); idx++)
+        {
+            try
+            {
+                table.addColumn((Column)targetTable.getColumn(idx).clone());
+            }
+            catch (CloneNotSupportedException ex)
+            {
+                throw new DdlUtilsException(ex);
+            }
+        }
+
+        boolean caseSensitive = getPlatform().isDelimitedIdentifierModeOn();
+
+        for (int idx = 0; idx < targetTable.getIndexCount(); idx++)
+        {
+            Index targetIndex = targetTable.getIndex(idx);
+            Index sourceIndex = sourceTable.findIndex(targetIndex.getName(), caseSensitive);
+
+            if (sourceIndex != null)
+            {
+                if ((caseSensitive  && sourceIndex.equals(targetIndex)) ||
+                    (!caseSensitive && sourceIndex.equalsIgnoreCase(targetIndex)))
+                {
+                    table.addIndex(targetIndex);
+                }
+            }
+        }
+
+        return table;
     }
 
     /**
@@ -2091,7 +2196,6 @@
         return index.getName();
     }
 
-    
     /**
      * Writes the indexes of the given table.
      * 

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=398713&r1=398712&r2=398713&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
Mon May  1 15:26:30 2006
@@ -1199,7 +1199,6 @@
             "</database>";
 
         assertEqualsIgnoringWhitespaces(
-            "DROP INDEX \"TESTINDEX\" ON \"TableA\";\n"+
             "CREATE TABLE \"TableA_\"\n"+
             "(\n"+
             "    \"ColPK\" INTEGER NOT NULL,\n"+
@@ -1216,9 +1215,9 @@
             "    \"Col2\" VARCHAR(64),\n"+
             "    PRIMARY KEY (\"ColPK\")\n"+
             ");\n"+
+            "CREATE INDEX \"TESTINDEX\" ON \"TableA\" (\"Col1\");\n"+
             "INSERT INTO \"TableA\" (\"ColPK\",\"Col1\",\"Col2\") SELECT \"ColPK\",\"Col1\",\"Col2\"
FROM \"TableA_\";\n"+
-            "DROP TABLE \"TableA_\";\n"+
-            "CREATE UNIQUE INDEX \"TESTINDEX\" ON \"TableA\" (\"Col1\");\n",
+            "DROP TABLE \"TableA_\";\n",
             getAlterDatabaseSQL(MODEL1, MODEL2));
     }
 
@@ -1262,8 +1261,8 @@
             "CREATE TABLE \"TABLEB_\"\n"+
             "(\n"+
             "    \"COLPK\" INTEGER NOT NULL,\n"+
-            "    \"COLFK\" DOUBLE,\n"+
-            "    \"COL\" VARCHAR(64),\n"+
+            "    \"COLFK\" INTEGER,\n"+
+            "    \"COL\" DOUBLE,\n"+
             "    PRIMARY KEY (\"COLPK\")\n"+
             ");\n"+
             "INSERT INTO \"TABLEB_\" (\"COLPK\",\"COLFK\") SELECT \"COLPK\",\"COLFK\" FROM
\"TABLEB\";\n"+
@@ -1271,8 +1270,8 @@
             "CREATE TABLE \"TABLEB\"\n"+
             "(\n"+
             "    \"COLPK\" INTEGER NOT NULL,\n"+
-            "    \"COLFK\" DOUBLE,\n"+
-            "    \"COL\" VARCHAR(64),\n"+
+            "    \"COLFK\" INTEGER,\n"+
+            "    \"COL\" DOUBLE,\n"+
             "    PRIMARY KEY (\"COLPK\")\n"+
             ");\n"+
             "INSERT INTO \"TABLEB\" (\"COLPK\",\"COLFK\",\"COL\") SELECT \"COLPK\",\"COLFK\",\"COL\"
FROM \"TABLEB_\";\n"+
@@ -1785,7 +1784,6 @@
             "</database>";
 
         assertEqualsIgnoringWhitespaces(
-            "DROP INDEX \"TestIndex\" ON \"TableA\";\n"+
             "CREATE TABLE \"TableA_\"\n"+
             "(\n"+
             "    \"ColPK\" INTEGER NOT NULL,\n"+
@@ -1800,9 +1798,9 @@
             "    \"Col\" DOUBLE NOT NULL,\n"+
             "    PRIMARY KEY (\"ColPK\")\n"+
             ");\n"+
+            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n"+
             "INSERT INTO \"TableA\" (\"ColPK\",\"Col\") SELECT \"ColPK\",\"Col\" FROM \"TableA_\";\n"+
-            "DROP TABLE \"TableA_\";\n"+
-            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n",
+            "DROP TABLE \"TableA_\";\n",
             getAlterDatabaseSQL(MODEL1, MODEL2));
     }
 
@@ -1989,7 +1987,6 @@
             "</database>";
 
         assertEqualsIgnoringWhitespaces(
-            "DROP INDEX \"TestIndex\" ON \"TableA\";\n"+
             "CREATE TABLE \"TableA_\"\n"+
             "(\n"+
             "    \"ColPK\" INTEGER NOT NULL,\n"+
@@ -2004,9 +2001,9 @@
             "    \"Col\" DECIMAL(15,2) NOT NULL,\n"+
             "    PRIMARY KEY (\"ColPK\")\n"+
             ");\n"+
+            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n"+
             "INSERT INTO \"TableA\" (\"ColPK\",\"Col\") SELECT \"ColPK\",\"Col\" FROM \"TableA_\";\n"+
-            "DROP TABLE \"TableA_\";\n"+
-            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n",
+            "DROP TABLE \"TableA_\";\n",
             getAlterDatabaseSQL(MODEL1, MODEL2));
     }
 
@@ -2193,7 +2190,6 @@
             "</database>";
 
         assertEqualsIgnoringWhitespaces(
-            "DROP INDEX \"TestIndex\" ON \"TableA\";\n"+
             "CREATE TABLE \"TableA_\"\n"+
             "(\n"+
             "    \"ColPK\" INTEGER NOT NULL,\n"+
@@ -2208,9 +2204,9 @@
             "    \"Col\" DATE DEFAULT '2001-02-03' NOT NULL,\n"+
             "    PRIMARY KEY (\"ColPK\")\n"+
             ");\n"+
+            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n"+
             "INSERT INTO \"TableA\" (\"ColPK\",\"Col\") SELECT \"ColPK\",\"Col\" FROM \"TableA_\";\n"+
-            "DROP TABLE \"TableA_\";\n"+
-            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n",
+            "DROP TABLE \"TableA_\";\n",
             getAlterDatabaseSQL(MODEL1, MODEL2));
     }
 
@@ -2397,7 +2393,6 @@
             "</database>";
 
         assertEqualsIgnoringWhitespaces(
-            "DROP INDEX \"TestIndex\" ON \"TableA\";\n"+
             "CREATE TABLE \"TableA_\"\n"+
             "(\n"+
             "    \"ColPK\" INTEGER NOT NULL,\n"+
@@ -2412,9 +2407,9 @@
             "    \"Col\" INTEGER NOT NULL IDENTITY,\n"+
             "    PRIMARY KEY (\"ColPK\")\n"+
             ");\n"+
+            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n"+
             "INSERT INTO \"TableA\" (\"ColPK\",\"Col\") SELECT \"ColPK\",\"Col\" FROM \"TableA_\";\n"+
-            "DROP TABLE \"TableA_\";\n"+
-            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n",
+            "DROP TABLE \"TableA_\";\n",
             getAlterDatabaseSQL(MODEL1, MODEL2));
     }
 
@@ -2422,7 +2417,7 @@
      * Tests the change of the auto-increment attribute of the columns of a primary key
      * and the referencing foreign key.
      */
-    public void testChangePrimaryAndsForeignKeyColumnsAutoIncrement() throws IOException
+    public void testChangePrimaryAndForeignKeyColumnsAutoIncrement() throws IOException
     {
         final String MODEL1 = 
             "<?xml version='1.0' encoding='ISO-8859-1'?>\n" +
@@ -2602,7 +2597,6 @@
             "</database>";
 
         assertEqualsIgnoringWhitespaces(
-            "DROP INDEX \"TestIndex\" ON \"TableA\";\n"+
             "CREATE TABLE \"TableA_\"\n"+
             "(\n"+
             "    \"ColPK\" INTEGER NOT NULL,\n"+
@@ -2617,9 +2611,9 @@
             "    \"Col\" INTEGER NOT NULL,\n"+
             "    PRIMARY KEY (\"ColPK\")\n"+
             ");\n"+
+            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n"+
             "INSERT INTO \"TableA\" (\"ColPK\",\"Col\") SELECT \"ColPK\",\"Col\" FROM \"TableA_\";\n"+
-            "DROP TABLE \"TableA_\";\n"+
-            "CREATE INDEX \"TestIndex\" ON \"TableA\" (\"Col\");\n",
+            "DROP TABLE \"TableA_\";\n",
             getAlterDatabaseSQL(MODEL1, MODEL2));
     }
 
@@ -2627,7 +2621,7 @@
      * Tests the change of the required attribute of the columns of a primary key
      * and the referencing foreign key.
      */
-    public void testChangePrimaryAndsForeignKeyColumnsRequired() throws IOException
+    public void testChangePrimaryAndForeignKeyColumnsRequired() throws IOException
     {
         final String MODEL1 = 
             "<?xml version='1.0' encoding='ISO-8859-1'?>\n" +



Mime
View raw message