moved to migration strategy, moved daos to top lib
This commit is contained in:
23
test/drift/sendtrain/generated/schema.dart
Normal file
23
test/drift/sendtrain/generated/schema.dart
Normal file
@ -0,0 +1,23 @@
|
||||
// dart format width=80
|
||||
// GENERATED CODE, DO NOT EDIT BY HAND.
|
||||
// ignore_for_file: type=lint
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/internal/migrations.dart';
|
||||
import 'schema_v1.dart' as v1;
|
||||
import 'schema_v2.dart' as v2;
|
||||
|
||||
class GeneratedHelper implements SchemaInstantiationHelper {
|
||||
@override
|
||||
GeneratedDatabase databaseForVersion(QueryExecutor db, int version) {
|
||||
switch (version) {
|
||||
case 1:
|
||||
return v1.DatabaseAtV1(db);
|
||||
case 2:
|
||||
return v2.DatabaseAtV2(db);
|
||||
default:
|
||||
throw MissingSchemaException(version, versions);
|
||||
}
|
||||
}
|
||||
|
||||
static const versions = const [1, 2];
|
||||
}
|
1916
test/drift/sendtrain/generated/schema_v1.dart
Normal file
1916
test/drift/sendtrain/generated/schema_v1.dart
Normal file
File diff suppressed because it is too large
Load Diff
1916
test/drift/sendtrain/generated/schema_v2.dart
Normal file
1916
test/drift/sendtrain/generated/schema_v2.dart
Normal file
File diff suppressed because it is too large
Load Diff
104
test/drift/sendtrain/migration_test.dart
Normal file
104
test/drift/sendtrain/migration_test.dart
Normal file
@ -0,0 +1,104 @@
|
||||
// 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:sendtrain/database/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 oldSessionsData = <v1.SessionsData>[];
|
||||
final expectedNewSessionsData = <v2.SessionsData>[];
|
||||
|
||||
final oldActivitiesData = <v1.ActivitiesData>[];
|
||||
final expectedNewActivitiesData = <v2.ActivitiesData>[];
|
||||
|
||||
final oldSessionActivitiesData = <v1.SessionActivitiesData>[];
|
||||
final expectedNewSessionActivitiesData = <v2.SessionActivitiesData>[];
|
||||
|
||||
final oldActionsData = <v1.ActionsData>[];
|
||||
final expectedNewActionsData = <v2.ActionsData>[];
|
||||
|
||||
final oldActivityActionsData = <v1.ActivityActionsData>[];
|
||||
final expectedNewActivityActionsData = <v2.ActivityActionsData>[];
|
||||
|
||||
final oldMediaItemsData = <v1.MediaItemsData>[];
|
||||
final expectedNewMediaItemsData = <v2.MediaItemsData>[];
|
||||
|
||||
final oldObjectMediaItemsData = <v1.ObjectMediaItemsData>[];
|
||||
final expectedNewObjectMediaItemsData = <v2.ObjectMediaItemsData>[];
|
||||
|
||||
await verifier.testWithDataIntegrity(
|
||||
oldVersion: 1,
|
||||
newVersion: 2,
|
||||
createOld: v1.DatabaseAtV1.new,
|
||||
createNew: v2.DatabaseAtV2.new,
|
||||
openTestedDatabase: AppDatabase.new,
|
||||
createItems: (batch, oldDb) {
|
||||
batch.insertAll(oldDb.sessions, oldSessionsData);
|
||||
batch.insertAll(oldDb.activities, oldActivitiesData);
|
||||
batch.insertAll(oldDb.sessionActivities, oldSessionActivitiesData);
|
||||
batch.insertAll(oldDb.actions, oldActionsData);
|
||||
batch.insertAll(oldDb.activityActions, oldActivityActionsData);
|
||||
batch.insertAll(oldDb.mediaItems, oldMediaItemsData);
|
||||
batch.insertAll(oldDb.objectMediaItems, oldObjectMediaItemsData);
|
||||
},
|
||||
validateItems: (newDb) async {
|
||||
expect(
|
||||
expectedNewSessionsData, await newDb.select(newDb.sessions).get());
|
||||
expect(expectedNewActivitiesData,
|
||||
await newDb.select(newDb.activities).get());
|
||||
expect(expectedNewSessionActivitiesData,
|
||||
await newDb.select(newDb.sessionActivities).get());
|
||||
expect(expectedNewActionsData, await newDb.select(newDb.actions).get());
|
||||
expect(expectedNewActivityActionsData,
|
||||
await newDb.select(newDb.activityActions).get());
|
||||
expect(expectedNewMediaItemsData,
|
||||
await newDb.select(newDb.mediaItems).get());
|
||||
expect(expectedNewObjectMediaItemsData,
|
||||
await newDb.select(newDb.objectMediaItems).get());
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user