mirror of https://github.com/AMT-Cheif/drift.git
The onUpgrade callback in the moor migrator was not being fired when downgrading a database using moor_flutter.
The change has been made to moor_flutter and to moor_flutter with encryption
This commit is contained in:
parent
3fba2ce05b
commit
2bcd51f437
|
@ -73,6 +73,9 @@ class _SqfliteDelegate extends DatabaseDelegate with _SqfliteExecutor {
|
||||||
onUpgrade: (db, from, to) {
|
onUpgrade: (db, from, to) {
|
||||||
_loadedSchemaVersion = from;
|
_loadedSchemaVersion = from;
|
||||||
},
|
},
|
||||||
|
onDowngrade: (db, from, to) {
|
||||||
|
_loadedSchemaVersion = from;
|
||||||
|
},
|
||||||
singleInstance: singleInstance,
|
singleInstance: singleInstance,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,6 +87,12 @@ class Database extends _$Database {
|
||||||
|
|
||||||
Database(QueryExecutor e, {this.schemaVersion = 2}) : super(e);
|
Database(QueryExecutor e, {this.schemaVersion = 2}) : super(e);
|
||||||
|
|
||||||
|
/// It will be set in the onUpgrade callback. Null if no migration ocurred
|
||||||
|
int schemaVersionChangedFrom;
|
||||||
|
|
||||||
|
/// It will be set in the onUpgrade callback. Null if no migration ocurred
|
||||||
|
int schemaVersionChangedTo;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MigrationStrategy get migration {
|
MigrationStrategy get migration {
|
||||||
return MigrationStrategy(
|
return MigrationStrategy(
|
||||||
|
@ -97,6 +103,9 @@ class Database extends _$Database {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onUpgrade: (m, from, to) async {
|
onUpgrade: (m, from, to) async {
|
||||||
|
schemaVersionChangedFrom = from;
|
||||||
|
schemaVersionChangedTo = to;
|
||||||
|
|
||||||
if (from == 1) {
|
if (from == 1) {
|
||||||
await m.createTable(friendships);
|
await m.createTable(friendships);
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,22 @@ void migrationTests(TestExecutor executor) {
|
||||||
// the 3 initial users plus People.florian
|
// the 3 initial users plus People.florian
|
||||||
final count = await database.userCountQuery().getSingle();
|
final count = await database.userCountQuery().getSingle();
|
||||||
expect(count, 4);
|
expect(count, 4);
|
||||||
|
expect(database.schemaVersionChangedFrom, 1);
|
||||||
|
expect(database.schemaVersionChangedTo, 2);
|
||||||
|
|
||||||
|
await database.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('runs the migrator when downgrading', () async {
|
||||||
|
var database = Database(executor.createExecutor(), schemaVersion: 2);
|
||||||
|
await database.executor.ensureOpen(); // Create the database
|
||||||
|
await database.close();
|
||||||
|
|
||||||
|
database = Database(executor.createExecutor(), schemaVersion: 1);
|
||||||
|
await database.executor.ensureOpen(); // Let the migrator run
|
||||||
|
|
||||||
|
expect(database.schemaVersionChangedFrom, 2);
|
||||||
|
expect(database.schemaVersionChangedTo, 1);
|
||||||
|
|
||||||
await database.close();
|
await database.close();
|
||||||
});
|
});
|
||||||
|
|
|
@ -73,6 +73,9 @@ class _SqfliteDelegate extends DatabaseDelegate with _SqfliteExecutor {
|
||||||
onUpgrade: (db, from, to) {
|
onUpgrade: (db, from, to) {
|
||||||
_loadedSchemaVersion = from;
|
_loadedSchemaVersion = from;
|
||||||
},
|
},
|
||||||
|
onDowngrade: (db, from, to) {
|
||||||
|
_loadedSchemaVersion = from;
|
||||||
|
},
|
||||||
singleInstance: singleInstance,
|
singleInstance: singleInstance,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue