Skip to content

Commit 61366a2

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. For the test that upgrade the schema with data, with the helper, while it ended up having more lines than the original, the test becomes more structured this way. --- The tests are adapted from 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 61366a2

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

test/model/database_test.dart

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,29 @@ 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+
for (final fromVersion in versions) {
106+
if (fromVersion == versions.last) break;
107+
108+
final nextVersion = fromVersion + 1;
109+
test('from v$fromVersion to v$nextVersion', () async {
110+
final connection = await verifier.startAt(fromVersion);
111+
final db = AppDatabase(connection);
112+
await verifier.migrateAndValidate(db, nextVersion);
113+
await db.close();
114+
});
115+
116+
if (nextVersion == latestVersion) continue;
117+
test('from v$fromVersion to latest (v$latestVersion)', () async {
118+
final connection = await verifier.startAt(fromVersion);
119+
final db = AppDatabase(connection);
120+
await verifier.migrateAndValidate(db, latestVersion);
121+
await db.close();
122+
});
123+
}
106124
});
107125

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

0 commit comments

Comments
 (0)