@Test public void onUpgradeFromV15() { final int oldVersion = 15; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mCategoriesTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertCategoryDefaults(mCategoriesTable); List<String> allValues = mSqlCaptor.getAllValues(); assertEquals(5, allValues.size()); assertEquals(allValues.get(0), "CREATE TABLE " + CategoriesTable.TABLE_NAME + "_copy" + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " + COLUMN_CODE + " TEXT, " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1, " + COLUMN_DRIVE_SYNC_ID + " TEXT, " + COLUMN_DRIVE_IS_SYNCED + " BOOLEAN DEFAULT 0, " + COLUMN_DRIVE_MARKED_FOR_DELETION + " BOOLEAN DEFAULT 0, " + COLUMN_LAST_LOCAL_MODIFICATION_TIME + " DATE, " + COLUMN_CUSTOM_ORDER_ID + " INTEGER DEFAULT 0" + ");"); final String baseColumns = TextUtils.join(", ", new String[]{ COLUMN_NAME, COLUMN_CODE, COLUMN_BREAKDOWN, COLUMN_DRIVE_SYNC_ID, COLUMN_DRIVE_IS_SYNCED, COLUMN_DRIVE_MARKED_FOR_DELETION, COLUMN_LAST_LOCAL_MODIFICATION_TIME}); assertEquals(allValues.get(1), "INSERT INTO " + CategoriesTable.TABLE_NAME + "_copy (" + baseColumns + ") " + "SELECT " + baseColumns + " FROM " + CategoriesTable.TABLE_NAME + ";"); assertEquals(allValues.get(2), "DROP TABLE " + CategoriesTable.TABLE_NAME + ";"); assertEquals(allValues.get(3), "ALTER TABLE " + CategoriesTable.TABLE_NAME + "_copy " + "RENAME TO " + CategoriesTable.TABLE_NAME + ";"); assertEquals(allValues.get(4), "ALTER TABLE " + CategoriesTable.TABLE_NAME + " ADD " + AbstractSqlTable.COLUMN_UUID + " TEXT"); }
@Override public synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer) { super.onUpgrade(db, oldVersion, newVersion, customizer); if (oldVersion <= 2) { final String alterCategories = "ALTER TABLE " + getTableName() + " ADD " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1"; db.execSQL(alterCategories); } if (oldVersion <= 14) { onUpgradeToAddSyncInformation(db, oldVersion, newVersion); } if (oldVersion <= 15) { final String copyTable = "CREATE TABLE " + getTableName() + "_copy" + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " + COLUMN_CODE + " TEXT, " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1, " + COLUMN_DRIVE_SYNC_ID + " TEXT, " + COLUMN_DRIVE_IS_SYNCED + " BOOLEAN DEFAULT 0, " + COLUMN_DRIVE_MARKED_FOR_DELETION + " BOOLEAN DEFAULT 0, " + COLUMN_LAST_LOCAL_MODIFICATION_TIME + " DATE, " + COLUMN_CUSTOM_ORDER_ID + " INTEGER DEFAULT 0" + ");"; Logger.debug(this, copyTable); db.execSQL(copyTable); final String baseColumns = TextUtils.join(", ", new String[]{ COLUMN_NAME, COLUMN_CODE, COLUMN_BREAKDOWN, COLUMN_DRIVE_SYNC_ID, COLUMN_DRIVE_IS_SYNCED, COLUMN_DRIVE_MARKED_FOR_DELETION, COLUMN_LAST_LOCAL_MODIFICATION_TIME}); final String insertData = "INSERT INTO " + getTableName() + "_copy" + " (" + baseColumns + ") " + "SELECT " + baseColumns + " FROM " + getTableName() + ";"; Logger.debug(this, insertData); db.execSQL(insertData); final String dropOldTable = "DROP TABLE " + getTableName() + ";"; Logger.debug(this, dropOldTable); db.execSQL(dropOldTable); final String renameTable = "ALTER TABLE " + getTableName() + "_copy" + " RENAME TO " + getTableName() + ";"; Logger.debug(this, renameTable); db.execSQL(renameTable); } if (oldVersion <= 18) { onUpgradeToAddUUID(db, oldVersion); } }
CategoriesTable extends AbstractSqlTable<Category> { @Override public synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer) { super.onUpgrade(db, oldVersion, newVersion, customizer); if (oldVersion <= 2) { final String alterCategories = "ALTER TABLE " + getTableName() + " ADD " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1"; db.execSQL(alterCategories); } if (oldVersion <= 14) { onUpgradeToAddSyncInformation(db, oldVersion, newVersion); } if (oldVersion <= 15) { final String copyTable = "CREATE TABLE " + getTableName() + "_copy" + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " + COLUMN_CODE + " TEXT, " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1, " + COLUMN_DRIVE_SYNC_ID + " TEXT, " + COLUMN_DRIVE_IS_SYNCED + " BOOLEAN DEFAULT 0, " + COLUMN_DRIVE_MARKED_FOR_DELETION + " BOOLEAN DEFAULT 0, " + COLUMN_LAST_LOCAL_MODIFICATION_TIME + " DATE, " + COLUMN_CUSTOM_ORDER_ID + " INTEGER DEFAULT 0" + ");"; Logger.debug(this, copyTable); db.execSQL(copyTable); final String baseColumns = TextUtils.join(", ", new String[]{ COLUMN_NAME, COLUMN_CODE, COLUMN_BREAKDOWN, COLUMN_DRIVE_SYNC_ID, COLUMN_DRIVE_IS_SYNCED, COLUMN_DRIVE_MARKED_FOR_DELETION, COLUMN_LAST_LOCAL_MODIFICATION_TIME}); final String insertData = "INSERT INTO " + getTableName() + "_copy" + " (" + baseColumns + ") " + "SELECT " + baseColumns + " FROM " + getTableName() + ";"; Logger.debug(this, insertData); db.execSQL(insertData); final String dropOldTable = "DROP TABLE " + getTableName() + ";"; Logger.debug(this, dropOldTable); db.execSQL(dropOldTable); final String renameTable = "ALTER TABLE " + getTableName() + "_copy" + " RENAME TO " + getTableName() + ";"; Logger.debug(this, renameTable); db.execSQL(renameTable); } if (oldVersion <= 18) { onUpgradeToAddUUID(db, oldVersion); } } }
CategoriesTable extends AbstractSqlTable<Category> { @Override public synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer) { super.onUpgrade(db, oldVersion, newVersion, customizer); if (oldVersion <= 2) { final String alterCategories = "ALTER TABLE " + getTableName() + " ADD " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1"; db.execSQL(alterCategories); } if (oldVersion <= 14) { onUpgradeToAddSyncInformation(db, oldVersion, newVersion); } if (oldVersion <= 15) { final String copyTable = "CREATE TABLE " + getTableName() + "_copy" + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " + COLUMN_CODE + " TEXT, " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1, " + COLUMN_DRIVE_SYNC_ID + " TEXT, " + COLUMN_DRIVE_IS_SYNCED + " BOOLEAN DEFAULT 0, " + COLUMN_DRIVE_MARKED_FOR_DELETION + " BOOLEAN DEFAULT 0, " + COLUMN_LAST_LOCAL_MODIFICATION_TIME + " DATE, " + COLUMN_CUSTOM_ORDER_ID + " INTEGER DEFAULT 0" + ");"; Logger.debug(this, copyTable); db.execSQL(copyTable); final String baseColumns = TextUtils.join(", ", new String[]{ COLUMN_NAME, COLUMN_CODE, COLUMN_BREAKDOWN, COLUMN_DRIVE_SYNC_ID, COLUMN_DRIVE_IS_SYNCED, COLUMN_DRIVE_MARKED_FOR_DELETION, COLUMN_LAST_LOCAL_MODIFICATION_TIME}); final String insertData = "INSERT INTO " + getTableName() + "_copy" + " (" + baseColumns + ") " + "SELECT " + baseColumns + " FROM " + getTableName() + ";"; Logger.debug(this, insertData); db.execSQL(insertData); final String dropOldTable = "DROP TABLE " + getTableName() + ";"; Logger.debug(this, dropOldTable); db.execSQL(dropOldTable); final String renameTable = "ALTER TABLE " + getTableName() + "_copy" + " RENAME TO " + getTableName() + ";"; Logger.debug(this, renameTable); db.execSQL(renameTable); } if (oldVersion <= 18) { onUpgradeToAddUUID(db, oldVersion); } } CategoriesTable(@NonNull SQLiteOpenHelper sqLiteOpenHelper,
                           @NonNull OrderingPreferencesManager orderingPreferencesManager); }
CategoriesTable extends AbstractSqlTable<Category> { @Override public synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer) { super.onUpgrade(db, oldVersion, newVersion, customizer); if (oldVersion <= 2) { final String alterCategories = "ALTER TABLE " + getTableName() + " ADD " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1"; db.execSQL(alterCategories); } if (oldVersion <= 14) { onUpgradeToAddSyncInformation(db, oldVersion, newVersion); } if (oldVersion <= 15) { final String copyTable = "CREATE TABLE " + getTableName() + "_copy" + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " + COLUMN_CODE + " TEXT, " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1, " + COLUMN_DRIVE_SYNC_ID + " TEXT, " + COLUMN_DRIVE_IS_SYNCED + " BOOLEAN DEFAULT 0, " + COLUMN_DRIVE_MARKED_FOR_DELETION + " BOOLEAN DEFAULT 0, " + COLUMN_LAST_LOCAL_MODIFICATION_TIME + " DATE, " + COLUMN_CUSTOM_ORDER_ID + " INTEGER DEFAULT 0" + ");"; Logger.debug(this, copyTable); db.execSQL(copyTable); final String baseColumns = TextUtils.join(", ", new String[]{ COLUMN_NAME, COLUMN_CODE, COLUMN_BREAKDOWN, COLUMN_DRIVE_SYNC_ID, COLUMN_DRIVE_IS_SYNCED, COLUMN_DRIVE_MARKED_FOR_DELETION, COLUMN_LAST_LOCAL_MODIFICATION_TIME}); final String insertData = "INSERT INTO " + getTableName() + "_copy" + " (" + baseColumns + ") " + "SELECT " + baseColumns + " FROM " + getTableName() + ";"; Logger.debug(this, insertData); db.execSQL(insertData); final String dropOldTable = "DROP TABLE " + getTableName() + ";"; Logger.debug(this, dropOldTable); db.execSQL(dropOldTable); final String renameTable = "ALTER TABLE " + getTableName() + "_copy" + " RENAME TO " + getTableName() + ";"; Logger.debug(this, renameTable); db.execSQL(renameTable); } if (oldVersion <= 18) { onUpgradeToAddUUID(db, oldVersion); } } CategoriesTable(@NonNull SQLiteOpenHelper sqLiteOpenHelper,
                           @NonNull OrderingPreferencesManager orderingPreferencesManager); @Override synchronized void onCreate(@NonNull SQLiteDatabase db, @NonNull TableDefaultsCustomizer customizer); @Override synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer); }
