Skip to content

Commit

Permalink
WIP: Db migration alterColumn ...
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Aug 10, 2015
1 parent c0d4dac commit 845faf7
Show file tree
Hide file tree
Showing 48 changed files with 6,928 additions and 428 deletions.
1,311 changes: 1,311 additions & 0 deletions dbmigration-test/resources/dbmigration/myapp/migration-current.xml

Large diffs are not rendered by default.

1,311 changes: 1,311 additions & 0 deletions ms-migration.xml

Large diffs are not rendered by default.

1,311 changes: 1,311 additions & 0 deletions mysql-migration.xml

Large diffs are not rendered by default.

1,311 changes: 1,311 additions & 0 deletions ora-migration.xml

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions src/main/java/com/avaje/ebean/config/DbMigrationConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.avaje.ebean.config;

/**
* Configuration for the DB migration processing.
*/
public class DbMigrationConfig {

/**
* The application name which is used as the unique code when applying migrations.
*/
private String appName;

/**
* Path where migration
*/
private String resourcePath;

public String getAppName() {
return appName;
}

public void setAppName(String appName) {
this.appName = appName;
}

public String getResourcePath() {
return resourcePath;
}

public void setResourcePath(String resourcePath) {
this.resourcePath = resourcePath;
}

/**
* Load the settings from the PropertiesWrapper.
*/
public void loadSettings(PropertiesWrapper properties) {

appName = properties.get("migration.appName", appName);
resourcePath = properties.get("migration.resourcePath", resourcePath);
}
}
23 changes: 22 additions & 1 deletion src/main/java/com/avaje/ebean/config/ServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ public class ServerConfig {
*/
private DataSourceConfig dataSourceConfig = new DataSourceConfig();

/**
* The db migration config (migration resource path etc).
*/
private DbMigrationConfig migrationConfig = new DbMigrationConfig();

/**
* Set to true if the DataSource uses autoCommit.
* <p>
Expand Down Expand Up @@ -502,7 +507,7 @@ public void setPersistBatch(PersistBatch persistBatch) {
/**
* Return the JDBC batch mode to use per save(), delete(), insert() or update() request.
* <p>
* This makes sense when a save() or delete() etc cascades and executes multiple child statements. The best caase
* This makes sense when a save() or delete() cascades and executes multiple child statements. The best case
* for this is when saving a master/parent bean this cascade inserts many detail/child beans.
* </p>
* <p>
Expand Down Expand Up @@ -620,6 +625,20 @@ public void setDatabaseSequenceBatchSize(int databaseSequenceBatchSize) {
this.databaseSequenceBatchSize = databaseSequenceBatchSize;
}

/**
* Return the DB migration configuration.
*/
public DbMigrationConfig getMigrationConfig() {
return migrationConfig;
}

/**
* Set the DB migration configuration.
*/
public void setMigrationConfig(DbMigrationConfig migrationConfig) {
this.migrationConfig = migrationConfig;
}

/**
* Return the suffix appended to the base table to derive the view that contains the union
* of the base table and the history table in order to support asOf queries.
Expand Down Expand Up @@ -1856,6 +1875,8 @@ protected void loadAutofetchSettings(PropertiesWrapper p) {
*/
protected void loadSettings(PropertiesWrapper p) {

migrationConfig.loadSettings(p);

namingConvention = createNamingConvention(p, namingConvention);
if (namingConvention != null) {
namingConvention.loadFromProperties(p);
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/avaje/ebean/dbmigration/DbMigration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.avaje.ebean.dbmigration;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServer;
import com.avaje.ebean.config.DbMigrationConfig;
import com.avaje.ebean.dbmigration.model.CurrentModel;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
*
*/
public class DbMigration {

private static final Logger logger = LoggerFactory.getLogger(DbMigration.class);

private final SpiEbeanServer server;

private final DbMigrationConfig migrationConfig;

public DbMigration() {
this(Ebean.getDefaultServer());
}

public DbMigration(EbeanServer ebeanServer) {
this.server = (SpiEbeanServer) ebeanServer;
this.migrationConfig = server.getServerConfig().getMigrationConfig();
}



public void writeCurrent() {

CurrentModel currentModel = new CurrentModel(server);

File writeTo = getWritePath();
logger.info("... write to {}", writeTo.getAbsolutePath());
currentModel.writeMigration(writeTo);
}

public File getWritePath() {
File resourceRootDir = new File("./dbmigration-test/resources");

// expect to be a relative path
String resourcePath = migrationConfig.getResourcePath();

File path = new File(resourceRootDir, resourcePath);
if (!path.exists()) {
path.mkdirs();
}
return new File(path, "migration-current.xml");
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/avaje/ebean/dbmigration/DbOffline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.avaje.ebean.dbmigration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
*/
public class DbOffline {

private static final Logger logger = LoggerFactory.getLogger(DbOffline.class);

private static final String KEY = "ebean.dboffline";

public static final String H2 = "H2";

public static void setPlatform(String platformName) {
System.setProperty(KEY, platformName);
}

public static String getPlatform() {
return System.getProperty(KEY);
}

public static void asH2() {
setPlatform(H2);
}

public static boolean isSet() {
return getPlatform() != null;
}

public static void reset() {
System.clearProperty(KEY);
logger.info("reset");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.avaje.ebean.dbmigration.ddlgeneration.platform.DdlNamingConvention;
import com.avaje.ebean.dbmigration.ddlgeneration.platform.PlatformDdl;
import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.ChangeSet;
import com.avaje.ebean.dbmigration.migration.CreateTable;
import com.avaje.ebean.dbmigration.migration.DropColumn;
Expand All @@ -23,7 +24,7 @@ public class BaseDdlHandler implements DdlHandler {

public BaseDdlHandler(DdlNamingConvention namingConvention, PlatformDdl platformDdl) {
this.tableDdl = new BaseTableDdl(namingConvention, platformDdl);
this.columnDdl = new BaseColumnDdl();
this.columnDdl = new BaseColumnDdl(platformDdl);
}

@Override
Expand All @@ -37,6 +38,8 @@ public void generate(DdlWrite writer, ChangeSet changeSet) throws IOException {
generate(writer, (AddColumn) change);
} else if (change instanceof DropColumn) {
generate(writer, (DropColumn) change);
} else if (change instanceof AlterColumn) {
generate(writer, (AlterColumn) change);
}
}
}
Expand All @@ -56,4 +59,8 @@ public void generate(DdlWrite writer, DropColumn dropColumn) throws IOException
columnDdl.generate(writer, dropColumn);
}

@Override
public void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException {
columnDdl.generate(writer, alterColumn);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.avaje.ebean.dbmigration.ddlgeneration;

import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.DropColumn;

import java.io.IOException;

/**
* Write AddColumn or DropColumn.
* Write DDL for AddColumn , DropColumn or AlterColumn.
*/
public interface ColumnDdl {

/**
* Write a AddColumn change.
* Write the add column change.
*/
void generate(DdlWrite writer, AddColumn addColumn) throws IOException;

/**
* Write a DropColumn change.
* Write the drop column change.
*/
void generate(DdlWrite writer, DropColumn dropColumn) throws IOException;

/**
* Write the alter column changes.
*/
void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.avaje.ebean.dbmigration.ddlgeneration;

import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.ChangeSet;
import com.avaje.ebean.dbmigration.migration.CreateTable;
import com.avaje.ebean.dbmigration.migration.DropColumn;
Expand All @@ -18,4 +19,7 @@ public interface DdlHandler {
void generate(DdlWrite writer, AddColumn addColumn) throws IOException;

void generate(DdlWrite writer, DropColumn dropColumn) throws IOException;
}

void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.avaje.ebean.dbmigration.ddlgeneration.DdlBuffer;
import com.avaje.ebean.dbmigration.ddlgeneration.DdlWrite;
import com.avaje.ebean.dbmigration.migration.AddColumn;
import com.avaje.ebean.dbmigration.migration.AlterColumn;
import com.avaje.ebean.dbmigration.migration.Column;
import com.avaje.ebean.dbmigration.migration.DropColumn;

Expand All @@ -14,6 +15,11 @@
*/
public class BaseColumnDdl implements ColumnDdl {

protected final PlatformDdl platformDdl;

public BaseColumnDdl(PlatformDdl platformDdl) {
this.platformDdl = platformDdl;
}

@Override
public void generate(DdlWrite writer, AddColumn addColumn) throws IOException {
Expand All @@ -40,6 +46,89 @@ public void generate(DdlWrite writer, DropColumn dropColumn) throws IOException
// are put into a separate changeSet that is run last
}

@Override
public void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException {

if (isTrue(alterColumn.isHistoryExclude())) {
historyExcludeColumn(writer, alterColumn);
} else if (isFalse(alterColumn.isHistoryExclude())) {
historyIncludeColumn(writer, alterColumn);
}

if (hasValue(alterColumn.getOldReferences())) {
dropForeignKey(writer, alterColumn);
}
if (hasValue(alterColumn.getNewReferences())) {
addForeignKey(writer, alterColumn);
}

if (isTrue(alterColumn.isUnique())) {
addUniqueConstraint(writer, alterColumn);
} else if (isFalse(alterColumn.isUnique())) {
dropUniqueConstraint(writer, alterColumn);
}

if (isTrue(alterColumn.isUniqueOneToOne())) {
addUniqueOneToOneConstraint(writer, alterColumn);
} else if (isFalse(alterColumn.isUniqueOneToOne())) {
dropUniqueOneToOneConstraint(writer, alterColumn);
}

}



protected void addForeignKey(DdlWrite writer, AlterColumn alterColumn) {

}

protected void dropForeignKey(DdlWrite writer, AlterColumn alterColumn) {

}

protected void dropUniqueOneToOneConstraint(DdlWrite writer, AlterColumn alterColumn) {

}

protected void addUniqueOneToOneConstraint(DdlWrite writer, AlterColumn alterColumn) {

}

protected void dropUniqueConstraint(DdlWrite writer, AlterColumn alter) throws IOException {

String tableName = alter.getTableName();
String columnName = alter.getColumnName();
String uqName = platformDdl.namingConvention.uniqueConstraintName(tableName, columnName, 50);

writer.apply()
.append(platformDdl.dropIndex(uqName, tableName))
.endOfStatement();
}

protected void addUniqueConstraint(DdlWrite writer, AlterColumn alter) throws IOException {

String tableName = alter.getTableName();
String columnName = alter.getColumnName();
String uqName = platformDdl.namingConvention.uniqueConstraintName(tableName, columnName, 50);

String[] cols = {columnName};
writer.apply()
.append(platformDdl.createExternalUniqueForOneToOne(uqName, tableName, cols))
.endOfStatement();

writer.rollbackForeignKeys()
.append(platformDdl.dropIndex(uqName, tableName))
.endOfStatement();
}

protected void historyIncludeColumn(DdlWrite writer, AlterColumn alterColumn) {
platformDdl.historyIncludeColumn(writer, alterColumn);
}

protected void historyExcludeColumn(DdlWrite writer, AlterColumn alterColumn) {
platformDdl.historyExcludeColumn(writer, alterColumn);
}

protected void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException {

buffer.append("alter table ").append(tableName)
Expand All @@ -63,6 +152,14 @@ protected void alterTableAddColumn(DdlBuffer buffer, String tableName, Column co
}


protected boolean isFalse(Boolean value) {
return value != null && !value;
}

protected boolean isTrue(Boolean value) {
return value != null && value;
}

protected boolean hasValue(String value) {
return value != null && !value.trim().isEmpty();
}
Expand Down
Loading

0 comments on commit 845faf7

Please sign in to comment.