Skip to content

Commit 43eb74f

Browse files
committed
db test: Cover simple migrations without data programmatically
This saves us from writing the simple migration tests between schemas without data in the future. --- The tests are inspired by the test template generated from `dart run drift_dev make-migrations`. To reproduce the outputs, go through the following steps: Modify `build.yaml` by specifying the location of the database. This step is needed because `make-migrations` does not accept this through command line arguments. ``` targets: $default: builders: # ... drift_dev: options: databases: default: lib/model/database.dart ``` Then, run the following commands: ``` dart run drift_dev make-migrations cp test/model/schemas/*.json drift_schemas/default/ dart run drift_dev make-migrations ``` The first `make-migrations` run generates the initial schema and test files without looking at the versions we have in test/model/schemas. Copying the schema files and running `make-migrations` will end up creating `test/drift/default/migration_test.dart`, along with other generated files. See also: https://drift.simonbinder.eu/Migrations/#usage Signed-off-by: Zixuan James Li <[email protected]>
1 parent a73e7c9 commit 43eb74f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

test/model/database_test.dart

+24-5
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,30 @@ void main() {
9898
verifier = SchemaVerifier(GeneratedHelper());
9999
});
100100

101-
test('upgrade to v2, empty', () async {
102-
final connection = await verifier.startAt(1);
103-
final db = AppDatabase(connection);
104-
await verifier.migrateAndValidate(db, 2);
105-
await db.close();
101+
group('migrate without data', () {
102+
const versions = GeneratedHelper.versions;
103+
final latestVersion = versions.last;
104+
105+
int fromVersion = versions.first;
106+
for (final toVersion in versions.skip(1)) {
107+
test('from v$fromVersion to v$toVersion', () async {
108+
final connection = await verifier.startAt(fromVersion!);
109+
final db = AppDatabase(connection);
110+
await verifier.migrateAndValidate(db, toVersion);
111+
await db.close();
112+
});
113+
fromVersion = toVersion;
114+
}
115+
116+
for (final fromVersion in versions) {
117+
if (fromVersion == latestVersion) break;
118+
test('from v$fromVersion to latest (v$latestVersion)', () async {
119+
final connection = await verifier.startAt(fromVersion);
120+
final db = AppDatabase(connection);
121+
await verifier.migrateAndValidate(db, latestVersion);
122+
await db.close();
123+
});
124+
}
106125
});
107126

108127
test('upgrade to v2, with data', () async {

0 commit comments

Comments
 (0)