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; }
@Test public void onUpgradeFromV2() { final int oldVersion = 2; 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); assertTrue(mSqlCaptor.getAllValues().get(0).equals("ALTER TABLE categories ADD breakdown BOOLEAN DEFAULT 1")); assertEquals(mSqlCaptor.getAllValues().get(1), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD drive_sync_id TEXT"); assertEquals(mSqlCaptor.getAllValues().get(2), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD drive_is_synced BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(3), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD drive_marked_for_deletion BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(4), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD last_local_modification_time DATE"); }
@Test public void onUpgradeFromV14() { final int oldVersion = 14; 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); assertEquals(mSqlCaptor.getAllValues().get(0), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD drive_sync_id TEXT"); assertEquals(mSqlCaptor.getAllValues().get(1), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD drive_is_synced BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(2), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD drive_marked_for_deletion BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(3), "ALTER TABLE " + mCategoriesTable.getTableName() + " ADD last_local_modification_time DATE"); }
@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"); }
@Test public void onUpgradeFromV18() { final int oldVersion = 18; 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(1, allValues.size()); assertEquals(allValues.get(0), String.format("ALTER TABLE %s ADD entity_uuid TEXT", CategoriesTable.TABLE_NAME)); }
@Test public void onUpgradeAlreadyOccurred() { final int oldVersion = DatabaseHelper.DATABASE_VERSION; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mCategoriesTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, never()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertCategoryDefaults(mCategoriesTable); }