Start migrating from older storage options

This commit is contained in:
Simon Binder 2023-06-02 16:23:02 +02:00
parent 0a052ff338
commit c79f95ac03
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 58 additions and 3 deletions

View File

@ -110,7 +110,7 @@ Future<WasmDatabaseResult> openWasmDatabase({
return WasmDatabaseResult(
DatabaseConnection(
WasmDatabase(sqlite3: sqlite3, path: '/app.db'),
WasmDatabase(sqlite3: sqlite3, path: '/database'),
),
WasmStorageImplementation.inMemory,
missingFeatures,

View File

@ -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<void> 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<void> migrateFromOpfsToIndexedDb(String databaseName) async {
final opfs =
await SimpleOpfsFileSystem.loadFromStorage(pathForOpfs(databaseName));
final indexedDb = await IndexedDbFileSystem.open(dbName: databaseName);
await _migrate(opfs, indexedDb);
}
Future<void> _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();
}
}
}

View File

@ -79,6 +79,10 @@ Future<bool> 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<String, DriftServer> _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 =>