@@ -105,39 +105,56 @@ void main() {
105
105
await db.close ();
106
106
}, skip: true ); // TODO(#1172): unskip this
107
107
108
- test ('upgrade to v2, empty' , () async {
109
- final connection = await verifier.startAt (1 );
110
- final db = AppDatabase (connection);
111
- await verifier.migrateAndValidate (db, 2 );
112
- await db.close ();
108
+ group ('migrate without data' , () {
109
+ // These simple tests verify all possible schema updates with a simple (no
110
+ // data) migration. This is a quick way to ensure that written database
111
+ // migrations properly alter the schema.
112
+ const versions = GeneratedHelper .versions;
113
+ for (final (i, fromVersion) in versions.indexed) {
114
+ group ('from $fromVersion ' , () {
115
+ for (final toVersion in versions.skip (i + 1 )) {
116
+ test ('to $toVersion ' , () async {
117
+ final schema = await verifier.schemaAt (fromVersion);
118
+ final db = AppDatabase (schema.newConnection ());
119
+ await verifier.migrateAndValidate (db, toVersion);
120
+ await db.close ();
121
+ });
122
+ }
123
+ });
124
+ }
113
125
});
114
126
115
- test ('upgrade to v2, with data' , () async {
116
- final schema = await verifier.schemaAt (1 );
117
- final before = v1.DatabaseAtV1 (schema.newConnection ());
118
- await before.into (before.accounts).insert (v1.AccountsCompanion .insert (
119
- realmUrl: 'https://chat.example/' ,
120
- userId: 1 ,
121
-
122
- apiKey: '1234' ,
123
- zulipVersion: '6.0' ,
124
- zulipMergeBase: const Value ('6.0' ),
125
- zulipFeatureLevel: 42 ,
126
- ));
127
- final accountV1 = await before.select (before.accounts).watchSingle ().first;
128
- await before.close ();
129
-
130
- final db = AppDatabase (schema.newConnection ());
131
- await verifier.migrateAndValidate (db, 2 );
132
- await db.close ();
133
-
134
- final after = v2.DatabaseAtV2 (schema.newConnection ());
135
- final account = await after.select (after.accounts).getSingle ();
136
- check (account.toJson ()).deepEquals ({
137
- ...accountV1.toJson (),
138
- 'ackedPushToken' : null ,
127
+ // Testing this can be useful for migrations that change existing columns
128
+ // (e.g. by alterating their type or constraints). Migrations that only add
129
+ // tables or columns typically don't need these advanced tests. For more
130
+ // information, see https://drift.simonbinder.eu/migrations/tests/#verifying-data-integrity
131
+ group ('migrate with data' , () {
132
+ test ('upgrade to v2' , () async {
133
+ late final v1.AccountsData oldAccountData;
134
+ await verifier.testWithDataIntegrity (
135
+ oldVersion: 1 , createOld: v1.DatabaseAtV1 .new ,
136
+ newVersion: 2 , createNew: v2.DatabaseAtV2 .new ,
137
+ openTestedDatabase: AppDatabase .new ,
138
+ createItems: (batch, oldDb) async {
139
+ await oldDb.into (oldDb.accounts).insert (v1.AccountsCompanion .insert (
140
+ realmUrl: 'https://chat.example/' ,
141
+ userId: 1 ,
142
+
143
+ apiKey: '1234' ,
144
+ zulipVersion: '6.0' ,
145
+ zulipMergeBase: const Value ('6.0' ),
146
+ zulipFeatureLevel: 42 ,
147
+ ));
148
+ oldAccountData = await oldDb.select (oldDb.accounts).watchSingle ().first;
149
+ },
150
+ validateItems: (newDb) async {
151
+ final account = await newDb.select (newDb.accounts).getSingle ();
152
+ check (account.toJson ()).deepEquals ({
153
+ ...oldAccountData.toJson (),
154
+ 'ackedPushToken' : null ,
155
+ });
156
+ });
139
157
});
140
- await after.close ();
141
158
});
142
159
});
143
160
}
0 commit comments