Skip to content

Fix drift migrations #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/drift_sqlite_async/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Supported functionality:
2. Transactions and nested transactions.
3. Table updates are propagated between sqlite_async and Drift - watching queries works using either API.
4. Select queries can run concurrently with writes and other select statements.
5. Drift migrations are supported (optional).

## Usage

Expand Down
131 changes: 131 additions & 0 deletions packages/drift_sqlite_async/example/main.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 6 additions & 9 deletions packages/drift_sqlite_async/example/with_migrations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,18 @@ class AppDatabase extends _$AppDatabase {

@override
MigrationStrategy get migration {
return MigrationStrategy(
onCreate: (m) async {
// In this example, the schema is managed by Drift
await m.createAll();
},
);
return MigrationStrategy(onCreate: (m) async {
// In this example, the schema is managed by Drift.
// For more options, see:
// https://drift.simonbinder.eu/migrations/#usage
await m.createAll();
});
}
}

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

await db.execute(
'CREATE TABLE IF NOT EXISTS todos(id integer primary key, description text)');

final appdb = AppDatabase(db);

// Watch a query on the Drift database
Expand Down
131 changes: 131 additions & 0 deletions packages/drift_sqlite_async/example/with_migrations.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion packages/drift_sqlite_async/lib/src/executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class _SqliteAsyncDelegate extends _SqliteAsyncQueryDelegate
implements DatabaseDelegate {
final SqliteConnection db;
bool _closed = false;
bool _calledOpen = false;

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

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

@override
bool get isOpen => !db.closed && !_closed;
bool get isOpen => !db.closed && !_closed && _calledOpen;

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

@override
Expand Down
4 changes: 2 additions & 2 deletions packages/drift_sqlite_async/test/db_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void main() {
// Drift may or may not emit duplicate update notifications.
// We use distinct() to ignore those.
.distinct()
.skipWhile((e) => e.isEmpty)
.skipWhile((e) => e == '[]')
.take(3)
.toList();

Expand Down Expand Up @@ -83,7 +83,7 @@ void main() {
var resultsPromise = stream
.map((rows) => rows.toString())
.distinct()
.skipWhile((e) => e.isEmpty)
.skipWhile((e) => e == '[]')
.take(3)
.toList();

Expand Down
13 changes: 13 additions & 0 deletions packages/drift_sqlite_async/test/generated/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ class TodoDatabase extends _$TodoDatabase {
@override
int get schemaVersion => 1;
}

class TodosMigrationDatabase extends TodoDatabase {
TodosMigrationDatabase(SqliteConnection db) : super(db);

@override
MigrationStrategy get migration {
return MigrationStrategy(
onCreate: (m) async {
await m.createAll();
},
);
}
}
Loading
Loading