In my Android application; I have a single database with multiple tables.
Each table is more-or-less separate from each other, but figured (for best practice?) to just have one DB file.
When it comes to Upgrades, it’s currently an all or nothing affair. On upgrade, it “DROP”‘s all the tables and re-creates them. However, this is rather harsh if only one of the tables has changed as all the other tables’ data is also lost.
Is there a built-in way to auto-upgrade just the tables that have changed?
(e.g. using a version number per/table?)
If not, I guess I can see two options:
-
Use separate databases/files for each table, to use built-in version upgrade functionality.
-
Use the database version number to know when the “schema” has changed, but have a separate table to store the current TABLE_VERSIONS and manage my own upgrade by checking the version number of each table against the current build and DROP/CREATE Tables where needed.
(I’d rather not re-invent the wheel here, so I’m hoping I’m missing something simple…)
You need an abstract class that implements the upgrade process described here. Then you extend this abstract class for each of your tables. In your abstract class you must store you tables in a way(list, hardcoded) so when the onUpgrade fires you iterate over the table items and for each table item you do the described steps. They will be self upgraded, keeping all their existing details. Please note that the onUpgrade event fires only once per database, that’s why you need to iterate over all your tables to do the upgrade of all of them. You maintain only 1 version number over all the database.
if not exists(we are doing an upgrade, so the table might not exists yet, it will fail alter and drop)List<String> columns = DBUtils.GetColumns(db, TableName);ALTER table " + TableName + " RENAME TO 'temp_" + TableName)columns.retainAll(DBUtils.GetColumns(db, TableName));)String cols = StringUtils.join(columns, ",");
)db.execSQL(String.format(
"INSERT INTO %s (%s) SELECT %s from temp_%s",
TableName, cols, cols, TableName));
DROP table 'temp_" + TableName)(This doesn’t handle table downgrade, if you rename a column, you don’t get the existing data transfered as the column names do not match).
.