[mob] Create db for collections & collection_files

This commit is contained in:
Neeraj Gupta
2025-02-11 15:45:03 +05:30
parent 670d6e8470
commit 77cde87927
3 changed files with 70 additions and 5 deletions

View File

@@ -1,14 +1,15 @@
import "dart:developer";
import "dart:io";
import "package:logging/logging.dart";
import "package:path/path.dart";
import "package:path_provider/path_provider.dart";
import "package:photos/db/remote/migration.dart";
import "package:sqlite_async/sqlite_async.dart";
class RemoteDB {
final Logger _logger = Logger("RemoteDB");
static const _databaseName = "ente.remote.db";
var devLog = log;
class RemoteDB {
static const _databaseName = "remote.db";
// only have a single app-wide reference to the database
static Future<SqliteDatabase>? _sqliteAsyncDBFuture;
@@ -23,8 +24,9 @@ class RemoteDB {
final Directory documentsDirectory =
await getApplicationDocumentsDirectory();
final String path = join(documentsDirectory.path, _databaseName);
_logger.info("DB path " + path);
devLog("DB path " + path);
final database = SqliteDatabase(path: path);
await RemoteDBMigration.migrate(database);
return database;
}
}

View File

@@ -0,0 +1,62 @@
import "package:flutter/cupertino.dart";
import "package:sqlite_async/sqlite_async.dart";
class RemoteDBMigration {
static const migrationScripts = [
'''
CREATE TABLE collections (
id INTEGER PRIMARY KEY,
owner TEXT NOT NULL,
enc_key TEXT NOT NULL,
enc_key_nonce TEXT NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
local_path TEXT,
is_deleted INTEGER NOT NULL
updation_time INTEGER NOT NULL,
sharees TEXT NOT NULL DEFAULT [],
public_urls TEXT NOT NULL DEFAULT [],
mmd_encoded_json TEXT NOT NULL DEFAULT {},
mmd_ver INTEGER NOT NULL DEFAULT 0,
pub_mmd_encoded_json TEXT DEFAULT {}',
pub_mmd_ver INTEGER NOT NULL DEFAULT 0,
shared_mmd_json TEXT NOT NULL {},
shared_mmd_ver INTEGER NOT NULL DEFAULT 0,
)
''',
'''
CREATE TABLE collection_files (
file_id INTEGER NOT NULL,
collection_id INTEGER NOT NULL,
PRIMARY KEY (file_id, collection_id)
enc_key TEXT NOT NULL,
enc_key_nonce TEXT NOT NULL,
is_deleted INTEGER NOT NULL
updated_at INTEGER NOT NULL,
created_at INTEGER NOT NULL DEFAULT 0,
)
''',
];
static Future<void> migrate(
SqliteDatabase database,
) async {
final result = await database.execute('PRAGMA user_version');
final currentVersion = result[0]['user_version'] as int;
final toVersion = migrationScripts.length;
if (currentVersion < toVersion) {
debugPrint("Migrating Remote DB from $currentVersion to $toVersion");
await database.writeTransaction((tx) async {
for (int i = currentVersion + 1; i <= toVersion; i++) {
await tx.execute(migrationScripts[i - 1]);
}
await tx.execute('PRAGMA user_version = $toVersion');
});
} else if (currentVersion > toVersion) {
throw AssertionError(
"currentVersion($currentVersion) cannot be greater than toVersion($toVersion)",
);
}
}
}

1
mobile/thirdparty/flutter vendored Submodule

Submodule mobile/thirdparty/flutter added at 2663184aa7