Fixed upgrading DB triggers

This commit is contained in:
M66B 2022-04-06 08:39:18 +02:00
parent 63a65eed62
commit da4a5fd93f
1 changed files with 38 additions and 48 deletions

View File

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