Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ $> droid-migrate generate down

You will then need to provide the relevant SQL details in the newly generated `DBVersion<next_sequence>` 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:
Expand Down
4 changes: 4 additions & 0 deletions src/com/b50/migrations/MigrationsDatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 14 additions & 3 deletions src/com/b50/migrations/Migrator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
}
}
}

Expand All @@ -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);
}
}
}
}
Expand All @@ -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);
}
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion test/test/com/b50/migrations/MigratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down