mirror of https://github.com/AMT-Cheif/drift.git
moor_flutter: Don't upgrade schema when migration throws (#584)
This commit is contained in:
parent
fee32fc302
commit
7ad8c619f9
|
@ -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:
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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).
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue