Add more docs in the migrations example

This commit is contained in:
Simon Binder 2020-10-21 10:33:31 +02:00
parent e9ac099b31
commit 6eab57f114
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,23 @@
Example to demonstrate tests for schema migrations.
See `test/migration_test.dart` on how to use the generated verification code.
## Workflow
### Schema changes
After adapting a schema and incrementing the `schemaVersion` in the database, run
```
dart pub run moor_generator schema dump lib/database.dart moor_migrations/moor_schema_v2.json
```
Replace `_v2` with the current `schemaVersion`.
### Generating test code
Run
```
dart pub run moor_generator schema generate moor_migrations/ test/generated/
```

View File

@ -20,7 +20,12 @@ class Database extends _$Database {
MigrationStrategy get migration { MigrationStrategy get migration {
return MigrationStrategy( return MigrationStrategy(
onUpgrade: (m, before, now) async { onUpgrade: (m, before, now) async {
await m.addColumn(users, users.name); for (var target = before + 1; target <= now; target++) {
if (target == 2) {
// Migration from 1 to 2: Add name column in users
await m.addColumn(users, users.name);
}
}
}, },
); );
} }

View File

@ -1,4 +1,5 @@
name: migrations_example name: migrations_example
publish_to: none
version: 1.0.0 version: 1.0.0
environment: environment:

View File

@ -2,6 +2,7 @@ import 'package:migrations_example/database.dart';
import 'package:test/test.dart'; import 'package:test/test.dart';
import 'package:moor_generator/api/migrations.dart'; import 'package:moor_generator/api/migrations.dart';
// Import the generated schema helper to instantiate databases at old versions.
import 'generated/schema.dart'; import 'generated/schema.dart';
void main() { void main() {