cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnth...@apache.org
Subject svn commit: r1516617 - in /cayenne/sandbox/cayenne-migrations/src: main/java/org/apache/cayenne/migration/ test/java/org/apache/cayenne/migration/
Date Thu, 22 Aug 2013 21:30:58 GMT
Author: johnthuss
Date: Thu Aug 22 21:30:58 2013
New Revision: 1516617

URL: http://svn.apache.org/r1516617
Log:
Fix adding a new column to an existing table with a NOT NULL constraint

Modified:
    cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/MigrationColumnNew.java
    cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationColumnNewTest.java
    cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationTableExistingTest.java

Modified: cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/MigrationColumnNew.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/MigrationColumnNew.java?rev=1516617&r1=1516616&r2=1516617&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/MigrationColumnNew.java
(original)
+++ cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/MigrationColumnNew.java
Thu Aug 22 21:30:58 2013
@@ -35,9 +35,7 @@ public class MigrationColumnNew extends 
 		super(table, name, jdbcType, maxLength, precision, scale, isMandatory, defaultValue);
 		if (!table.isNew()) {
 			create();
-		}
-	      
-        if (getDefaultValue() != null) {
+		} else if (getDefaultValue() != null) {
             setDefault(getDefaultValue());
         }
 	}
@@ -50,6 +48,17 @@ public class MigrationColumnNew extends 
 	protected void create() {
 		MergerToken op = factory().createAddColumnToDb(getTable().getEntity(), getAttribute());
 		getTable().getDatabase().addOperation(op);
+		
+		if (getDefaultValue() != null) {
+            setDefault(getDefaultValue());
+        }
+		
+		if (getAttribute().isMandatory()) {
+		    getTable().getDatabase().execute("UPDATE " +  getTable().getEntity().getFullyQualifiedName()
+ " SET " + getAttribute().getName() + " = " + sqlForLiteral(getDefaultValue()));
+		    
+            op = factory().createSetNotNullToDb(getTable().getEntity(), getAttribute());
+            getTable().getDatabase().addOperation(op);
+		}
 	}
 	
 }

Modified: cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationColumnNewTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationColumnNewTest.java?rev=1516617&r1=1516616&r2=1516617&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationColumnNewTest.java
(original)
+++ cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationColumnNewTest.java
Thu Aug 22 21:30:58 2013
@@ -28,7 +28,9 @@ import org.apache.cayenne.configuration.
 import org.apache.cayenne.configuration.RuntimeProperties;
 import org.apache.cayenne.dba.postgres.PostgresAdapter;
 import org.apache.cayenne.merge.AddColumnToDb;
+import org.apache.cayenne.merge.ArbitrarySqlToDb;
 import org.apache.cayenne.merge.CreateTableToDb;
+import org.apache.cayenne.merge.SetNotNullToDb;
 import org.apache.cayenne.merge.SetValueForNullToDb;
 
 public class MigrationColumnNewTest extends TestCase {
@@ -87,9 +89,11 @@ public class MigrationColumnNewTest exte
 		MigrationTable table = db.alterTable("table");
 		subject = new MigrationColumnNew(table, "column", Types.INTEGER, 0, 0, 0, true, 1);
 
-		assertEquals(2, subject.getTable().getDatabase().getOperations().size());
+		assertEquals(4, subject.getTable().getDatabase().getOperations().size());
 		assertTrue(subject.getTable().getDatabase().getOperations().get(0) instanceof AddColumnToDb);
 	    assertTrue(subject.getTable().getDatabase().getOperations().get(1) instanceof SetValueForNullToDb);
+	    assertTrue(subject.getTable().getDatabase().getOperations().get(2) instanceof ArbitrarySqlToDb);
+        assertTrue(subject.getTable().getDatabase().getOperations().get(3) instanceof SetNotNullToDb);
 
 //		AddColumnToDb op = (AddColumnToDb) subject.getTable().getDatabase().getOperations().get(0);
 //		List<String> statements = op.createSql(node.getAdapter());

Modified: cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationTableExistingTest.java
URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationTableExistingTest.java?rev=1516617&r1=1516616&r2=1516617&view=diff
==============================================================================
--- cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationTableExistingTest.java
(original)
+++ cayenne/sandbox/cayenne-migrations/src/test/java/org/apache/cayenne/migration/MigrationTableExistingTest.java
Thu Aug 22 21:30:58 2013
@@ -31,8 +31,10 @@ import org.apache.cayenne.map.DbAttribut
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.merge.AddColumnToDb;
 import org.apache.cayenne.merge.AddRelationshipToDb;
+import org.apache.cayenne.merge.ArbitrarySqlToDb;
 import org.apache.cayenne.merge.DropColumnToDb;
 import org.apache.cayenne.merge.DropRelationshipToDb;
+import org.apache.cayenne.merge.SetNotNullToDb;
 import org.apache.cayenne.merge.SetPrimaryKeyToDb;
 import org.apache.cayenne.merge.SetValueForNullToDb;
 
@@ -87,9 +89,11 @@ public class MigrationTableExistingTest 
 	public void testAddColumnWithDefault() {
 	    MigrationTableExisting table = db.alterTable("table");
 	    table.addColumn("column", Types.INTEGER, true, 1);
-	    assertEquals(2, table.getDatabase().getOperations().size());
+	    assertEquals(4, table.getDatabase().getOperations().size());
 	    assertTrue(table.getDatabase().getOperations().get(0) instanceof AddColumnToDb);
 	    assertTrue(table.getDatabase().getOperations().get(1) instanceof SetValueForNullToDb);
+	    assertTrue(table.getDatabase().getOperations().get(2) instanceof ArbitrarySqlToDb);
+        assertTrue(table.getDatabase().getOperations().get(3) instanceof SetNotNullToDb);
 
         AddColumnToDb operation = (AddColumnToDb) table.getDatabase().getOperations().get(0);
         assertEquals("column", operation.getColumn().getName());



Mime
View raw message