Skip to content

Commit

Permalink
Fix example snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Sep 9, 2023
1 parent def066a commit c3d2065
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
23 changes: 11 additions & 12 deletions docs/lib/snippets/migrations/migrations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ extension StepByStep2 on GeneratedDatabase {
}
}

extension StepByStep3 on GeneratedDatabase {
extension StepByStep3 on Example {
MigrationStrategy get migration {
return MigrationStrategy(
onCreate: (Migrator m) async {
Expand All @@ -180,26 +180,25 @@ extension StepByStep3 on GeneratedDatabase {
// (https://drift.simonbinder.eu/docs/advanced-features/migrations/#tips)
await customStatement('PRAGMA foreign_keys = OFF');

// Manually running migrations up to schema version 2, after which we've
// enabled step-by-step migrations.
if (from < 2) {
// we added the dueDate property in the change from version 1 to
// version 2
await m.addColumn(schema.todos, schema.todos.dueDate);
}

if (from < 3) {
// we added the priority property in the change from version 1 or 2
// to version 3
await m.addColumn(schema.todos, schema.todos.priority);
// version 2 - before switching to step-by-step migrations.
await m.addColumn(todos, todos.dueDate);
}

// At this point, we should be migrated to schema 3. For future schema
// changes, we will "start" at schema 3.
await m.runMigrationSteps(
from: math.max(3, from),
from: math.max(2, from),
to: to,
// ignore: missing_required_argument
steps: migrationSteps(
from3To4: (m, schema) async {
// Perform schema migrations for schema 4
from2To3: (m, schema) async {
// we added the priority property in the change from version 1 or
// 2 to version 3
await m.addColumn(schema.todos, schema.todos.priority);
},
),
);
Expand Down
10 changes: 5 additions & 5 deletions docs/pages/docs/Advanced Features/migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ in debug modes.

#### Moving to step-by-step migrations

If you've been using drift before `stepByStep` was added to the library, or if you've never exported a schema,
you can move to step-by-step migrations by pinning the `from` value in `Migrator.runMigrationSteps` to a known
If you've been using drift before `stepByStep` was added to the library, or if you've never exported a schema,
you can move to step-by-step migrations by pinning the `from` value in `Migrator.runMigrationSteps` to a known
starting point.

This allows you to perform all prior migration work to get the database to the "starting" point for
This allows you to perform all prior migration work to get the database to the "starting" point for
`stepByStep` migrations, and then use `stepByStep` migrations beyond that schema version.

{% include "blocks/snippet" snippets = snippets name = 'stepbystep3' %}

Here, we give a "floor" to the `from` value of `3`, since we've performed all other migration work to get to
Here, we give a "floor" to the `from` value of `2`, since we've performed all other migration work to get to
this point. From now on, you can generate step-by-step migrations for each schema change.

If you did not do this, a user migrating from schema 1 directly to schema 4 would not properly walk migrations
If you did not do this, a user migrating from schema 1 directly to schema 3 would not properly walk migrations
and apply all migration changes required.

### Writing tests
Expand Down

0 comments on commit c3d2065

Please sign in to comment.