[recreation] defer all not nulls on recreation
authorAsier Lostalé <asier.lostale@openbravo.com>
Tue, 14 Apr 2015 17:14:30 +0200
changeset 589 0212ec50316b
parent 588 3513b3498ae7
child 590 5f57ef7275b7
[recreation] defer all not nulls on recreation
src/org/apache/ddlutils/platform/SqlBuilder.java
src/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java
--- a/src/org/apache/ddlutils/platform/SqlBuilder.java	Tue Apr 14 16:27:34 2015 +0200
+++ b/src/org/apache/ddlutils/platform/SqlBuilder.java	Tue Apr 14 17:14:30 2015 +0200
@@ -1633,13 +1633,15 @@
     // Likewise, foreign keys have already been dropped as necessary
     dropTable(sourceTable);
 
+    @SuppressWarnings("unchecked")
+    List<String> originallyRecreatedTables = (List<String>) recreatedTables.clone();
     if (recreatedTables.contains(sourceTable.getName())) {
       recreatedTablesTwice.add(sourceTable.getName());
     }
     recreatedTables.add(sourceTable.getName());
 
     createTable(desiredModel, realTargetTable, parameters);
-    disableAllNOTNULLColumns(realTargetTable);
+    disableAllNOTNULLColumns(realTargetTable, originallyRecreatedTables);
     writeCopyDataStatement(tempTable, targetTable);
     dropTemporaryTable(desiredModel, tempTable);
 
@@ -1709,10 +1711,14 @@
   }
 
   protected void disableAllNOTNULLColumns(Table table) throws IOException {
+    disableAllNOTNULLColumns(table, recreatedTables);
+  }
+
+  protected void disableAllNOTNULLColumns(Table table, List<String> recreatedTbls)
+      throws IOException {
     for (int i = 0; i < table.getColumnCount(); i++) {
       Column column = table.getColumn(i);
-      if (column.isRequired() && !column.isPrimaryKey()
-          && !recreatedTables.contains(table.getName())) {
+      if (column.isRequired() && !column.isPrimaryKey() && !recreatedTbls.contains(table.getName())) {
         if (getSqlType(column).equalsIgnoreCase("CLOB")) {
           // In the case of CLOB columns in oracle, it is wrong to specify the type when changing
           // the null/not null constraint
--- a/src/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java	Tue Apr 14 16:27:34 2015 +0200
+++ b/src/org/apache/ddlutils/platform/postgresql/PostgreSqlBuilder.java	Tue Apr 14 17:14:30 2015 +0200
@@ -21,6 +21,7 @@
 
 import java.io.IOException;
 import java.sql.Types;
+import java.util.List;
 import java.util.Map;
 import java.util.Vector;
 
@@ -954,4 +955,18 @@
     }
     printEndOfStatement();
   }
+
+  @Override
+  protected void disableAllNOTNULLColumns(Table table, List<String> recreatedTbls)
+      throws IOException {
+    for (int i = 0; i < table.getColumnCount(); i++) {
+      Column column = table.getColumn(i);
+      if (column.isRequired() && !column.isPrimaryKey() && !recreatedTbls.contains(table.getName())) {
+        println("ALTER TABLE " + table.getName() + " ALTER COLUMN " + getColumnName(column)
+            + " DROP  NOT NULL");
+        printEndOfStatement();
+      }
+
+    }
+  }
 }