@Override public synchronized void onCreate( @NonNull SQLiteDatabase db, @NonNull TableDefaultsCustomizer customizer) { super.onCreate(db, customizer); final String sql = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_PARENT + " TEXT REFERENCES " + TripsTable.COLUMN_NAME + " ON DELETE CASCADE," + COLUMN_DISTANCE + " DECIMAL(10, 2) DEFAULT 0.00," + COLUMN_LOCATION + " TEXT," + COLUMN_DATE + " DATE," + COLUMN_TIMEZONE + " TEXT," + COLUMN_COMMENT + " TEXT," + COLUMN_RATE_CURRENCY + " TEXT NOT NULL, " + COLUMN_RATE + " DECIMAL(10, 2) DEFAULT 0.00, " + AbstractSqlTable.COLUMN_DRIVE_SYNC_ID + " TEXT, " + AbstractSqlTable.COLUMN_DRIVE_IS_SYNCED + " BOOLEAN DEFAULT 0, " + AbstractSqlTable.COLUMN_DRIVE_MARKED_FOR_DELETION + " BOOLEAN DEFAULT 0, " + AbstractSqlTable.COLUMN_LAST_LOCAL_MODIFICATION_TIME + " DATE " + ");"; Logger.debug(this, sql); db.execSQL(sql); }
@Override public synchronized void onUpgrade( @NonNull SQLiteDatabase db, int oldVersion, int newVersion, @NonNull TableDefaultsCustomizer customizer) { super.onUpgrade(db, oldVersion, newVersion, customizer); if (oldVersion <= 12) { final String createSqlV12 = "CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_PARENT + " TEXT REFERENCES " + TripsTable.COLUMN_NAME + " ON DELETE CASCADE," + COLUMN_DISTANCE + " DECIMAL(10, 2) DEFAULT 0.00," + COLUMN_LOCATION + " TEXT," + COLUMN_DATE + " DATE," + COLUMN_TIMEZONE + " TEXT," + COLUMN_COMMENT + " TEXT," + COLUMN_RATE_CURRENCY + " TEXT NOT NULL, " + COLUMN_RATE + " DECIMAL(10, 2) DEFAULT 0.00" + ");"; Logger.debug(this, createSqlV12); db.execSQL(createSqlV12); // Once we create the table, we need to move our "trips" mileage into a single item in the // distance table final String distanceMigrateBase = "INSERT INTO " + DistanceTable.TABLE_NAME + "(" + DistanceTable.COLUMN_PARENT + ", " + DistanceTable.COLUMN_DISTANCE + ", " + DistanceTable.COLUMN_LOCATION + ", " + DistanceTable.COLUMN_DATE + ", " + DistanceTable.COLUMN_TIMEZONE + ", " + DistanceTable.COLUMN_COMMENT + ", " + DistanceTable.COLUMN_RATE_CURRENCY + ")" + " SELECT " + TripsTable.COLUMN_NAME + ", " + TripsTable.COLUMN_MILEAGE + " , \"\" as " + DistanceTable.COLUMN_LOCATION + ", " + TripsTable.COLUMN_FROM + ", " + TripsTable.COLUMN_FROM_TIMEZONE + " , \"\" as " + DistanceTable.COLUMN_COMMENT + ", "; final String distanceMigrateNotNullCurrency = distanceMigrateBase + TripsTable.COLUMN_DEFAULT_CURRENCY + " FROM " + TripsTable.TABLE_NAME + " WHERE " + TripsTable.COLUMN_DEFAULT_CURRENCY + " IS NOT NULL AND " + TripsTable.COLUMN_MILEAGE + " > 0;"; final String distanceMigrateNullCurrency = distanceMigrateBase + "\"" + mDefaultCurrencyCode + "\" as " + DistanceTable.COLUMN_RATE_CURRENCY + " FROM " + TripsTable.TABLE_NAME + " WHERE " + TripsTable.COLUMN_DEFAULT_CURRENCY + " IS NULL AND " + TripsTable.COLUMN_MILEAGE + " > 0;"; Logger.debug(this, distanceMigrateNotNullCurrency); Logger.debug(this, distanceMigrateNullCurrency); db.execSQL(distanceMigrateNotNullCurrency); db.execSQL(distanceMigrateNullCurrency); } if (oldVersion <= 14) { onUpgradeToAddSyncInformation(db, oldVersion, newVersion); } }