Android rails version updates for similar rails for Android

So, I am working with an Android SQLite database, SQLiteOpenHelper . It looks like you have the concept of database versions and updates ... but it looks like you have to roll your own code in order to actually perform the updates, and update your onCreate method with your updates.

Based on the background of Rails development, this seems a bit primitive. For the uninitiated, Rails allows you to simply write an update for each version, and Rails will take care of applying any versions that should be ... this applies to creating a database; you only have one view of your database schema, a set of migrations. Rails also makes a DB-independent representation of schema changes, but this is not necessary for Android because it only supports SQLite (this is normal).

Has anyone written a suitable schema migration migration class for Android that allows me to get closer to managing the DB nails (RailsEdition (TM))? It will save me from my own ugly implementation.

+4
source share
1 answer

Given that I did not find anything that supported Android, it actually worked, didn’t require me to sign up for a crazy world view of the database and not cost a lot (hobby project, no blocks), I came up with the following hack. This is not smart, but at least it allows me to think about my circuits the way I know. I do not expect this to work well for a large code / database scheme, but if you have this, you can probably afford to pay for 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; } } } } 
+3
source

Source: https://habr.com/ru/post/1339503/


All Articles