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

File diff suppressed because it is too large Load Diff

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) {

View File

@ -0,0 +1,38 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/
import androidx.room.Dao;
import androidx.room.Query;
import java.util.List;
@Dao
public interface DaoSync {
@Query("SELECT * FROM sync" +
" WHERE (:entity IS NULL OR entity = :entity)" +
" AND (:reference IS NULL OR reference = :reference)" +
" AND time < :time" +
" ORDER BY time")
List<EntitySync> getSync(String entity, String reference, long time);
@Query("DELETE FROM sync WHERE time < :time")
int deleteSync(long time);
}

View File

@ -84,7 +84,7 @@ public class EntityAccount extends EntityOrder implements Serializable {
@NonNull
public Integer port;
@NonNull
public Integer auth_type; // immutable
public Integer auth_type;
public String provider;
@NonNull
public String user;

View File

@ -0,0 +1,49 @@
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2023 by Marcel Bokhorst (M66B)
*/
import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;
@Entity(
tableName = EntitySync.TABLE_NAME,
foreignKeys = {
},
indices = {
@Index(value = {"entity", "reference"}),
@Index(value = {"time"})
}
)
public class EntitySync {
static final String TABLE_NAME = "sync";
@PrimaryKey(autoGenerate = true)
public Long id;
@NonNull
public String entity;
public String reference;
@NonNull
public String action;
@NonNull
public Long time;
}

View File

@ -1543,6 +1543,10 @@ public class FragmentOptionsBackup extends FragmentBase implements SharedPrefere
long sync_status = prefs.getLong("sync_status", new Date().getTime());
Log.i("Cloud sync status=" + sync_status);
for (EntitySync s : db.sync().getSync(null, null, Long.MAX_VALUE))
Log.i("Cloud sync " + s.entity + ":" + s.reference + " " + s.action + " " + new Date(s.time));
db.sync().deleteSync(Long.MAX_VALUE);
JSONObject jsyncstatus = new JSONObject();
jsyncstatus.put("key", "sync.status");
jsyncstatus.put("rev", sync_status);