DropCreate Migrator

alt text alt text alt text

As mentioned earlier, DropCreate Migrator is just simple utility that would generate drop and create statements of your new schema version and would try to apply it at KittyDatabaseHelper.onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) method call. In this case DropCreate Migrator only process new schema table names, so if in new schema there is no table that is present in older one then this table would stay at database. Also, notice that DropCreate Migrator can fail if any constraint violation happens. This can be, for example, in a case when generated drop script tries to delete two tables, where one table refers to another one with usage of FOREIGN KEY constraint. If in such case there would be an attempt to delete referred table first, then constraint violation would occur.

To prevent constraint violation in case when you are using FOREIGN KEY references - just use predefined CREATE and DROP schema scripts defined, for example, at overloaded getPreGeneratedCreateStatements(KittyDatabaseConfiguration dbConf) and getPreGeneratedDropStatements(KittyDatabaseConfiguration dbConf) methods of your KittyDatabase (more info at tip №5 of lesson 4 tab 1 “Speed Up KittyORM” in this demo).

This migration option is default, so you do not need anything to do. However, if for some reason you wish to clearly define this onUpgrade behavior for your database, than just set @KITTY_DATABASE_HELPER.onUpgradeBehavior to KITTY_DATABASE_HELPER.UpgradeBehavior.DROP_AND_CREATE:

 1@KITTY_DATABASE(
 2        isLoggingOn = true,
 3        isProductionOn = false,
 4        databaseName = "mig",
 5        databaseVersion = 2,
 6        logTag = MigrationDBv2.LTAG,
 7        domainPackageNames = {"net.akaish.kittyormdemo.sqlite.migrations.migv2"}
 8)
 9@KITTY_DATABASE_REGISTRY(
10        domainModels = {
11                net.akaish.kittyormdemo.sqlite.migrations.migv2.MigOneModel.class,
12                net.akaish.kittyormdemo.sqlite.migrations.migv2.MigTwoModel.class
13        }
14)
15@KITTY_DATABASE_HELPER(
16        onUpgradeBehavior = KITTY_DATABASE_HELPER.UpgradeBehavior.DROP_AND_CREATE
17)
18public class MigrationDBv2 extends KittyDatabase {
19    ...
20}

That’s all, simple as a pie.

Mig v.2

MigOneModel (mig_one)

Java type Name SQLite name Constraints
Long id id PRIMARY KEY
String creationDate creation_date NOT_NULL DEFAULT(CURRENT_DATE)
Integer defaultInteger default_integer DEFAULT(228)
Timestamp currentTimestamp current_timestamp -

Index on default_integer.

MigTwoModel (mig_two)

Java type Name SQLite name Constraints
Long id id PRIMARY KEY
Long migOneReference mig_one_reference FOREIGN KEY reference on mig_one.id
Animals someAnimal some_animal -

Index on some_animal.

v.1 -> v.2 diffs

  1. + column mig_one.current_timestamp
  2. + column mig_one.default_integer DEFAULT(28)
  3. - column mig_one.some_integer
  4. + constraint on mig_one.cretaion_date: DEFAULT(CURRENT_DATE)
  5. + table mig_two
  6. + index on mig_one (default_integer)
  7. + index on mig_two (some_animal)

Create schema script generated by KittyORM for database mig version 2

CREATE TABLE IF NOT EXISTS mig_one (id INTEGER NOT NULL PRIMARY KEY ASC, creation_date TEXT NOT NULL DEFAULT  CURRENT_DATE , default_integer INTEGER DEFAULT 228, current_timestamp INTEGER);

CREATE INDEX IF NOT EXISTS m1_di_index ON mig_one (default_integer);

CREATE TABLE IF NOT EXISTS mig_two (id INTEGER NOT NULL PRIMARY KEY ASC, mig_one_reference INTEGER REFERENCES mig_one (id) ON UPDATE CASCADE ON DELETE CASCADE, some_animal TEXT);

CREATE INDEX IF NOT EXISTS m2_sa_index ON mig_two (some_animal);
Drop schema script generated by KittyORM for database mig version 2
DROP TABLE IF EXISTS mig_one;

DROP TABLE IF EXISTS mig_two;
Migration script generated by KittyORM for database mig from version 1 to version 2 (DC migrator)
DROP TABLE IF EXISTS mig_one;

DROP TABLE IF EXISTS mig_two;

CREATE TABLE IF NOT EXISTS mig_one (id INTEGER NOT NULL PRIMARY KEY ASC, creation_date TEXT NOT NULL DEFAULT  CURRENT_DATE , default_integer INTEGER DEFAULT 228, current_timestamp INTEGER);

CREATE INDEX IF NOT EXISTS m1_di_index ON mig_one (default_integer);

CREATE TABLE IF NOT EXISTS mig_two (id INTEGER NOT NULL PRIMARY KEY ASC, mig_one_reference INTEGER REFERENCES mig_one (id) ON UPDATE CASCADE ON DELETE CASCADE, some_animal TEXT);

CREATE INDEX IF NOT EXISTS m2_sa_index ON mig_two (some_animal);