Cloud sync: triggers

This commit is contained in:
M66B
2023-01-16 09:59:21 +01:00
parent 4ca9f3222e
commit f91c317f95
6 changed files with 3073 additions and 4 deletions

View File

@@ -68,7 +68,7 @@ import javax.mail.internet.InternetAddress;
// https://developer.android.com/topic/libraries/architecture/room.html
@Database(
version = 261,
version = 262,
entities = {
EntityIdentity.class,
EntityAccount.class,
@@ -81,7 +81,8 @@ import javax.mail.internet.InternetAddress;
EntityAnswer.class,
EntityRule.class,
EntitySearch.class,
EntityLog.class
EntityLog.class,
EntitySync.class
},
views = {
TupleAccountView.class,
@@ -116,6 +117,8 @@ public abstract class DB extends RoomDatabase {
public abstract DaoLog log();
public abstract DaoSync sync();
private static int sPid;
private static Context sContext;
private static DB sInstance;
@@ -476,9 +479,17 @@ public abstract class DB extends RoomDatabase {
Log.i("Get PRAGMA " + pragma + "=<?>");
}
if (BuildConfig.DEBUG && false) {
if (BuildConfig.DEBUG) {
db.execSQL("DROP TRIGGER IF EXISTS `attachment_insert`");
db.execSQL("DROP TRIGGER IF EXISTS `attachment_delete`");
db.execSQL("DROP TRIGGER IF EXISTS `account_insert`");
db.execSQL("DROP TRIGGER IF EXISTS `account_update`");
db.execSQL("DROP TRIGGER IF EXISTS `account_delete`");
db.execSQL("DROP TRIGGER IF EXISTS `identity_insert`");
db.execSQL("DROP TRIGGER IF EXISTS `identity_update`");
db.execSQL("DROP TRIGGER IF EXISTS `identity_delete`");
}
createTriggers(db);
@@ -540,6 +551,52 @@ public abstract class DB extends RoomDatabase {
" 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");
db.execSQL("CREATE TRIGGER IF NOT EXISTS account_insert" +
" AFTER INSERT ON account" +
" BEGIN" +
" INSERT INTO sync ('entity', 'reference', 'action', 'time')" +
" VALUES ('account', NEW.uuid, 'insert', strftime('%s') * 1000);" +
" END");
db.execSQL("CREATE TRIGGER IF NOT EXISTS account_update" +
" AFTER UPDATE" +
" OF host, encryption, insecure, port, auth_type, provider, `user`, password, certificate_alias, realm, fingerprint" +
" ON account" +
" BEGIN" +
" INSERT INTO sync ('entity', 'reference', 'action', 'time')" +
" VALUES ('account', NEW.uuid, 'update', strftime('%s') * 1000);" +
" END");
db.execSQL("CREATE TRIGGER IF NOT EXISTS account_delete" +
" AFTER DELETE ON account" +
" BEGIN" +
" INSERT INTO sync ('entity', 'reference', 'action', 'time')" +
" VALUES ('account', OLD.uuid, 'delete', strftime('%s') * 1000);" +
" END");
db.execSQL("CREATE TRIGGER IF NOT EXISTS identity_insert" +
" AFTER INSERT ON identity" +
" BEGIN" +
" INSERT INTO sync ('entity', 'reference', 'action', 'time')" +
" VALUES ('identity', NEW.uuid, 'insert', strftime('%s') * 1000);" +
" END");
db.execSQL("CREATE TRIGGER IF NOT EXISTS identity_update" +
" AFTER UPDATE" +
" OF host, encryption, insecure, port, auth_type, provider, `user`, password, certificate_alias, realm, fingerprint" +
" ON identity" +
" BEGIN" +
" INSERT INTO sync ('entity', 'reference', 'action', 'time')" +
" VALUES ('identity', NEW.uuid, 'update', strftime('%s') * 1000);" +
" END");
db.execSQL("CREATE TRIGGER IF NOT EXISTS identity_delete" +
" AFTER DELETE ON identity" +
" BEGIN" +
" INSERT INTO sync ('entity', 'reference', 'action', 'time')" +
" VALUES ('identity', OLD.uuid, 'delete', strftime('%s') * 1000);" +
" END");
}
private static void logMigration(int startVersion, int endVersion) {
@@ -2637,6 +2694,20 @@ public abstract class DB extends RoomDatabase {
else
db.execSQL("UPDATE `folder` SET `hide_seen` = 0");
}
}).addMigrations(new Migration(261, 262) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
logMigration(startVersion, endVersion);
db.execSQL("CREATE TABLE `sync`" +
" (`id` INTEGER PRIMARY KEY AUTOINCREMENT" +
", `entity` TEXT NOT NULL" +
", `reference` TEXT" +
", `action` TEXT NOT NULL" +
", `time` INTEGER NOT NULL)");
db.execSQL("CREATE INDEX IF NOT EXISTS `index_sync_entity_reference` ON `sync` (`entity`, `reference`)");
db.execSQL("CREATE INDEX IF NOT EXISTS `index_sync_time` ON `sync` (`time`)");
createTriggers(db);
}
}).addMigrations(new Migration(998, 999) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {