Skip to content

Commit 4d8a667

Browse files
committed
db: Cut transaction wrappers within migrations
No transactions here will do any good if the changes aren't in a transaction with the `PRAGMA user_version =` statements that update the database's record of what version the schema is at, and therefore of which migrations might be needed. And if Drift is setting up for us such a transaction, it'll necessarily enclose the whole migration anyway.
1 parent e90ea10 commit 4d8a667

File tree

1 file changed

+23
-27
lines changed

1 file changed

+23
-27
lines changed

lib/model/database.dart

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,21 @@ class AppDatabase extends _$AppDatabase {
8787
int get schemaVersion => latestSchemaVersion;
8888

8989
static Future<void> _dropAndCreateAll(Migrator m) async {
90-
await m.database.transaction(() async {
91-
final query = m.database.customSelect(
92-
"SELECT name FROM sqlite_master WHERE type='table'");
93-
for (final row in await query.get()) {
94-
final data = row.data;
95-
final tableName = data['name'] as String;
96-
// Skip sqlite-internal tables. See for comparison:
97-
// https://www.sqlite.org/fileformat2.html#intschema
98-
// https://github.com/simolus3/drift/blob/0901c984a/drift_dev/lib/src/services/schema/verifier_common.dart#L9-L22
99-
if (tableName.startsWith('sqlite_')) continue;
100-
// No need to worry about SQL injection; this table name
101-
// was already a table name in the database, not something
102-
// that should be affected by user data.
103-
await m.database.customStatement('DROP TABLE $tableName');
104-
}
105-
await m.createAll();
106-
});
90+
final query = m.database.customSelect(
91+
"SELECT name FROM sqlite_master WHERE type='table'");
92+
for (final row in await query.get()) {
93+
final data = row.data;
94+
final tableName = data['name'] as String;
95+
// Skip sqlite-internal tables. See for comparison:
96+
// https://www.sqlite.org/fileformat2.html#intschema
97+
// https://github.com/simolus3/drift/blob/0901c984a/drift_dev/lib/src/services/schema/verifier_common.dart#L9-L22
98+
if (tableName.startsWith('sqlite_')) continue;
99+
// No need to worry about SQL injection; this table name
100+
// was already a table name in the database, not something
101+
// that should be affected by user data.
102+
await m.database.customStatement('DROP TABLE $tableName');
103+
}
104+
await m.createAll();
107105
}
108106

109107
static final MigrationStepWithVersion _migrationSteps = migrationSteps(
@@ -122,16 +120,14 @@ class AppDatabase extends _$AppDatabase {
122120
// This migration ensures there is a row in GlobalSettings.
123121
// (If the app already ran at schema 3 or 4, there will be;
124122
// if not, there won't be before this point.)
125-
await m.database.transaction(() async {
126-
final rows = await m.database.select(schema.globalSettings).get();
127-
if (rows.isEmpty) {
128-
await m.database.into(schema.globalSettings).insert(
129-
// No field values; just use the defaults for both fields.
130-
// (This is like `GlobalSettingsCompanion.insert()`, but
131-
// without dependence on the current schema.)
132-
RawValuesInsertable({}));
133-
}
134-
});
123+
final rows = await m.database.select(schema.globalSettings).get();
124+
if (rows.isEmpty) {
125+
await m.database.into(schema.globalSettings).insert(
126+
// No field values; just use the defaults for both fields.
127+
// (This is like `GlobalSettingsCompanion.insert()`, but
128+
// without dependence on the current schema.)
129+
RawValuesInsertable({}));
130+
}
135131
},
136132
);
137133

0 commit comments

Comments
 (0)