diff --git a/README.md b/README.md index ab86cd2..058fd00 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,22 @@ $> droid-migrate generate down You will then need to provide the relevant SQL details in the newly generated `DBVersion` class (just like you did for the initial class, dubbed `DBVersion1`). +#Working with legacy apps + +If you are retrospectively applying migrations to a legacy application that already has a version number greater than 1, Droid Migrate can handle this. + +Let's say your version number is already ```5``` in your legacy application. Follow the steps above to add Droid Migrate to your project first of all. Then: + +1. Adjust ```database_version``` in ```res/values/migrations.xml``` and change it to ```5``` +2. Rename the ```DBVersion1.java``` class to ```DBVersion5.java``` +3. Adjust your instantiation of the `DatabaseHelper` class as follows: + +``` +SQLiteDatabase db = (new DatabaseHelper(this).setAlertOnMissingMigrations(false)).getWritableDatabase(); +``` + +and your migrations will start from version 5 onwards. + #How it works Droid Migrate's secret sauce can be found in three files: diff --git a/src/com/b50/migrations/MigrationsDatabaseHelper.java b/src/com/b50/migrations/MigrationsDatabaseHelper.java index 932f3f0..e4165c2 100644 --- a/src/com/b50/migrations/MigrationsDatabaseHelper.java +++ b/src/com/b50/migrations/MigrationsDatabaseHelper.java @@ -17,6 +17,10 @@ public MigrationsDatabaseHelper(Context context, String dbName, SQLiteDatabase.C this.intendedVersion = dbVersion; } + public void setAlertOnMissingMigrations(boolean alertOnMissingMigrations) { + this.migrator.setAlertOnMissingMigrations(alertOnMissingMigrations); + } + public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { try { migrator.downgrade(db, oldVersion, newVersion); diff --git a/src/com/b50/migrations/Migrator.java b/src/com/b50/migrations/Migrator.java index 3bf35a6..1cd6ba9 100644 --- a/src/com/b50/migrations/Migrator.java +++ b/src/com/b50/migrations/Migrator.java @@ -4,11 +4,16 @@ public class Migrator { private String packageName; + private boolean alertOnMissingMigrations = true; public Migrator(String packageName) { this.packageName = packageName; } + public void setAlertOnMissingMigrations(boolean alertOnMissingMigrations) { + this.alertOnMissingMigrations = alertOnMissingMigrations; + } + public void initialMigration(SQLiteDatabase db) throws MigrationException { try { handleUp(db, packageName, "DBVersion1"); @@ -17,7 +22,9 @@ public void initialMigration(SQLiteDatabase db) throws MigrationException { } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (ClassNotFoundException e) { - throw new MigrationException(e); + if (alertOnMissingMigrations) { + throw new MigrationException(e); + } } } @@ -30,7 +37,9 @@ public void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) throws Mi } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (ClassNotFoundException e) { - throw new MigrationException(e); + if (alertOnMissingMigrations) { + throw new MigrationException(e); + } } } } @@ -44,7 +53,9 @@ public void downgrade(SQLiteDatabase db, int oldVersion, int newVersion) throws } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (ClassNotFoundException e) { - throw new MigrationException(e); + if (alertOnMissingMigrations) { + throw new MigrationException(e); + } } } } diff --git a/test/test/com/b50/migrations/MigratorTest.java b/test/test/com/b50/migrations/MigratorTest.java index 0c09f2d..2fd0f05 100644 --- a/test/test/com/b50/migrations/MigratorTest.java +++ b/test/test/com/b50/migrations/MigratorTest.java @@ -36,7 +36,25 @@ public void testDBLogicException() throws Exception { Migrator migrator = new Migrator("test.com.b50.migrations.nopackage"); migrator.upgrade(mockedDB, 1, 2); } - + + @Test + public void testDBCanIgnoreMissingInitialMigrationClass() throws Exception { + SQLiteDatabase mockedDB = mock(SQLiteDatabase.class); + Migrator migrator = new Migrator("test.com.b50.migrations"); + migrator.setAlertOnMissingMigrations(false); + migrator.initialMigration(mockedDB); + } + + @Test + public void testDBCanIgnoreMissingClasses() throws Exception { + SQLiteDatabase mockedDB = mock(SQLiteDatabase.class); + Migrator migrator = new Migrator("test.com.b50.migrations"); + migrator.setAlertOnMissingMigrations(false); + migrator.upgrade(mockedDB, 1, 4); // Migration version 4 does not exist + verify(mockedDB, times(1)).execSQL("some additional sql stmts from #2"); + verify(mockedDB, times(1)).execSQL("some additional sql stmts from #3"); + } + @Test public void testDBUpLogic() throws Exception { SQLiteDatabase mockedDB = mock(SQLiteDatabase.class);