Fixed upgrading DB triggers

pull/207/head
M66B 3 years ago
parent 63a65eed62
commit da4a5fd93f

@ -459,43 +459,33 @@ public abstract class DB extends RoomDatabase {
} }
private static void createTriggers(@NonNull SupportSQLiteDatabase db) { private static void createTriggers(@NonNull SupportSQLiteDatabase db) {
createTriggers(db, false); List<String> image = new ArrayList<>();
} for (String img : ImageHelper.IMAGE_TYPES)
image.add("'" + img + "'");
private static void createTriggers(@NonNull SupportSQLiteDatabase db, boolean fail) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
try { for (String img : ImageHelper.IMAGE_TYPES8)
List<String> image = new ArrayList<>();
for (String img : ImageHelper.IMAGE_TYPES)
image.add("'" + img + "'"); image.add("'" + img + "'");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
for (String img : ImageHelper.IMAGE_TYPES8) for (String img : ImageHelper.IMAGE_TYPES12)
image.add("'" + img + "'"); image.add("'" + img + "'");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) String images = TextUtils.join(",", image);
for (String img : ImageHelper.IMAGE_TYPES12)
image.add("'" + img + "'"); db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_insert" +
String images = TextUtils.join(",", image); " AFTER INSERT ON attachment" +
" BEGIN" +
db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_insert" + " UPDATE message SET attachments = attachments + 1" +
" AFTER INSERT ON attachment" + " WHERE message.id = NEW.message" +
" BEGIN" + " AND NEW.encryption IS NULL" +
" UPDATE message SET attachments = attachments + 1" + " AND NOT ((NEW.disposition = 'inline' OR (NEW.related IS NOT 0 AND NEW.cid IS NOT NULL)) AND NEW.type IN (" + images + "));" +
" WHERE message.id = NEW.message" + " END");
" AND NEW.encryption IS NULL" + db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_delete" +
" AND NOT ((NEW.disposition = 'inline' OR (NEW.related IS NOT 0 AND NEW.cid IS NOT NULL)) AND NEW.type IN (" + images + "));" + " AFTER DELETE ON attachment" +
" END"); " BEGIN" +
db.execSQL("CREATE TRIGGER IF NOT EXISTS attachment_delete" + " UPDATE message SET attachments = attachments - 1" +
" AFTER DELETE ON attachment" + " WHERE message.id = OLD.message" +
" BEGIN" + " AND OLD.encryption IS NULL" +
" UPDATE message SET attachments = attachments - 1" + " AND NOT ((OLD.disposition = 'inline' OR (OLD.related IS NOT 0 AND OLD.cid IS NOT NULL)) AND OLD.type IN (" + images + "));" +
" WHERE message.id = OLD.message" + " END");
" AND OLD.encryption IS NULL" +
" AND NOT ((OLD.disposition = 'inline' OR (OLD.related IS NOT 0 AND OLD.cid IS NOT NULL)) AND OLD.type IN (" + images + "));" +
" END");
} catch (Throwable ex) {
Log.w(ex);
if (fail)
throw ex;
}
} }
private static void logMigration(int startVersion, int endVersion) { private static void logMigration(int startVersion, int endVersion) {
@ -1608,7 +1598,7 @@ public abstract class DB extends RoomDatabase {
logMigration(startVersion, endVersion); logMigration(startVersion, endVersion);
db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db); //createTriggers(db);
} }
}) })
.addMigrations(new Migration(134, 135) { .addMigrations(new Migration(134, 135) {
@ -1797,9 +1787,9 @@ public abstract class DB extends RoomDatabase {
@Override @Override
public void migrate(@NonNull SupportSQLiteDatabase db) { public void migrate(@NonNull SupportSQLiteDatabase db) {
logMigration(startVersion, endVersion); logMigration(startVersion, endVersion);
db.execSQL("DROP TRIGGER attachment_insert"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER attachment_delete"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db); //createTriggers(db);
} }
}) })
.addMigrations(new Migration(160, 161) { .addMigrations(new Migration(160, 161) {
@ -1823,18 +1813,18 @@ public abstract class DB extends RoomDatabase {
@Override @Override
public void migrate(@NonNull SupportSQLiteDatabase db) { public void migrate(@NonNull SupportSQLiteDatabase db) {
logMigration(startVersion, endVersion); logMigration(startVersion, endVersion);
db.execSQL("DROP TRIGGER attachment_insert"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER attachment_delete"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db); //createTriggers(db);
} }
}) })
.addMigrations(new Migration(163, 164) { .addMigrations(new Migration(163, 164) {
@Override @Override
public void migrate(@NonNull SupportSQLiteDatabase db) { public void migrate(@NonNull SupportSQLiteDatabase db) {
logMigration(startVersion, endVersion); logMigration(startVersion, endVersion);
db.execSQL("DROP TRIGGER attachment_insert"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER attachment_delete"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db); //createTriggers(db);
} }
}) })
.addMigrations(new Migration(164, 165) { .addMigrations(new Migration(164, 165) {
@ -1884,7 +1874,7 @@ public abstract class DB extends RoomDatabase {
logMigration(startVersion, endVersion); logMigration(startVersion, endVersion);
db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db); //createTriggers(db);
} }
}) })
.addMigrations(new Migration(171, 172) { .addMigrations(new Migration(171, 172) {
@ -2269,7 +2259,7 @@ public abstract class DB extends RoomDatabase {
db.execSQL("ALTER TABLE `attachment` ADD COLUMN `related` INTEGER"); db.execSQL("ALTER TABLE `attachment` ADD COLUMN `related` INTEGER");
db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db); //createTriggers(db);
} }
}).addMigrations(new Migration(223, 224) { }).addMigrations(new Migration(223, 224) {
@Override @Override
@ -2297,7 +2287,7 @@ public abstract class DB extends RoomDatabase {
logMigration(startVersion, endVersion); logMigration(startVersion, endVersion);
db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`"); db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
createTriggers(db, true); createTriggers(db);
} }
}).addMigrations(new Migration(227, 228) { }).addMigrations(new Migration(227, 228) {
@Override @Override

Loading…
Cancel
Save