CategoriesTable extends AbstractSqlTable<Category> { @Override public synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer) { super.onUpgrade(db, oldVersion, newVersion, customizer); if (oldVersion <= 2) { final String alterCategories = "ALTER TABLE " + getTableName() + " ADD " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1"; db.execSQL(alterCategories); } if (oldVersion <= 14) { onUpgradeToAddSyncInformation(db, oldVersion, newVersion); } if (oldVersion <= 15) { final String copyTable = "CREATE TABLE " + getTableName() + "_copy" + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME + " TEXT, " + COLUMN_CODE + " TEXT, " + COLUMN_BREAKDOWN + " BOOLEAN DEFAULT 1, " + COLUMN_DRIVE_SYNC_ID + " TEXT, " + COLUMN_DRIVE_IS_SYNCED + " BOOLEAN DEFAULT 0, " + COLUMN_DRIVE_MARKED_FOR_DELETION + " BOOLEAN DEFAULT 0, " + COLUMN_LAST_LOCAL_MODIFICATION_TIME + " DATE, " + COLUMN_CUSTOM_ORDER_ID + " INTEGER DEFAULT 0" + ");"; Logger.debug(this, copyTable); db.execSQL(copyTable); final String baseColumns = TextUtils.join(", ", new String[]{ COLUMN_NAME, COLUMN_CODE, COLUMN_BREAKDOWN, COLUMN_DRIVE_SYNC_ID, COLUMN_DRIVE_IS_SYNCED, COLUMN_DRIVE_MARKED_FOR_DELETION, COLUMN_LAST_LOCAL_MODIFICATION_TIME}); final String insertData = "INSERT INTO " + getTableName() + "_copy" + " (" + baseColumns + ") " + "SELECT " + baseColumns + " FROM " + getTableName() + ";"; Logger.debug(this, insertData); db.execSQL(insertData); final String dropOldTable = "DROP TABLE " + getTableName() + ";"; Logger.debug(this, dropOldTable); db.execSQL(dropOldTable); final String renameTable = "ALTER TABLE " + getTableName() + "_copy" + " RENAME TO " + getTableName() + ";"; Logger.debug(this, renameTable); db.execSQL(renameTable); } if (oldVersion <= 18) { onUpgradeToAddUUID(db, oldVersion); } } CategoriesTable(@NonNull SQLiteOpenHelper sqLiteOpenHelper,
                           @NonNull OrderingPreferencesManager orderingPreferencesManager); @Override synchronized void onCreate(@NonNull SQLiteDatabase db, @NonNull TableDefaultsCustomizer customizer); @Override synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer); static final String TABLE_NAME; static final String COLUMN_NAME; static final String COLUMN_CODE; static final String COLUMN_BREAKDOWN; }