#2702 - IllegalArgumentException: No rule for io.ebeaninternal.dbmigration.migration.RenameTable

Add support for applying the RenameTable change to the model.
This commit is contained in:
Rob Bygrave
2022-05-24 10:48:38 +12:00
parent 8106ffd73d
commit 68680ffd34
6 changed files with 109 additions and 28 deletions
@@ -28,7 +28,7 @@ public class MTable {
private static final Logger logger = LoggerFactory.getLogger(MTable.class);
private final String name;
private String name;
private MTable draftTable;
/**
* Marked true for draft tables. These need to have their FK references adjusted
@@ -408,6 +408,14 @@ public class MTable {
addColumn(column.rename(renameColumn.getNewName()));
}
/**
* Apply table rename to the model.
*/
public void apply(RenameTable renameTable) {
checkTableName(renameTable.getOldName());
this.name = renameTable.getNewName();
}
public String getName() {
return name;
}
@@ -547,7 +555,7 @@ public class MTable {
private void checkTableName(String tableName) {
if (!name.equals(tableName)) {
throw new IllegalArgumentException("addColumn tableName [" + tableName + "] does not match [" + name + "]");
throw new IllegalArgumentException("tableName [" + tableName + "] does not match [" + name + "]");
}
}
@@ -2,24 +2,7 @@ package io.ebeaninternal.dbmigration.model;
import io.ebean.migration.MigrationVersion;
import io.ebeaninternal.dbmigration.ddlgeneration.platform.DdlHelp;
import io.ebeaninternal.dbmigration.migration.AddColumn;
import io.ebeaninternal.dbmigration.migration.AddHistoryTable;
import io.ebeaninternal.dbmigration.migration.AddTableComment;
import io.ebeaninternal.dbmigration.migration.AddUniqueConstraint;
import io.ebeaninternal.dbmigration.migration.AlterColumn;
import io.ebeaninternal.dbmigration.migration.AlterForeignKey;
import io.ebeaninternal.dbmigration.migration.AlterTable;
import io.ebeaninternal.dbmigration.migration.ChangeSet;
import io.ebeaninternal.dbmigration.migration.ChangeSetType;
import io.ebeaninternal.dbmigration.migration.CreateIndex;
import io.ebeaninternal.dbmigration.migration.CreateTable;
import io.ebeaninternal.dbmigration.migration.DropColumn;
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 io.ebeaninternal.dbmigration.migration.*;
import io.ebeaninternal.server.deploy.TablespaceMeta;
import java.util.ArrayList;
@@ -122,14 +105,11 @@ public class ModelContainer {
* Apply a migration with associated changeSets to the model.
*/
public void apply(Migration migration, MigrationVersion version) {
List<ChangeSet> changeSets = migration.getChangeSet();
for (ChangeSet changeSet : changeSets) {
for (ChangeSet changeSet : migration.getChangeSet()) {
boolean pending = changeSet.getType() == ChangeSetType.PENDING_DROPS;
if (pending) {
// un-applied drop columns etc
pendingDrops.add(version, changeSet);
} else if (isDropsFor(changeSet)) {
pendingDrops.appliedDropsFor(changeSet);
}
@@ -150,15 +130,15 @@ public class ModelContainer {
* Apply a changeSet to the model.
*/
protected void applyChangeSet(ChangeSet changeSet) {
List<Object> changeSetChildren = changeSet.getChangeSetChildren();
for (Object change : changeSetChildren) {
for (Object change : changeSet.getChangeSetChildren()) {
if (change instanceof CreateTable) {
applyChange((CreateTable) change);
} else if (change instanceof DropTable) {
applyChange((DropTable) change);
} else if (change instanceof AlterTable) {
applyChange((AlterTable) change);
} else if (change instanceof RenameTable) {
applyChange((RenameTable) change);
} else if (change instanceof AlterColumn) {
applyChange((AlterColumn) change);
} else if (change instanceof AddColumn) {
@@ -265,7 +245,7 @@ public class ModelContainer {
protected void applyChange(DropTable dropTable) {
tables.remove(dropTable.getName());
}
/**
* Apply a AlterTable change to the model.
*/
@@ -294,6 +274,19 @@ public class ModelContainer {
}
}
protected void applyChange(RenameTable renameTable) {
MTable notExpected = tables.get(renameTable.getNewName());
if (notExpected != null) {
throw new IllegalStateException("RenameTable to newName [" + renameTable.getNewName() + "] but this table already exists in the model?");
}
MTable table = tables.remove(renameTable.getOldName());
if (table == null) {
throw new IllegalStateException("RenameTable oldName [" + renameTable.getOldName() + "] does not exist in model?");
}
table.apply(renameTable);
tables.put(renameTable.getNewName(), table);
}
/**
* Apply a CreateTable change to the model.
*/
@@ -10,6 +10,7 @@ import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class ModelContainerTest {
@@ -152,6 +153,42 @@ class ModelContainerTest {
assertThat(table.getColumn("short_title")).isNull();
}
@Test
void apply_renameTable() {
ModelContainer container = new ModelContainer();
container.apply(mig("7.0__renameTable.model.xml"), ver("7.0"));
final MTable table = container.getTable("document_newname");
assertThat(table.getColumn("title")).isNotNull();
assertThat(table.getColumn("short_title")).isNull();
}
@Test
void apply_renameTable_when_oldTableNotFound() {
ModelContainer container = new ModelContainer();
assertThatThrownBy(() -> {
container.apply(mig("7.0__renameTable.errOldTable.xml"), ver("7.0"));
}).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("RenameTable oldName");
final MTable table = container.getTable("document");
assertThat(table.getColumn("title")).isNotNull();
}
@Test
void apply_renameTable_when_newTableAlreadyExists() {
ModelContainer container = new ModelContainer();
assertThatThrownBy(() -> {
container.apply(mig("7.0__renameTable.errNewTable.xml"), ver("7.0"));
}).isInstanceOf(IllegalStateException.class)
.hasMessageContaining("RenameTable to newName");
final MTable table = container.getTable("document");
assertThat(table.getColumn("title")).isNotNull();
}
@Test
void getSchemas() {
MTable t0 = new MTable("foo.one");
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet type="apply">
<createTable name="document" withHistory="true" pkName="pk_document">
<column name="id" type="bigint" primaryKey="true" references="document_draft.id" foreignKeyName="fk_document_id"/>
<column name="title" type="varchar(255)"/>
</createTable>
<createTable name="document_newname" pkName="pk_document">
<column name="id" type="bigint"/>
</createTable>
<renameTable oldName="document" newName="document_newname"/>
</changeSet>
</migration>
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet type="apply">
<createTable name="document" withHistory="true" pkName="pk_document">
<column name="id" type="bigint" primaryKey="true" references="document_draft.id" foreignKeyName="fk_document_id"/>
<column name="title" type="varchar(255)"/>
</createTable>
<renameTable oldName="document_doesNotExist" newName="document_newname"/>
</changeSet>
</migration>
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet type="apply">
<createTable name="document" withHistory="true" pkName="pk_document">
<column name="id" type="bigint" primaryKey="true" references="document_draft.id" foreignKeyName="fk_document_id"/>
<column name="title" type="varchar(255)"/>
</createTable>
<renameTable oldName="document" newName="document_newname"/>
</changeSet>
</migration>