From b4c987d43004727b860b5b4cd1b6d24fe4c5e361 Mon Sep 17 00:00:00 2001 From: James Moger Date: Fri, 21 Sep 2012 08:18:25 -0400 Subject: [PATCH] Support inheritance of IQVersion for DbUpgrader implementations (issue 10) --- docs/05_releases.mkd | 1 + src/com/iciql/Db.java | 7 ++++ tests/com/iciql/test/UpgradesTest.java | 46 ++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/docs/05_releases.mkd b/docs/05_releases.mkd index 8ce8e12..a209f82 100644 --- a/docs/05_releases.mkd +++ b/docs/05_releases.mkd @@ -5,6 +5,7 @@ **%VERSION%** ([zip](http://code.google.com/p/iciql/downloads/detail?name=%ZIP%)|[jar](http://code.google.com/p/iciql/downloads/detail?name=%JAR%))   *released %BUILDDATE%* - Fixed order of DEFAULT value in create table statement (issue 11) +- Support inheritance of IQVersion for DbUpgrader implementations (issue 10) - Fixed password bug in model generator (issue 7) **1.1.0**   *released 2012-08-20* diff --git a/src/com/iciql/Db.java b/src/com/iciql/Db.java index d436b8d..90e7613 100644 --- a/src/com/iciql/Db.java +++ b/src/com/iciql/Db.java @@ -311,6 +311,13 @@ public class Db { upgradeChecked.add(dbUpgrader.getClass()); IQVersion model = dbUpgrader.getClass().getAnnotation(IQVersion.class); + if (model.value() == 0) { + // try superclass + Class superClass = dbUpgrader.getClass().getSuperclass(); + if (superClass.isAnnotationPresent(IQVersion.class)) { + model = superClass.getAnnotation(IQVersion.class); + } + } if (model.value() > 0) { DbVersion v = new DbVersion(); // (SCHEMA="" && TABLE="") == DATABASE diff --git a/tests/com/iciql/test/UpgradesTest.java b/tests/com/iciql/test/UpgradesTest.java index d4c15e1..7de691f 100644 --- a/tests/com/iciql/test/UpgradesTest.java +++ b/tests/com/iciql/test/UpgradesTest.java @@ -72,6 +72,43 @@ public class UpgradesTest { db.close(); db2.close(); } + + @Test + public void testDatabaseInheritedUpgrade() { + Db db = IciqlSuite.openNewDb(); + + List products = Product.getList(); + + // set the v1 upgrader and insert a record. + // this will trigger the upgrade. + V1DbUpgrader v1 = new V1DbUpgrader(); + db.setDbUpgrader(v1); + db.insert(products.get(0)); + + // confirm that upgrade occurred + assertEquals(0, v1.oldVersion.get()); + assertEquals(1, v1.newVersion.get()); + + // open a second connection to the database + // and then apply the v2 upgrade. + // For H2 its important to keep the first connection + // alive so that the database is not destroyed. + Db db2 = IciqlSuite.openCurrentDb(); + + // set the v2 upgrader and insert a record. + // this will trigger the upgrade. + V2DbUpgraderImpl v2 = new V2DbUpgraderImpl(); + db2.setDbUpgrader(v2); + db2.insert(products.get(1)); + + // confirm that upgrade occurred + assertEquals(1, v2.oldVersion.get()); + assertEquals(2, v2.newVersion.get()); + + db.executeUpdate("DROP TABLE iq_versions"); + db.close(); + db2.close(); + } @Test public void testTableUpgrade() { @@ -131,4 +168,13 @@ public class UpgradesTest { class V2DbUpgrader extends BaseDbUpgrader { } + + /** + * A sample V2 database upgrader class which inherits its + * version from the parent class. + */ + @IQVersion() + class V2DbUpgraderImpl extends V2DbUpgrader { + } + } -- 2.39.5