Author: johnthuss Date: Thu Feb 9 19:29:34 2017 New Revision: 1782379 URL: http://svn.apache.org/viewvc?rev=1782379&view=rev Log: Add more efficient way to add columns to existing tables
Simplify database-specific SQL file naming Modified: cayenne/sandbox/cayenne-migrations/.classpath cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migration.java cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/MigrationColumnNew.java cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/SqlFileMigration.java Modified: cayenne/sandbox/cayenne-migrations/.classpath URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/.classpath?rev=1782379&r1=1782378&r2=1782379&view=diff ============================================================================== --- cayenne/sandbox/cayenne-migrations/.classpath (original) +++ cayenne/sandbox/cayenne-migrations/.classpath Thu Feb 9 19:29:34 2017 @@ -1,10 +1,32 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="src" output="target/classes" path="src/main/java"/> - <classpathentry kind="src" output="target/test-classes" path="src/test/java"/> - <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"/> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> - <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"/> + <classpathentry kind="src" output="target/classes" path="src/main/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="src" output="target/test-classes" path="src/test/java"> + <attributes> + <attribute name="optional" value="true"/> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> + <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> + <attributes> + <attribute name="maven.pomderived" value="true"/> + </attributes> + </classpathentry> <classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/> <classpathentry kind="output" path="target/classes"/> </classpath> Modified: cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migration.java URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migration.java?rev=1782379&r1=1782378&r2=1782379&view=diff ============================================================================== --- cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migration.java (original) +++ cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migration.java Thu Feb 9 19:29:34 2017 @@ -63,8 +63,8 @@ public abstract class Migration { protected static final boolean MANDATORY = true; protected static final boolean OPTIONAL = false; - private DataNode node; - private MigrationDatabase database; + private final DataNode node; + private final MigrationDatabase database; public Migration(DataNode node) { this.node = node; 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=1782379&r1=1782378&r2=1782379&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 Feb 9 19:29:34 2017 @@ -18,6 +18,11 @@ ****************************************************************/ package org.apache.cayenne.migration; +import java.util.List; + +import org.apache.cayenne.dba.postgres.PostgresAdapter; +import org.apache.cayenne.merge.AbstractToDbToken; +import org.apache.cayenne.merge.ArbitrarySqlToDb; import org.apache.cayenne.merge.MergerToken; /** @@ -46,19 +51,34 @@ 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); - } + if (Migrator.USE_EFFICIENT_ALTER_TABLE) { + MergerToken op = factory().createAddColumnToDb(getTable().getEntity(), getAttribute()); + String sql = ((AbstractToDbToken)op).createSql(getTable().getDatabase().getAdapter()).get(0); + + if (getDefaultValue() != null) { + sql += " DEFAULT " + sqlForLiteral(getDefaultValue()); + } + + if (getAttribute().isMandatory()) { + sql += " NOT NULL"; + } + + getTable().getDatabase().addOperation(new ArbitrarySqlToDb(sql)); + } else { + 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/main/java/org/apache/cayenne/migration/Migrator.java URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java?rev=1782379&r1=1782378&r2=1782379&view=diff ============================================================================== --- cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java (original) +++ cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/Migrator.java Thu Feb 9 19:29:34 2017 @@ -42,6 +42,8 @@ public class Migrator { //private static final Logger log = Logger.getLogger(Migrator.class); + public static boolean USE_EFFICIENT_ALTER_TABLE = false; + private final DataNode node; private final String migrationsPackage; private Connection connection; @@ -154,7 +156,7 @@ public class Migrator { node.getAdapter().mergerFactory(); // force adapter to resolve early getConnection(); - + for (DataMap map : node.getDataMaps()) { int version = currentDbVersion(map)+1; Modified: cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/SqlFileMigration.java URL: http://svn.apache.org/viewvc/cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/SqlFileMigration.java?rev=1782379&r1=1782378&r2=1782379&view=diff ============================================================================== --- cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/SqlFileMigration.java (original) +++ cayenne/sandbox/cayenne-migrations/src/main/java/org/apache/cayenne/migration/SqlFileMigration.java Thu Feb 9 19:29:34 2017 @@ -83,20 +83,8 @@ public class SqlFileMigration extends Mi * @return */ protected String databaseSpecificSqlFilename() { - Connection conn = null; - try { - conn = getDataNode().getDataSource().getConnection(); - String databaseProductName = getDatabase().getDatabaseProductName().replace(" ", ""); - return getClass().getSimpleName() + "-" + databaseProductName + ".sql"; - } catch (SQLException e) { - throw new RuntimeException(e); - } finally { - if (conn != null) { - try { - conn.close(); - } catch (SQLException e) {} - } - } + String databaseProductName = getDatabase().getDatabaseProductName().replace(" ", ""); + return getClass().getSimpleName() + "-" + databaseProductName + ".sql"; } /**