mirror of https://github.com/AMT-Cheif/drift.git
Add more docs in the migrations example
This commit is contained in:
parent
e9ac099b31
commit
6eab57f114
|
@ -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/
|
||||
```
|
|
@ -20,7 +20,12 @@ class Database extends _$Database {
|
|||
MigrationStrategy get migration {
|
||||
return MigrationStrategy(
|
||||
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);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
name: migrations_example
|
||||
publish_to: none
|
||||
version: 1.0.0
|
||||
|
||||
environment:
|
||||
|
|
|
@ -2,6 +2,7 @@ import 'package:migrations_example/database.dart';
|
|||
import 'package:test/test.dart';
|
||||
import 'package:moor_generator/api/migrations.dart';
|
||||
|
||||
// Import the generated schema helper to instantiate databases at old versions.
|
||||
import 'generated/schema.dart';
|
||||
|
||||
void main() {
|
||||
|
|
Loading…
Reference in New Issue