[recreation] don't try to set twice FKs to recreated AD tables
authorAsier Lostalé <asier.lostale@openbravo.com>
Tue, 14 Apr 2015 14:53:20 +0200
changeset 583 b12235a7d2a3
parent 582 34a13b000015
child 584 f86c7f770b00
[recreation] don't try to set twice FKs to recreated AD tables
src/org/apache/ddlutils/platform/PlatformImplBase.java
src/org/apache/ddlutils/platform/SqlBuilder.java
--- a/src/org/apache/ddlutils/platform/PlatformImplBase.java	Tue Apr 14 09:40:13 2015 +0200
+++ b/src/org/apache/ddlutils/platform/PlatformImplBase.java	Tue Apr 14 14:53:20 2015 +0200
@@ -2323,16 +2323,25 @@
     try {
       StringWriter buffer = new StringWriter();
       getSqlBuilder().setWriter(buffer);
+      ArrayList<String> recreatedTables = getSqlBuilder().recreatedTables;
       for (int i = 0; i < model.getTableCount(); i++) {
         Table table = model.getTable(i);
         if (dataset.getTable(table.getName()) != null) {
-          for (int idx = 0; idx < table.getForeignKeyCount(); idx++) {
-            getSqlBuilder().writeExternalForeignKeyDropStmt(table, table.getForeignKey(idx));
+          if (!recreatedTables.contains(table.getName())) {
+            for (int idx = 0; idx < table.getForeignKeyCount(); idx++) {
+              ForeignKey fk = table.getForeignKey(idx);
+              if (!recreatedTables.contains(fk.getForeignTableName())) {
+                // FKs to recreated tables are already dropped
+                getSqlBuilder().writeExternalForeignKeyDropStmt(table, table.getForeignKey(idx));
+              }
+            }
           }
         } else {
           for (int j = 0; j < table.getForeignKeyCount(); j++) {
             ForeignKey fk = table.getForeignKey(j);
-            if (dataset.getTable(fk.getForeignTableName()) != null) {
+            if (dataset.getTable(fk.getForeignTableName()) != null
+                && !recreatedTables.contains(fk.getForeignTableName())) {
+              // FKs to recreated tables are already dropped
               getSqlBuilder().writeExternalForeignKeyDropStmt(table, fk);
             }
           }
@@ -2367,14 +2376,18 @@
     try {
       StringWriter buffer = new StringWriter();
       getSqlBuilder().setWriter(buffer);
+      ArrayList<String> recreatedTables = getSqlBuilder().recreatedTables;
       for (int i = 0; i < model.getTableCount(); i++) {
         Table table = model.getTable(i);
         if (dataset.getTable(table.getName()) != null) {
-          getSqlBuilder().createExternalForeignKeys(model, table);
+          if (!recreatedTables.contains(table.getName())) {
+            getSqlBuilder().createExternalForeignKeys(model, table, true);
+          }
         } else {
           for (int j = 0; j < table.getForeignKeyCount(); j++) {
             ForeignKey fk = table.getForeignKey(j);
-            if (dataset.getTable(fk.getForeignTableName()) != null) {
+            if (dataset.getTable(fk.getForeignTableName()) != null
+                && !recreatedTables.contains(table.getName())) {
               getSqlBuilder().writeExternalForeignKeyCreateStmt(model, table, fk);
             }
           }
--- a/src/org/apache/ddlutils/platform/SqlBuilder.java	Tue Apr 14 09:40:13 2015 +0200
+++ b/src/org/apache/ddlutils/platform/SqlBuilder.java	Tue Apr 14 14:53:20 2015 +0200
@@ -2181,9 +2181,17 @@
    *          The table
    */
   public void createExternalForeignKeys(Database database, Table table) throws IOException {
+    createExternalForeignKeys(database, table, false);
+  }
+
+  public void createExternalForeignKeys(Database database, Table table, boolean skipRecreated)
+      throws IOException {
     if (!getPlatformInfo().isForeignKeysEmbedded()) {
       for (int idx = 0; idx < table.getForeignKeyCount(); idx++) {
-        writeExternalForeignKeyCreateStmt(database, table, table.getForeignKey(idx));
+        ForeignKey fk = table.getForeignKey(idx);
+        if (!skipRecreated || !recreatedTables.contains(fk.getForeignTableName())) {
+          writeExternalForeignKeyCreateStmt(database, table, fk);
+        }
       }
     }
   }