Skip to content

Commit f350c19

Browse files
committed
db [nfc]: Extract _migrationSteps as a static
This helpfully makes it impossible for these migration steps to accidentally call methods on this database instance directly, rather than on their Migrator parameters `m`. As a bonus, the code gets three levels less indented.
1 parent e02c95d commit f350c19

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

lib/model/database.dart

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class AppDatabase extends _$AppDatabase {
9595
// See ../../README.md#generated-files for more
9696
// information on using the build_runner.
9797
// * Update [_getSchema] to handle the new schemaVersion.
98-
// * Write a migration in `onUpgrade` below.
98+
// * Write a migration in `_migrationSteps` below.
9999
// * Write tests.
100100
@override
101101
int get schemaVersion => 4; // See note.
@@ -125,6 +125,19 @@ class AppDatabase extends _$AppDatabase {
125125
});
126126
}
127127

128+
static final MigrationStepWithVersion _migrationSteps = migrationSteps(
129+
from1To2: (m, schema) async {
130+
await m.addColumn(schema.accounts, schema.accounts.ackedPushToken);
131+
},
132+
from2To3: (m, schema) async {
133+
await m.createTable(schema.globalSettings);
134+
},
135+
from3To4: (m, schema) async {
136+
await m.addColumn(
137+
schema.globalSettings, schema.globalSettings.browserPreference);
138+
},
139+
);
140+
128141
@override
129142
MigrationStrategy get migration {
130143
return MigrationStrategy(
@@ -142,19 +155,7 @@ class AppDatabase extends _$AppDatabase {
142155
}
143156
assert(1 <= from && from <= to && to <= schemaVersion);
144157

145-
await m.runMigrationSteps(from: from, to: to,
146-
steps: migrationSteps(
147-
from1To2: (m, schema) async {
148-
await m.addColumn(schema.accounts, schema.accounts.ackedPushToken);
149-
},
150-
from2To3: (m, schema) async {
151-
await m.createTable(schema.globalSettings);
152-
},
153-
from3To4: (m, schema) async {
154-
await m.addColumn(
155-
schema.globalSettings, schema.globalSettings.browserPreference);
156-
},
157-
));
158+
await m.runMigrationSteps(from: from, to: to, steps: _migrationSteps);
158159
});
159160
}
160161

0 commit comments

Comments
 (0)