diff --git a/extras/integration_tests/flutter_db/pubspec.lock b/extras/integration_tests/flutter_db/pubspec.lock index 8d3d93e1..b165faa1 100644 --- a/extras/integration_tests/flutter_db/pubspec.lock +++ b/extras/integration_tests/flutter_db/pubspec.lock @@ -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: diff --git a/extras/integration_tests/tests/lib/suite/migrations.dart b/extras/integration_tests/tests/lib/suite/migrations.dart index 7224f1ff..74b6b827 100644 --- a/extras/integration_tests/tests/lib/suite/migrations.dart +++ b/extras/integration_tests/tests/lib/suite/migrations.dart @@ -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(); + }); } diff --git a/moor_flutter/CHANGELOG.md b/moor_flutter/CHANGELOG.md index bc3bcbe5..e06cb1f5 100644 --- a/moor_flutter/CHANGELOG.md +++ b/moor_flutter/CHANGELOG.md @@ -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). diff --git a/moor_flutter/lib/moor_flutter.dart b/moor_flutter/lib/moor_flutter.dart index d2d439dc..9c392480 100644 --- a/moor_flutter/lib/moor_flutter.dart +++ b/moor_flutter/lib/moor_flutter.dart @@ -20,7 +20,6 @@ export 'package:moor/moor.dart'; typedef DatabaseCreator = FutureOr 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 get schemaVersion async { + final result = await _db.rawQuery('PRAGMA user_version;'); + return result.single.values.first as int; + } + + @override + Future setSchemaVersion(int version) async { + await _db.rawUpdate('PRAGMA user_version = $version;'); + } +} + class _SqfliteTransactionDelegate extends SupportedTransactionDelegate { final _SqfliteDelegate delegate; diff --git a/moor_flutter/pubspec.yaml b/moor_flutter/pubspec.yaml index edec7a2f..b7bab9f3 100644 --- a/moor_flutter/pubspec.yaml +++ b/moor_flutter/pubspec.yaml @@ -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