moor_flutter: Don't upgrade schema when migration throws (#584)

This commit is contained in:
Simon Binder 2020-05-28 10:40:33 +02:00
parent fee32fc302
commit 7ad8c619f9
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
5 changed files with 52 additions and 17 deletions

View File

@ -173,14 +173,14 @@ packages:
path: "../../../moor"
relative: true
source: path
version: "3.0.0"
version: "3.1.0"
moor_ffi:
dependency: "direct main"
description:
path: "../../../moor_ffi"
relative: true
source: path
version: "0.5.0-dev"
version: "0.6.0"
moor_flutter:
dependency: "direct main"
description:

View File

@ -58,4 +58,30 @@ void migrationTests(TestExecutor executor) {
await database.close();
});
test('does not apply schema version when migration throws', () async {
var database = Database(executor.createConnection(), schemaVersion: 1);
await database.executor.ensureOpen(database); // Create the database
await database.close();
database = Database(executor.createConnection(), schemaVersion: 2);
database.overrideMigration = MigrationStrategy(
onUpgrade: (m, from, to) => Future.error('oops'),
);
try {
await database.executor.ensureOpen(database);
fail('Should have thrown');
} catch (e) {
//ignore
await database.close();
}
// Open it one last time, the schema version should still be at 1
database = Database(executor.createConnection(), schemaVersion: 1);
final result =
await database.customSelect('PRAGMA user_version').getSingle();
expect(result.data.values.single, 1);
await database.close();
});
}

View File

@ -1,3 +1,7 @@
## 3.1.0
Don't write the schema version to the database if the migration throws an exception.
## 3.0.0
Support `moor` version 3.0. To learn what's new, head over to [its changelog](https://pub.dev/packages/moor).

View File

@ -20,7 +20,6 @@ export 'package:moor/moor.dart';
typedef DatabaseCreator = FutureOr<void> Function(File file);
class _SqfliteDelegate extends DatabaseDelegate with _SqfliteExecutor {
int _loadedSchemaVersion;
@override
s.Database db;
@ -35,9 +34,10 @@ class _SqfliteDelegate extends DatabaseDelegate with _SqfliteExecutor {
singleInstance ??= true;
}
DbVersionDelegate _delegate;
@override
DbVersionDelegate get versionDelegate {
return OnOpenVersionDelegate(() => Future.value(_loadedSchemaVersion));
return _delegate ??= _SqfliteVersionDelegate(db);
}
@override
@ -62,20 +62,8 @@ class _SqfliteDelegate extends DatabaseDelegate with _SqfliteExecutor {
}
// default value when no migration happened
_loadedSchemaVersion = user.schemaVersion;
db = await s.openDatabase(
resolvedPath,
version: user.schemaVersion,
onCreate: (db, version) {
_loadedSchemaVersion = 0;
},
onUpgrade: (db, from, to) {
_loadedSchemaVersion = from;
},
onDowngrade: (db, from, to) {
_loadedSchemaVersion = from;
},
singleInstance: singleInstance,
);
}
@ -86,6 +74,23 @@ class _SqfliteDelegate extends DatabaseDelegate with _SqfliteExecutor {
}
}
class _SqfliteVersionDelegate extends DynamicVersionDelegate {
final s.Database _db;
_SqfliteVersionDelegate(this._db);
@override
Future<int> get schemaVersion async {
final result = await _db.rawQuery('PRAGMA user_version;');
return result.single.values.first as int;
}
@override
Future<void> setSchemaVersion(int version) async {
await _db.rawUpdate('PRAGMA user_version = $version;');
}
}
class _SqfliteTransactionDelegate extends SupportedTransactionDelegate {
final _SqfliteDelegate delegate;

View File

@ -1,6 +1,6 @@
name: moor_flutter
description: Flutter implementation of moor, a safe and reactive persistence library for Dart applications
version: 3.0.0
version: 3.1.0
repository: https://github.com/simolus3/moor
homepage: https://moor.simonbinder.eu/
issue_tracker: https://github.com/simolus3/moor/issues