From da4a5fd93fd844a6c641975061d1766c4ad02cd6 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 6 Apr 2022 08:39:18 +0200 Subject: [PATCH] Fixed upgrading DB triggers --- app/src/main/java/eu/faircode/email/DB.java | 86 +++++++++------------ 1 file changed, 38 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index 2a1e0bbf08..259eeb3b24 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -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 image = new ArrayList<>(); - for (String img : ImageHelper.IMAGE_TYPES) + List 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