About migration test #1967
-
I write the test that database upgrade from v1 to v2, and current database version is v3. test('upgrade from v1 to v2', () async {
final InitializedSchema schema = await verifier.schemaAt(1);
// Add some data to the users table, which only has an id column at v1
final v1.DatabaseAtV1 oldDb = v1.DatabaseAtV1.connect(schema.newConnection());
print(oldDb.schemaVersion); // print 1
await oldDb.into(oldDb.partner).insert(const v1.PartnerCompanion(
uid: Value<int>(1234),
appId: Value<String>('1234'),
displayName: Value<String>('V1'),
stopSharing: Value<bool>(false),
stopSharingDate: Value<String>('20220101000000'),
));
await oldDb.into(oldDb.glucoseMapping).insert(const v1.GlucoseMappingCompanion(
uid: Value<int>(1234),
year: Value<int>(2022),
month: Value<int>(1),
lastModify: Value<String>('20220101000000'),
downloadLink: Value<String>('https://www.google.com'),
));
await oldDb.close();
// Run the migration and verify that it adds the name column.
final Database db = Database.connect(schema.newConnection());
print(db.schemaVersion); // print 3, it should be 1.
await verifier.migrateAndValidate(db, 2);
await db.close();
// Make sure the user is still here
final v2.DatabaseAtV2 migratedDb = v2.DatabaseAtV2.connect(schema.newConnection());
final v2.GlucoseMappingData glucoseMappingData = await migratedDb.select(migratedDb.glucoseMapping).getSingle();
expect(glucoseMappingData.uid, 1234);
expect(glucoseMappingData.year, 2022);
expect(glucoseMappingData.month, 1);
await migratedDb.close();
});
I print the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The To make the migration test as realistic as possible, drift uses your regular database class without any modifications, there's no magic being done to the |
Beta Was this translation helpful? Give feedback.
The
schemaVersion
getter of a database returns the latest schema known to the database class. So in your case, you have something like@override int get schemaVersion => 3
in your code, and that is the one that gets returned in the test.To make the migration test as realistic as possible, drift uses your regular database class without any modifications, there's no magic being done to the
Database
class you've written. The only difference is that, inside a schema test, theto
parameter of youronUpgrade
callback is set to the schema version your test is migrating to.