Skip to content

Commit 14c1d6f

Browse files
committed
Fix migrations not running for drift_sqlite_async.
1 parent 1e64f5d commit 14c1d6f

File tree

7 files changed

+457
-10
lines changed

7 files changed

+457
-10
lines changed

packages/drift_sqlite_async/example/main.g.dart

+131
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/drift_sqlite_async/example/with_migrations.dart

+6-9
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,18 @@ class AppDatabase extends _$AppDatabase {
2121

2222
@override
2323
MigrationStrategy get migration {
24-
return MigrationStrategy(
25-
onCreate: (m) async {
26-
// In this example, the schema is managed by Drift
27-
await m.createAll();
28-
},
29-
);
24+
return MigrationStrategy(onCreate: (m) async {
25+
// In this example, the schema is managed by Drift.
26+
// For more options, see:
27+
// https://drift.simonbinder.eu/migrations/#usage
28+
await m.createAll();
29+
});
3030
}
3131
}
3232

3333
Future<void> main() async {
3434
final db = SqliteDatabase(path: 'with_migrations.db');
3535

36-
await db.execute(
37-
'CREATE TABLE IF NOT EXISTS todos(id integer primary key, description text)');
38-
3936
final appdb = AppDatabase(db);
4037

4138
// Watch a query on the Drift database

packages/drift_sqlite_async/example/with_migrations.g.dart

+131
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/drift_sqlite_async/lib/src/executor.dart

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class _SqliteAsyncDelegate extends _SqliteAsyncQueryDelegate
1515
implements DatabaseDelegate {
1616
final SqliteConnection db;
1717
bool _closed = false;
18+
bool _calledOpen = false;
1819

1920
_SqliteAsyncDelegate(this.db) : super(db, db.writeLock);
2021

@@ -30,12 +31,15 @@ class _SqliteAsyncDelegate extends _SqliteAsyncQueryDelegate
3031
_SqliteAsyncTransactionDelegate(db);
3132

3233
@override
33-
bool get isOpen => !db.closed && !_closed;
34+
bool get isOpen => !db.closed && !_closed && _calledOpen;
3435

3536
@override
3637
Future<void> open(QueryExecutorUser user) async {
3738
// Workaround - this ensures the db is open
3839
await db.get('SELECT 1');
40+
// We need to delay this until open() has been called, otherwise
41+
// migrations don't run.
42+
_calledOpen = true;
3943
}
4044

4145
@override

packages/drift_sqlite_async/test/generated/database.dart

+13
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ class TodoDatabase extends _$TodoDatabase {
1919
@override
2020
int get schemaVersion => 1;
2121
}
22+
23+
class TodosMigrationDatabase extends TodoDatabase {
24+
TodosMigrationDatabase(SqliteConnection db) : super(db);
25+
26+
@override
27+
MigrationStrategy get migration {
28+
return MigrationStrategy(
29+
onCreate: (m) async {
30+
await m.createAll();
31+
},
32+
);
33+
}
34+
}

0 commit comments

Comments
 (0)