From 4848c24a0a5e4a6c4deabdad13f615c74921bf36 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 12 Feb 2016 14:33:19 +1300 Subject: [PATCH] #563 - ENH: DDL DB Migration - add a suppressDropsForever="true" attribute such that drops we never want to run are not warned on and can't be selected for a migration. --- .../avaje/ebean/dbmigration/DbMigration.java | 18 +- .../dbmigration/migration/ChangeSet.java | 27 +++ .../dbmigration/model/ModelContainer.java | 11 +- .../ebean/dbmigration/model/PendingDrops.java | 105 +++++++-- src/main/resources/ebean-dbmigration-1.0.xsd | 1 + .../ebean/dbmigration/model/EntryTest.java | 126 ++++++++++ .../dbmigration/model/PendingDropsTest.java | 219 ++++++++++++++++++ 7 files changed, 467 insertions(+), 40 deletions(-) create mode 100644 src/test/java/com/avaje/ebean/dbmigration/model/EntryTest.java create mode 100644 src/test/java/com/avaje/ebean/dbmigration/model/PendingDropsTest.java diff --git a/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java b/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java index 9836917fd..78f14c263 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java +++ b/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java @@ -222,8 +222,9 @@ public class DbMigration { private void generateDiff(Request request) throws IOException { - if (request.hasPendingDrops()) { - logger.info("Pending un-applied drops in versions {}", request.getPendingDrops()); + List pendingDrops = request.getPendingDrops(); + if (!pendingDrops.isEmpty()) { + logger.info("Pending un-applied drops in versions {}", pendingDrops); } ModelDiff diff = request.createDiff(); @@ -240,8 +241,10 @@ public class DbMigration { Migration migration = request.migrationForPendingDrop(pendingVersion); generateMigration(request, migration, pendingVersion); - if (request.hasPendingDrops()) { - logger.info("... remaining pending un-applied drops in versions {}", request.getPendingDrops()); + + List pendingDrops = request.getPendingDrops(); + if (!pendingDrops.isEmpty()) { + logger.info("... remaining pending un-applied drops in versions {}", pendingDrops); } } @@ -267,13 +270,6 @@ public class DbMigration { this.current = currentModel.read(); } - /** - * Return true if there are pending un-applied drops. - */ - public boolean hasPendingDrops() { - return migrated.hasPendingDrops(); - } - /** * Return the migration for the pending drops for a given version. */ diff --git a/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java b/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java index a835aeb71..4db9e88c2 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java +++ b/src/main/java/com/avaje/ebean/dbmigration/migration/ChangeSet.java @@ -28,6 +28,7 @@ import javax.xml.bind.annotation.XmlType; * </sequence> * <attribute name="type" use="required" type="{http://ebean-orm.github.io/xml/ns/dbmigration}changeSetType" /> * <attribute name="dropsFor" type="{http://www.w3.org/2001/XMLSchema}string" /> + * <attribute name="suppressDropsForever" type="{http://www.w3.org/2001/XMLSchema}boolean" /> * <attribute name="generated" type="{http://www.w3.org/2001/XMLSchema}boolean" /> * <attribute name="author" type="{http://www.w3.org/2001/XMLSchema}string" /> * <attribute name="comment" type="{http://www.w3.org/2001/XMLSchema}string" /> @@ -65,6 +66,8 @@ public class ChangeSet { protected ChangeSetType type; @XmlAttribute(name = "dropsFor") protected String dropsFor; + @XmlAttribute(name = "suppressDropsForever") + protected Boolean suppressDropsForever; @XmlAttribute(name = "generated") protected Boolean generated; @XmlAttribute(name = "author") @@ -161,6 +164,30 @@ public class ChangeSet { this.dropsFor = value; } + /** + * Gets the value of the suppressDropsForever property. + * + * @return + * possible object is + * {@link Boolean } + * + */ + public Boolean isSuppressDropsForever() { + return suppressDropsForever; + } + + /** + * Sets the value of the suppressDropsForever property. + * + * @param value + * allowed object is + * {@link Boolean } + * + */ + public void setSuppressDropsForever(Boolean value) { + this.suppressDropsForever = value; + } + /** * Gets the value of the generated property. * diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java b/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java index 2ec4839e1..359538d76 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/ModelContainer.java @@ -92,9 +92,9 @@ public class ModelContainer { if (pending) { // un-applied drop columns etc pendingDrops.add(version, changeSet); + } else if (isDropsFor(changeSet)) { - // applied drops (so no longer pending) - pendingDrops.remove(MigrationVersion.parse(changeSet.getDropsFor())); + pendingDrops.appliedDropsFor(MigrationVersion.parse(changeSet.getDropsFor())); } if (!isDropsFor(changeSet)) { applyChangeSet(changeSet); @@ -265,13 +265,6 @@ public class ModelContainer { indexes.put(indexName, new MIndex(indexName, tableName, columnNames)); } - /** - * Return true if there are pending drops. - */ - public boolean hasPendingDrops() { - return !pendingDrops.isEmpty(); - } - /** * Return the list of versions containing un-applied pending drops. */ diff --git a/src/main/java/com/avaje/ebean/dbmigration/model/PendingDrops.java b/src/main/java/com/avaje/ebean/dbmigration/model/PendingDrops.java index 4c0f44804..27e064ec7 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/model/PendingDrops.java +++ b/src/main/java/com/avaje/ebean/dbmigration/model/PendingDrops.java @@ -37,23 +37,26 @@ public class PendingDrops { List versions = new ArrayList(); for (Entry value : map.values()) { - versions.add(value.version.asString()); + if (value.hasPendingDrops()) { + versions.add(value.version.asString()); + } } return versions; } /** - * Remove the pending drops for a version (as they have been applied). + * All the pending drops for this migration version have been applied so we need + * to remove the (unsuppressed) pending drops for this version. */ - public void remove(MigrationVersion version) { - map.remove(version.normalised()); - } + public boolean appliedDropsFor(MigrationVersion version) { + Entry entry = map.get(version.normalised()); + if (entry.removeDrops()) { + // it had no suppressForever changeSets so remove completely + map.remove(version.normalised()); + return true; + } - /** - * Return true if there are no pending drops. - */ - public boolean isEmpty() { - return map.isEmpty(); + return false; } /** @@ -64,34 +67,43 @@ public class PendingDrops { */ public Migration migrationForVersion(String pendingVersion) { - Entry entry = getChangeSets(pendingVersion); + Entry entry = getEntry(pendingVersion); Migration migration = new Migration(); for (ChangeSet changeSet : entry.list) { - changeSet.setType(ChangeSetType.APPLY); - changeSet.setDropsFor(entry.version.asString()); - migration.getChangeSet().add(changeSet); + if (!isSuppressForever(changeSet)) { + changeSet.setType(ChangeSetType.APPLY); + changeSet.setDropsFor(entry.version.asString()); + migration.getChangeSet().add(changeSet); + } + } + + if (migration.getChangeSet().isEmpty()) { + throw new IllegalArgumentException("The remaining pendingDrops changeSets in migration ["+pendingVersion+"] are suppressDropsForever=true and can't be applied"); + } + + if (!entry.containsSuppressForever()) { + // we can remove it completely as it has no suppressForever changes + map.remove(entry.version.normalised()); } return migration; } - private Entry getChangeSets(String pendingVersion) { + private Entry getEntry(String pendingVersion) { if ("next".equalsIgnoreCase(pendingVersion)) { Iterator it = map.values().iterator(); if (it.hasNext()) { - Entry first = it.next(); - it.remove(); - return first; + return it.next(); } } else { - Entry remove = map.remove(MigrationVersion.parse(pendingVersion).normalised()); + Entry remove = map.get(MigrationVersion.parse(pendingVersion).normalised()); if (remove != null) { return remove; } } - throw new IllegalArgumentException("No pending changeSets for version [" + pendingVersion + "] found"); + throw new IllegalArgumentException("No 'pendingDrops' changeSets for migration version [" + pendingVersion + "] found"); } /** @@ -113,6 +125,14 @@ public class PendingDrops { } } + /** + * Return true if there is an Entry for the given version. + */ + boolean testContainsEntryFor(MigrationVersion version) { + + return map.containsKey(version.normalised()); + } + static class Entry { final MigrationVersion version; @@ -126,6 +146,51 @@ public class PendingDrops { void add(ChangeSet changeSet) { list.add(changeSet); } + + /** + * Return true if this contains suppressForever changeSets. + */ + boolean containsSuppressForever() { + for (ChangeSet changeSet : list) { + if (isSuppressForever(changeSet)) { + return true; + } + } + return false; + } + + /** + * Return true if this contains drops that can be applied / migrated. + */ + boolean hasPendingDrops() { + for (ChangeSet changeSet : list) { + if (!isSuppressForever(changeSet)) { + return true; + } + } + return false; + } + + /** + * Remove the drops that are not suppressForever and return true if that + * removed all the changeSets (and there are no suppressForever ones). + */ + boolean removeDrops() { + + Iterator iterator = list.iterator(); + while (iterator.hasNext()) { + ChangeSet next = iterator.next(); + if (!isSuppressForever(next)) { + iterator.remove(); + } + } + + return list.isEmpty(); + } + } + + private static boolean isSuppressForever(ChangeSet next) { + return Boolean.TRUE.equals(next.isSuppressDropsForever()); } } diff --git a/src/main/resources/ebean-dbmigration-1.0.xsd b/src/main/resources/ebean-dbmigration-1.0.xsd index ab0b7bf08..2ada00820 100644 --- a/src/main/resources/ebean-dbmigration-1.0.xsd +++ b/src/main/resources/ebean-dbmigration-1.0.xsd @@ -25,6 +25,7 @@ + diff --git a/src/test/java/com/avaje/ebean/dbmigration/model/EntryTest.java b/src/test/java/com/avaje/ebean/dbmigration/model/EntryTest.java new file mode 100644 index 000000000..4ccdcd31e --- /dev/null +++ b/src/test/java/com/avaje/ebean/dbmigration/model/EntryTest.java @@ -0,0 +1,126 @@ +package com.avaje.ebean.dbmigration.model; + +import com.avaje.ebean.dbmigration.migration.ChangeSet; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +import static org.assertj.core.api.StrictAssertions.assertThat; + +public class EntryTest { + + + @Test + public void test_when_empty() throws Exception { + + PendingDrops.Entry entry = createEntry(); + assertThat(entry.hasPendingDrops()).isFalse(); + } + + + @Test + public void test_when_normal() throws Exception { + + PendingDrops.Entry entry = createEntry(); + entry.add(new ChangeSet()); + + assertThat(entry.hasPendingDrops()).isTrue(); + } + + @Test + public void test_when_suppressOnly() throws Exception { + + PendingDrops.Entry entry = createEntry(); + + ChangeSet cs = new ChangeSet(); + cs.setSuppressDropsForever(Boolean.TRUE); + entry.add(cs); + + assertThat(entry.hasPendingDrops()).isFalse(); + } + + @Test + public void test_when_both() throws Exception { + + PendingDrops.Entry entry = createEntry(); + + ChangeSet cs = new ChangeSet(); + cs.setSuppressDropsForever(Boolean.TRUE); + entry.add(cs); + entry.add(new ChangeSet()); + + assertThat(entry.hasPendingDrops()).isTrue(); + } + + @Test + public void test_containsSuppressForever_when_empty() { + + PendingDrops.Entry entry = createEntry(); + + assertThat(entry.containsSuppressForever()).isFalse(); + } + + @Test + public void test_containsSuppressForever_when_not() { + + PendingDrops.Entry entry = createEntry(); + entry.add(new ChangeSet()); + + assertThat(entry.containsSuppressForever()).isFalse(); + } + + @Test + public void test_containsSuppressForever_when_does() { + + PendingDrops.Entry entry = createEntry(); + ChangeSet cs = new ChangeSet(); + cs.setSuppressDropsForever(Boolean.TRUE); + entry.add(cs); + + assertThat(entry.containsSuppressForever()).isTrue(); + } + + @Test + public void test_containsSuppressForever_when_mixed() { + + PendingDrops.Entry entry = createEntry(); + ChangeSet cs = new ChangeSet(); + cs.setSuppressDropsForever(Boolean.TRUE); + entry.add(cs); + entry.add(new ChangeSet()); + + assertThat(entry.containsSuppressForever()).isTrue(); + } + + @Test + public void test_removeDrops_when_empty() { + + PendingDrops.Entry entry = createEntry(); + assertThat(entry.removeDrops()).isTrue(); + } + + @Test + public void test_removeDrops_when_notSuppressed() { + + PendingDrops.Entry entry = createEntry(); + entry.add(new ChangeSet()); + assertThat(entry.removeDrops()).isTrue(); + } + + @Test + public void test_removeDrops_when_containsSuppressed() { + + PendingDrops.Entry entry = createEntry(); + entry.add(new ChangeSet()); + ChangeSet cs = new ChangeSet(); + cs.setSuppressDropsForever(Boolean.TRUE); + entry.add(cs); + + assertThat(entry.removeDrops()).isFalse(); + } + + @NotNull + private PendingDrops.Entry createEntry() { + MigrationVersion version = MigrationVersion.parse("1.1"); + return new PendingDrops.Entry(version); + } +} \ No newline at end of file diff --git a/src/test/java/com/avaje/ebean/dbmigration/model/PendingDropsTest.java b/src/test/java/com/avaje/ebean/dbmigration/model/PendingDropsTest.java new file mode 100644 index 000000000..75dfee6ca --- /dev/null +++ b/src/test/java/com/avaje/ebean/dbmigration/model/PendingDropsTest.java @@ -0,0 +1,219 @@ +package com.avaje.ebean.dbmigration.model; + +import com.avaje.ebean.dbmigration.migration.ChangeSet; +import com.avaje.ebean.dbmigration.migration.DropColumn; +import com.avaje.ebean.dbmigration.migration.Migration; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class PendingDropsTest { + + @Test + public void testAdd() throws Exception { + + ChangeSet cs = new ChangeSet(); + + PendingDrops pendingDrops = new PendingDrops(); + pendingDrops.add(MigrationVersion.parse("1.1"), cs); + } + + @Test + public void test_add_appliedDropsFor() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.add(MigrationVersion.parse("1.1"), new ChangeSet()); + assertThat(pendingDrops.pendingDrops()).containsExactly("1.1"); + + pendingDrops.appliedDropsFor(MigrationVersion.parse("1_1")); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + } + + @Test + public void test_add_appliedDropsFor_whenSuppressed() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.add(MigrationVersion.parse("1.1"), newSuppressForeverChangeSet()); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.appliedDropsFor(MigrationVersion.parse("1_1")); + } + + + @Test + public void test_add_appliedDropsFor_whenBoth() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.add(MigrationVersion.parse("1.1"), newSuppressForeverChangeSet()); + pendingDrops.add(MigrationVersion.parse("1.1"), new ChangeSet()); + assertThat(pendingDrops.pendingDrops()).containsExactly("1.1"); + + pendingDrops.appliedDropsFor(MigrationVersion.parse("1_1")); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + } + + @Test + public void test_pendingDrops() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.add(MigrationVersion.parse("1.1"), new ChangeSet()); + pendingDrops.add(MigrationVersion.parse("1.2"), new ChangeSet()); + assertThat(pendingDrops.pendingDrops()).containsExactly("1.1", "1.2"); + } + + @Test + public void test_pendingDrops_when_suppressForever() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.add(MigrationVersion.parse("1.1"), newSuppressForeverChangeSet()); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.add(MigrationVersion.parse("1.2"), new ChangeSet()); + assertThat(pendingDrops.pendingDrops()).containsExactly("1.2"); + } + + @Test + public void test_pendingDrops_when_both() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + + pendingDrops.add(MigrationVersion.parse("1.1"), newSuppressForeverChangeSet()); + pendingDrops.add(MigrationVersion.parse("1.1"), new ChangeSet()); + assertThat(pendingDrops.pendingDrops()).containsExactly("1.1"); + + pendingDrops.add(MigrationVersion.parse("1.2"), new ChangeSet()); + assertThat(pendingDrops.pendingDrops()).containsExactly("1.1", "1.2"); + + pendingDrops.appliedDropsFor(MigrationVersion.parse("1_1")); + assertThat(pendingDrops.pendingDrops()).containsExactly("1.2"); + + pendingDrops.appliedDropsFor(MigrationVersion.parse("1_2")); + assertThat(pendingDrops.pendingDrops()).isEmpty(); + } + + @Test + public void testMigrationForVersion() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + + ChangeSet applyDropChangeSet1 = new ChangeSet(); + ChangeSet applyDropChangeSet2 = new ChangeSet(); + + MigrationVersion version = MigrationVersion.parse("1.1"); + pendingDrops.add(version, applyDropChangeSet1); + pendingDrops.add(version, applyDropChangeSet2); + + Migration migration = pendingDrops.migrationForVersion("1_1"); + assertThat(migration.getChangeSet()).containsExactly(applyDropChangeSet1, applyDropChangeSet2); + + assertThat(pendingDrops.testContainsEntryFor(version)).isFalse(); + } + + @Test + public void testMigrationForVersion_when_both() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + + ChangeSet applyDropChangeSet = new ChangeSet(); + MigrationVersion version = MigrationVersion.parse("1.1"); + pendingDrops.add(version, newSuppressForeverChangeSet()); + pendingDrops.add(version, applyDropChangeSet); + + Migration migration = pendingDrops.migrationForVersion("1_1"); + assertThat(migration.getChangeSet()).containsExactly(applyDropChangeSet); + + assertThat(pendingDrops.testContainsEntryFor(version)).isTrue(); + } + + @Test + public void testMigrationForVersion_when_next() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + MigrationVersion version = MigrationVersion.parse("1.1"); + + ChangeSet applyDropChangeSet = new ChangeSet(); + pendingDrops.add(version, newSuppressForeverChangeSet()); + pendingDrops.add(version, applyDropChangeSet); + + + Migration migration = pendingDrops.migrationForVersion("next"); + assertThat(migration.getChangeSet()).containsExactly(applyDropChangeSet); + assertThat(pendingDrops.testContainsEntryFor(version)).isTrue(); + } + + @Test(expected = IllegalArgumentException.class) + public void testMigrationForVersion_when_next_isSuppressForever() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + pendingDrops.add(MigrationVersion.parse("1.1"), newSuppressForeverChangeSet()); + pendingDrops.migrationForVersion("next"); + } + + @Test(expected = IllegalArgumentException.class) + public void testMigrationForVersion_when_doesNotExist() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + pendingDrops.migrationForVersion("1_1"); + } + + @Test(expected = IllegalArgumentException.class) + public void testMigrationForVersion_when_next_doesNotExist() throws Exception { + + PendingDrops pendingDrops = new PendingDrops(); + pendingDrops.migrationForVersion("next"); + } + + private ChangeSet newSuppressForeverChangeSet() { + ChangeSet changeSet = new ChangeSet(); + changeSet.setSuppressDropsForever(Boolean.TRUE); + return changeSet; + } + + + @Test + public void testRegisterPendingHistoryDropColumns() throws Exception { + + TDModelContainer modelContainer = new TDModelContainer(); + + + DropColumn drop1 = new DropColumn(); + drop1.setWithHistory(Boolean.TRUE); + + DropColumn drop2 = new DropColumn(); + + ChangeSet changeSet = new ChangeSet(); + changeSet.getChangeSetChildren().add(drop1); + changeSet.getChangeSetChildren().add(drop2); + + PendingDrops pendingDrops = new PendingDrops(); + pendingDrops.add(MigrationVersion.parse("1.1"), changeSet); + pendingDrops.registerPendingHistoryDropColumns(modelContainer); + + assertThat(modelContainer.drops).containsExactly(drop1); + } + + class TDModelContainer extends ModelContainer { + + List drops = new ArrayList(); + + @Override + public void registerPendingDropColumn(DropColumn dropColumn) { + drops.add(dropColumn); + } + } +} \ No newline at end of file