fix: Make database deleteable without the need to init the boxcollection

This commit is contained in:
krille-chan 2024-03-24 18:02:20 +01:00
parent 29c0bc7acc
commit 76a4ce7f67
No known key found for this signature in database
4 changed files with 17 additions and 13 deletions

View File

@ -9,10 +9,9 @@ import 'package:matrix/src/database/zone_transaction_mixin.dart';
class BoxCollection with ZoneTransactionMixin {
final Database _db;
final Set<String> boxNames;
final String _name;
final IdbFactory _idbFactory;
final String name;
BoxCollection(this._db, this.boxNames, this._name, this._idbFactory);
BoxCollection(this._db, this.boxNames, this.name);
static Future<BoxCollection> open(
String name,
@ -29,7 +28,7 @@ class BoxCollection with ZoneTransactionMixin {
db.createObjectStore(name, autoIncrement: true);
}
});
return BoxCollection(db, boxNames, name, idbFactory);
return BoxCollection(db, boxNames, name);
}
Box<V> openBox<V>(String name) {
@ -81,7 +80,8 @@ class BoxCollection with ZoneTransactionMixin {
return _db.close();
}
Future<void> delete() => _idbFactory.deleteDatabase(_name);
static Future<void> delete(String path, [IdbFactory? factory]) =>
(factory ?? window.indexedDB!).deleteDatabase(path);
}
class Box<V> {

View File

@ -1594,5 +1594,8 @@ class MatrixSdkDatabase extends DatabaseApi {
}
@override
Future<void> delete() => _collection.delete();
Future<void> delete() => BoxCollection.delete(
name,
sqfliteFactory ?? idbFactory,
);
}

View File

@ -10,9 +10,9 @@ import 'package:matrix/src/database/zone_transaction_mixin.dart';
class BoxCollection with ZoneTransactionMixin {
final Database _db;
final Set<String> boxNames;
final DatabaseFactory? _factory;
final String name;
BoxCollection(this._db, this.boxNames, this._factory);
BoxCollection(this._db, this.boxNames, this.name);
static Future<BoxCollection> open(
String name,
@ -32,7 +32,7 @@ class BoxCollection with ZoneTransactionMixin {
batch.execute('CREATE INDEX IF NOT EXISTS k_index ON $name (k)');
}
await batch.commit(noResult: true);
return BoxCollection(sqfliteDatabase, boxNames, sqfliteFactory);
return BoxCollection(sqfliteDatabase, boxNames, name);
}
Box<V> openBox<V>(String name) {
@ -67,8 +67,8 @@ class BoxCollection with ZoneTransactionMixin {
Future<void> close() => _db.close();
Future<void> delete() =>
(_factory ?? databaseFactory).deleteDatabase(_db.path);
static Future<void> delete(String path, [DatabaseFactory? factory]) =>
(factory ?? databaseFactory).deleteDatabase(path);
}
class Box<V> {

View File

@ -9,8 +9,9 @@ void main() {
const Set<String> boxNames = {'cats', 'dogs'};
const data = {'name': 'Fluffy', 'age': 2};
const data2 = {'name': 'Loki', 'age': 4};
late Database db;
setUp(() async {
final db = await databaseFactoryFfi.openDatabase(':memory:');
db = await databaseFactoryFfi.openDatabase(':memory:');
collection = await BoxCollection.open(
'testbox',
boxNames,
@ -91,7 +92,7 @@ void main() {
});
test('Box.delete', () async {
await collection.delete();
await BoxCollection.delete(db.path, databaseFactoryFfi);
});
});
}