From c79f95ac0399500f1858625379318daec0115363 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 2 Jun 2023 16:23:02 +0200 Subject: [PATCH] Start migrating from older storage options --- drift/lib/src/web/wasm_setup.dart | 2 +- drift/lib/src/web/wasm_setup/migration.dart | 52 +++++++++++++++++++++ drift/lib/src/web/wasm_setup/shared.dart | 7 ++- 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 drift/lib/src/web/wasm_setup/migration.dart diff --git a/drift/lib/src/web/wasm_setup.dart b/drift/lib/src/web/wasm_setup.dart index 6138eafc..41360753 100644 --- a/drift/lib/src/web/wasm_setup.dart +++ b/drift/lib/src/web/wasm_setup.dart @@ -110,7 +110,7 @@ Future openWasmDatabase({ return WasmDatabaseResult( DatabaseConnection( - WasmDatabase(sqlite3: sqlite3, path: '/app.db'), + WasmDatabase(sqlite3: sqlite3, path: '/database'), ), WasmStorageImplementation.inMemory, missingFeatures, diff --git a/drift/lib/src/web/wasm_setup/migration.dart b/drift/lib/src/web/wasm_setup/migration.dart new file mode 100644 index 00000000..2dbbce1c --- /dev/null +++ b/drift/lib/src/web/wasm_setup/migration.dart @@ -0,0 +1,52 @@ +import 'dart:typed_data'; + +import 'package:sqlite3/wasm.dart'; + +import 'shared.dart'; + +const paths = {'/database', '/database-journal'}; + + + +/// Migrates the drift database identified by [databaseName] from the IndexedDB +/// storage implementation to the OPFS storage implementation. +/// +/// Must be called in a dedicated worker, as only those have access to OPFS. +Future migrateFromIndexedDbToOpfs(String databaseName) async { + +} + +/// Migrates the drift database identified by [databaseName] from the OPFS +/// storage implementation back to IndexedDB. +/// +/// Must be called in a dedicated worker, as only those have access to OPFS. +Future migrateFromOpfsToIndexedDb(String databaseName) async { + final opfs = + await SimpleOpfsFileSystem.loadFromStorage(pathForOpfs(databaseName)); + final indexedDb = await IndexedDbFileSystem.open(dbName: databaseName); + + await _migrate(opfs, indexedDb); +} + +Future _migrate( + VirtualFileSystem source, VirtualFileSystem target) async { + for (final path in paths) { + if (target.xAccess(path, 0) != 0) { + target.xDelete(path, 0); + } + + if (source.xAccess(path, 0) != 0) { + final (file: sourceFile, outFlags: _) = + source.xOpen(Sqlite3Filename(path), SqlFlag.SQLITE_OPEN_CREATE); + final (file: targetFile, outFlags: _) = + target.xOpen(Sqlite3Filename(path), SqlFlag.SQLITE_OPEN_CREATE); + + final buffer = Uint8List(sourceFile.xFileSize()); + sourceFile.xRead(buffer, 0); + targetFile.xWrite(buffer, 0); + + sourceFile.xClose(); + targetFile.xClose(); + } + } +} diff --git a/drift/lib/src/web/wasm_setup/shared.dart b/drift/lib/src/web/wasm_setup/shared.dart index e83fe444..1b32c4b1 100644 --- a/drift/lib/src/web/wasm_setup/shared.dart +++ b/drift/lib/src/web/wasm_setup/shared.dart @@ -79,6 +79,10 @@ Future checkIndexedDbSupport() async { return true; } +String pathForOpfs(String databaseName) { + return '/drift_db/${databaseName}'; +} + class DriftServerController { /// Running drift servers by the name of the database they're serving. final Map _servers = {}; @@ -90,8 +94,7 @@ class DriftServerController { final vfs = await switch (message.storage) { WasmStorageImplementation.opfsShared => - SimpleOpfsFileSystem.loadFromStorage( - '/drift_db/${message.databaseName}'), + SimpleOpfsFileSystem.loadFromStorage(message.databaseName), WasmStorageImplementation.opfsLocks => _loadLockedWasmVfs(), WasmStorageImplementation.unsafeIndexedDb || WasmStorageImplementation.sharedIndexedDb =>