diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/MColumn.java b/src/main/java/io/ebeaninternal/dbmigration/model/MColumn.java index 25fe92b09..de71d794d 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/MColumn.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/MColumn.java @@ -15,7 +15,7 @@ import java.util.Objects; */ public class MColumn { - private final String name; + private String name; private String type; private String checkConstraint; private String checkConstraintName; @@ -88,7 +88,6 @@ public class MColumn { * Return a copy of this column used for creating the associated draft table. */ public MColumn copyForDraft() { - MColumn copy = new MColumn(name, type); copy.draftOnly = draftOnly; copy.checkConstraint = checkConstraint; @@ -470,6 +469,14 @@ public class MColumn { this.dbMigrationInfos = dbMigrationInfos; } + /** + * Rename the column. + */ + public MColumn rename(String newName) { + this.name = newName; + return this; + } + /** * Apply changes based on the AlterColumn request. */ diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java b/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java index 5a1dc8ba8..82a687eb7 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/MTable.java @@ -11,6 +11,7 @@ import io.ebeaninternal.dbmigration.migration.DropColumn; import io.ebeaninternal.dbmigration.migration.DropHistoryTable; import io.ebeaninternal.dbmigration.migration.DropTable; import io.ebeaninternal.dbmigration.migration.ForeignKey; +import io.ebeaninternal.dbmigration.migration.RenameColumn; import io.ebeaninternal.dbmigration.migration.UniqueConstraint; import io.ebeaninternal.server.deploy.BeanDescriptor; import io.ebeaninternal.server.deploy.BeanProperty; @@ -415,6 +416,15 @@ public class MTable { } } + public void apply(RenameColumn renameColumn) { + checkTableName(renameColumn.getTableName()); + MColumn column = columns.remove(renameColumn.getOldName()); + if (column == null) { + throw new IllegalStateException("Column [" + renameColumn.getOldName() + "] does not exist for RenameColumn change on table [" + renameColumn.getTableName() + "]?"); + } + addColumn(column.rename(renameColumn.getNewName())); + } + public String getName() { return name; } diff --git a/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java b/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java index 6a8e71463..3f75064d2 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java +++ b/src/main/java/io/ebeaninternal/dbmigration/model/ModelContainer.java @@ -17,6 +17,7 @@ import io.ebeaninternal.dbmigration.migration.DropHistoryTable; import io.ebeaninternal.dbmigration.migration.DropIndex; import io.ebeaninternal.dbmigration.migration.DropTable; import io.ebeaninternal.dbmigration.migration.Migration; +import io.ebeaninternal.dbmigration.migration.RenameColumn; import io.ebeaninternal.dbmigration.migration.Sql; import java.util.ArrayList; @@ -163,6 +164,8 @@ public class ModelContainer { applyChange((AddColumn) change); } else if (change instanceof DropColumn) { applyChange((DropColumn) change); + } else if (change instanceof RenameColumn) { + applyChange((RenameColumn) change); } else if (change instanceof CreateIndex) { applyChange((CreateIndex) change); } else if (change instanceof DropIndex) { @@ -314,6 +317,14 @@ public class ModelContainer { table.apply(dropColumn); } + protected void applyChange(RenameColumn renameColumn) { + MTable table = tables.get(renameColumn.getTableName()); + if (table == null) { + throw new IllegalStateException("Table [" + renameColumn.getTableName() + "] does not exist in model?"); + } + table.apply(renameColumn); + } + /** * Add a table (typically from reading EbeanServer meta data). */ diff --git a/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java b/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java index 368419a08..c9c1dc8c0 100644 --- a/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java +++ b/src/test/java/io/ebeaninternal/dbmigration/model/ModelContainerTest.java @@ -154,6 +154,16 @@ public class ModelContainerTest { container.applyChange(dropHistoryTable); } + @Test + public void apply_renameColumn() { + ModelContainer container = new ModelContainer(); + container.apply(mig("6.0__renameColumn.model.xml"), ver("6.0")); + + final MTable table = container.getTable("document"); + assertThat(table.getColumn("title")).isNotNull(); + assertThat(table.getColumn("short_title")).isNull(); + } + @Test public void getSchemas() { diff --git a/src/test/resources/io/ebeaninternal/dbmigration/model/6.0__renameColumn.model.xml b/src/test/resources/io/ebeaninternal/dbmigration/model/6.0__renameColumn.model.xml new file mode 100644 index 000000000..b37a99b82 --- /dev/null +++ b/src/test/resources/io/ebeaninternal/dbmigration/model/6.0__renameColumn.model.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + +