PaymentMethodsTable extends AbstractSqlTable<PaymentMethod> { @Override public synchronized void onUpgrade(@NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer) { super.onUpgrade(db, oldVersion, newVersion, customizer); if (oldVersion <= 11) { final String sql = "CREATE TABLE " + getTableName() + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_METHOD + " TEXT" + ");"; Logger.debug(this, sql); db.execSQL(sql); customizer.insertPaymentMethodDefaults(this); } if (oldVersion <= 14) { onUpgradeToAddSyncInformation(db, oldVersion, newVersion); } if (oldVersion <= 15) { final String addCustomOrderColumn = String.format("ALTER TABLE %s ADD COLUMN %s INTEGER DEFAULT 0", getTableName(), AbstractColumnTable.COLUMN_CUSTOM_ORDER_ID); Logger.debug(this, addCustomOrderColumn); db.execSQL(addCustomOrderColumn); } if (oldVersion <= 16) { if (!hasCustomOrderIdColumn(db)) { final String addCustomOrderColumn = String.format("ALTER TABLE %s ADD COLUMN %s INTEGER DEFAULT 0", getTableName(), AbstractColumnTable.COLUMN_CUSTOM_ORDER_ID); final String updateDefaultCustomOrder = String.format("UPDATE %s SET %s = ROWID", getTableName(), AbstractColumnTable.COLUMN_CUSTOM_ORDER_ID); Logger.debug(this, addCustomOrderColumn); Logger.debug(this, updateDefaultCustomOrder); db.execSQL(addCustomOrderColumn); db.execSQL(updateDefaultCustomOrder); } } if (oldVersion <= 18) { onUpgradeToAddUUID(db, oldVersion); } if (oldVersion <= 21) { final String alterPaymentMethods = "ALTER TABLE " + getTableName() + " ADD COLUMN " + COLUMN_REIMBURSABLE + " BOOLEAN DEFAULT 0"; Logger.debug(this, alterPaymentMethods); db.execSQL(alterPaymentMethods); } } PaymentMethodsTable(@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_METHOD; static final String COLUMN_REIMBURSABLE; }
@Test public void onUpgradeFromV11() { final int oldVersion = 11; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer).insertPaymentMethodDefaults(mPaymentMethodsTable); assertTrue(mSqlCaptor.getAllValues().get(0).contains(PaymentMethodsTable.TABLE_NAME)); assertTrue(mSqlCaptor.getAllValues().get(0).contains(PaymentMethodsTable.COLUMN_ID)); assertTrue(mSqlCaptor.getAllValues().get(0).contains(PaymentMethodsTable.COLUMN_METHOD)); assertEquals(mSqlCaptor.getAllValues().get(0), "CREATE TABLE paymentmethods (id INTEGER PRIMARY KEY AUTOINCREMENT, method TEXT);"); assertEquals(mSqlCaptor.getAllValues().get(1), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD drive_sync_id TEXT"); assertEquals(mSqlCaptor.getAllValues().get(2), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD drive_is_synced BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(3), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD drive_marked_for_deletion BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(4), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD last_local_modification_time DATE"); assertEquals(mSqlCaptor.getAllValues().get(5), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD COLUMN custom_order_id INTEGER DEFAULT 0"); }
@Test public void onUpgradeFromV14() { final int oldVersion = 14; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertPaymentMethodDefaults(mPaymentMethodsTable); assertEquals(mSqlCaptor.getAllValues().get(0), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD drive_sync_id TEXT"); assertEquals(mSqlCaptor.getAllValues().get(1), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD drive_is_synced BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(2), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD drive_marked_for_deletion BOOLEAN DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(3), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD last_local_modification_time DATE"); assertEquals(mSqlCaptor.getAllValues().get(4), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD COLUMN custom_order_id INTEGER DEFAULT 0"); }
@Test public void onUpgradeFromV15() { final int oldVersion = 15; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertPaymentMethodDefaults(mPaymentMethodsTable); assertEquals(mSqlCaptor.getAllValues().get(0), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD COLUMN custom_order_id INTEGER DEFAULT 0"); }
@Test public void onUpgradeFromV16WhenCustomOrderIdColumnIsPresent() { final int oldVersion = 16; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); final Cursor cursor = mock(Cursor.class); final String pragmaTableInfo = "PRAGMA table_info(" + mPaymentMethodsTable.getTableName() + ")"; final int columnNameIndex = 0; when(mSQLiteDatabase.rawQuery(pragmaTableInfo, null)).thenReturn(cursor); when(cursor.moveToFirst()).thenReturn(true); when(cursor.getColumnIndex("name")).thenReturn(columnNameIndex); when(cursor.getString(columnNameIndex)).thenReturn("id", "method", "drive_sync_id", "drive_is_synced", "drive_marked_for_deletion", "last_local_modification_time", "custom_order_id"); when(cursor.moveToNext()).thenReturn(true, true, true, true, true, true, false); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertPaymentMethodDefaults(mPaymentMethodsTable); }
@Test public void onUpgradeFromV16WhenCustomOrderIdColumnIsMissing() { final int oldVersion = 16; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); final Cursor cursor = mock(Cursor.class); final String pragmaTableInfo = "PRAGMA table_info(" + mPaymentMethodsTable.getTableName() + ")"; final int columnNameIndex = 0; when(mSQLiteDatabase.rawQuery(pragmaTableInfo, null)).thenReturn(cursor); when(cursor.moveToFirst()).thenReturn(true); when(cursor.getColumnIndex("name")).thenReturn(columnNameIndex); when(cursor.getString(columnNameIndex)).thenReturn("id", "method", "drive_sync_id", "drive_is_synced", "drive_marked_for_deletion", "last_local_modification_time"); when(cursor.moveToNext()).thenReturn(true, true, true, true, true, false); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertPaymentMethodDefaults(mPaymentMethodsTable); assertEquals(mSqlCaptor.getAllValues().get(0), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD COLUMN custom_order_id INTEGER DEFAULT 0"); assertEquals(mSqlCaptor.getAllValues().get(1), "UPDATE " + mPaymentMethodsTable.getTableName() + " SET custom_order_id = ROWID"); }
@Test public void onUpgradeFromV18() { final int oldVersion = 18; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertPaymentMethodDefaults(mPaymentMethodsTable); final List<String> allValues = mSqlCaptor.getAllValues(); assertEquals(2, allValues.size()); assertEquals(allValues.get(0), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD entity_uuid TEXT"); }
@Test public void onUpgradeFromV19() { final int oldVersion = 19; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeastOnce()).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertPaymentMethodDefaults(mPaymentMethodsTable); final List<String> allValues = mSqlCaptor.getAllValues(); assertEquals(1, allValues.size()); assertEquals(allValues.get(0), "ALTER TABLE " + mPaymentMethodsTable.getTableName() + " ADD COLUMN expenseable BOOLEAN DEFAULT 0"); }
@Test public void onUpgradeAlreadyOccurred() { final int oldVersion = DatabaseHelper.DATABASE_VERSION; final int newVersion = DatabaseHelper.DATABASE_VERSION; final TableDefaultsCustomizer customizer = mock(TableDefaultsCustomizer.class); mPaymentMethodsTable.onUpgrade(mSQLiteDatabase, oldVersion, newVersion, customizer); verify(mSQLiteDatabase, atLeast(0)).execSQL(mSqlCaptor.capture()); verify(customizer, never()).insertPaymentMethodDefaults(mPaymentMethodsTable); }