Initial and done prolly
This commit is contained in:
commit
6f88b9966f
175 changed files with 15445 additions and 0 deletions
86
test/drift/my_database/migration_test.dart
Normal file
86
test/drift/my_database/migration_test.dart
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
// dart format width=80
|
||||
// ignore_for_file: unused_local_variable, unused_import
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift_dev/api/migrations_native.dart';
|
||||
import 'package:groceries_manager/db/database.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'generated/schema.dart';
|
||||
|
||||
import 'generated/schema_v1.dart' as v1;
|
||||
import 'generated/schema_v2.dart' as v2;
|
||||
|
||||
void main() {
|
||||
driftRuntimeOptions.dontWarnAboutMultipleDatabases = true;
|
||||
late SchemaVerifier verifier;
|
||||
|
||||
setUpAll(() {
|
||||
verifier = SchemaVerifier(GeneratedHelper());
|
||||
});
|
||||
|
||||
group('simple database migrations', () {
|
||||
// These simple tests verify all possible schema updates with a simple (no
|
||||
// data) migration. This is a quick way to ensure that written database
|
||||
// migrations properly alter the schema.
|
||||
final versions = GeneratedHelper.versions;
|
||||
for (final (i, fromVersion) in versions.indexed) {
|
||||
group('from $fromVersion', () {
|
||||
for (final toVersion in versions.skip(i + 1)) {
|
||||
test('to $toVersion', () async {
|
||||
final schema = await verifier.schemaAt(fromVersion);
|
||||
final db = AppDatabase(schema.newConnection());
|
||||
await verifier.migrateAndValidate(db, toVersion);
|
||||
await db.close();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// The following template shows how to write tests ensuring your migrations
|
||||
// preserve existing data.
|
||||
// Testing this can be useful for migrations that change existing columns
|
||||
// (e.g. by alterating their type or constraints). Migrations that only add
|
||||
// tables or columns typically don't need these advanced tests. For more
|
||||
// information, see https://drift.simonbinder.eu/migrations/tests/#verifying-data-integrity
|
||||
// TODO: This generated template shows how these tests could be written. Adopt
|
||||
// it to your own needs when testing migrations with data integrity.
|
||||
test("migration from v1 to v2 does not corrupt data", () async {
|
||||
// Add data to insert into the old database, and the expected rows after the
|
||||
// migration.
|
||||
// TODO: Fill these lists
|
||||
final oldProductCategoryData = <v1.ProductCategoryData>[];
|
||||
final expectedNewProductCategoryData = <v2.ProductCategoryData>[];
|
||||
|
||||
final oldStorageLocationData = <v1.StorageLocationData>[];
|
||||
final expectedNewStorageLocationData = <v2.StorageLocationData>[];
|
||||
|
||||
final oldProductData = <v1.ProductData>[];
|
||||
final expectedNewProductData = <v2.ProductData>[];
|
||||
|
||||
final oldShoppingListItemData = <v1.ShoppingListItemData>[];
|
||||
final expectedNewShoppingListItemData = <v2.ShoppingListItemData>[];
|
||||
|
||||
await verifier.testWithDataIntegrity(
|
||||
oldVersion: 1,
|
||||
newVersion: 2,
|
||||
createOld: v1.DatabaseAtV1.new,
|
||||
createNew: v2.DatabaseAtV2.new,
|
||||
openTestedDatabase: AppDatabase.new,
|
||||
createItems: (batch, oldDb) {
|
||||
batch.insertAll(oldDb.productCategory, oldProductCategoryData);
|
||||
batch.insertAll(oldDb.storageLocation, oldStorageLocationData);
|
||||
batch.insertAll(oldDb.product, oldProductData);
|
||||
batch.insertAll(oldDb.shoppingListItem, oldShoppingListItemData);
|
||||
},
|
||||
validateItems: (newDb) async {
|
||||
expect(expectedNewProductCategoryData,
|
||||
await newDb.select(newDb.productCategory).get());
|
||||
expect(expectedNewStorageLocationData,
|
||||
await newDb.select(newDb.storageLocation).get());
|
||||
expect(expectedNewProductData, await newDb.select(newDb.product).get());
|
||||
expect(expectedNewShoppingListItemData,
|
||||
await newDb.select(newDb.shoppingListItem).get());
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue