So, I am dealing with an Android SQLite database, with SQLiteOpenHelper. It appears to possess a idea of database versions and improving... however it appears like you are designed to roll your personal code to really perform the upgrades, and your onCreate method up-to-date together with your updates.

From a Rails development background, this appears just a little primitive. For that uninitiated, Rails enables you to definitely just write a category-per-version-upgrade, and Rails takes proper care of using whichever versions have to be... this is applicable for DB creation, too you simply have one representation of the database schema, the group of migrations. Rails also does a DB-independent representation from the schema changes, but that is not essential for Android since it only supports SQLite (that is fine).

Has anybody written a good schema migration assistant class for Android that enables me to obtain nearer to DB schema management nirvana (RailsEdition(TM))? It'll save me moving my very own ugly implementation.

Considering that I did not find something that supported Android, really labored, did not require me a subscription for an insane database world view, and did not be expensive (hobby project, no dice), I emerged using the following bodgy hack. It isn't clever, however it a minimum of enables me to consider my schemas in ways that I am acquainted with. I do not expect it'd work real well for any large codebase/database schema, but when you have that you could most likely afford to cover something.

public class AppDatabase extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "main";
    public static final int LATEST_VERSION = 4;

    public static SQLiteDatabase open(Context ctx) {
        AppDatabase db = new AppDatabase(ctx);
        return db.getWritableDatabase();
    }

    public AppDatabase(Context ctx) {
        super(ctx, DATABASE_NAME, null, LATEST_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        onUpgrade(db, 0, LATEST_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        for (int i = oldVersion+1; i <= newVersion; i++) {
            switch (i) {
            case 1:
                db.execSQL("CREATE TABLE blah ( " +
                        "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
                        "start CHAR(4)," +
                        "end CHAR(4)" +
                        ")");
                break;
            case 2:
                db.execSQL("CREATE TABLE fortnights ( " +
                        "first_day DATE PRIMARY KEY" +
                        ")");
                break;
            case 3:
                db.execSQL("ALTER TABLE shifts ADD top CHAR(4)");
                db.execSQL("ALTER TABLE shifts ADD bottom CHAR(4)");
                db.execSQL("UPDATE shifts set top=start, bottom=end");
                break;
            case 4:
                db.execSQL("ALTER TABLE shifts ADD callout BOOLEAN DEFAULT 0");
                break;
            }
        }
    }
}