1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-25 18:15:49 +01:00

Only add column if it doesn't exist yet

Column can exist if there down- and upgrades
This commit is contained in:
cpfeiffer 2016-03-04 23:19:44 +01:00
parent 3b3458e196
commit dc162a9ac8
2 changed files with 14 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package nodomain.freeyourgadget.gadgetbridge.database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@ -70,6 +71,13 @@ public class DBHelper {
db.execSQL(statement);
}
public static boolean existsColumn(String tableName, String columnName, SQLiteDatabase db) {
try (Cursor res = db.rawQuery("PRAGMA table_info('" + tableName + "')", null)) {
int result = res.getColumnIndex(columnName);
return result != -1;
}
}
/**
* WITHOUT ROWID is only available with sqlite 3.8.2, which is available
* with Lollipop and later.

View File

@ -2,6 +2,7 @@ package nodomain.freeyourgadget.gadgetbridge.database.schema;
import android.database.sqlite.SQLiteDatabase;
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
import nodomain.freeyourgadget.gadgetbridge.database.DBUpdateScript;
import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.KEY_CUSTOM_SHORT;
@ -13,10 +14,12 @@ import static nodomain.freeyourgadget.gadgetbridge.database.DBConstants.TABLE_GB
public class ActivityDBUpdate_6 implements DBUpdateScript {
@Override
public void upgradeSchema(SQLiteDatabase db) {
if (!DBHelper.existsColumn(TABLE_GBACTIVITYSAMPLES, KEY_CUSTOM_SHORT, db)) {
String ADD_COLUMN_CUSTOM_SHORT = "ALTER TABLE " + TABLE_GBACTIVITYSAMPLES + " ADD COLUMN "
+ KEY_CUSTOM_SHORT + " INT;";
db.execSQL(ADD_COLUMN_CUSTOM_SHORT);
}
}
@Override
public void downgradeSchema(SQLiteDatabase db) {