Skip to content

Added refreshSchema() #57

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 9 commits into from
Aug 20, 2024
6 changes: 5 additions & 1 deletion packages/sqlite_async/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## 0.8.2

- Added `refreshSchema()` to `SqliteConnection` and its implementations, allowing queries and watch calls to work against update schemas.

## 0.8.1

- Added Navigator locks for web `Mutex`s.
- Added Navigator locks for web `Mutex`s.

## 0.8.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class SyncSqliteConnection extends SqliteConnection with SqliteQueries {
Future<bool> getAutoCommit() async {
return db.autocommit;
}

@override
Future<void> refreshSchema() async {
db.execute("PRAGMA table_info('sqlite_master')");
}
}

class SyncReadContext implements SqliteReadContext {
Expand Down
5 changes: 5 additions & 0 deletions packages/sqlite_async/lib/src/impl/stub_sqlite_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ class SqliteDatabaseImpl
Future<bool> getAutoCommit() {
throw UnimplementedError();
}

@override
Future<void> refreshSchema() {
throw UnimplementedError();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
// read-only connections first.
await _writeConnection?.close();
}

@override
Future<void> refreshSchema() async {
final toRefresh = _allReadConnections.toList();

for (var connection in toRefresh) {
await connection.refreshSchema();
}
}
}

typedef ReadCallback<T> = Future<T> Function(SqliteReadContext tx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ class SqliteConnectionImpl
});
}, timeout: lockTimeout);
}

@override
Future<void> refreshSchema() async {
await get("PRAGMA table_info('sqlite_master')");
}
}

int _nextCtxId = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,9 @@ class SqliteDatabaseImpl
readOnly: false,
openFactory: openFactory);
}

@override
Future<void> refreshSchema() async {
await _pool.refreshSchema();
}
}
4 changes: 4 additions & 0 deletions packages/sqlite_async/lib/src/sqlite_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ abstract class SqliteConnection extends SqliteWriteContext {
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
{Duration? lockTimeout, String? debugContext});

/// Ensures that all connections are aware of the latest schema changes applied (if any).
/// Queries and watch calls can potentially use outdated schema information after a schema update.
Future<void> refreshSchema();

Future<void> close();

/// Returns true if the connection is closed
Expand Down
5 changes: 5 additions & 0 deletions packages/sqlite_async/lib/src/web/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ class WebDatabase
}
}
}

@override
Future<void> refreshSchema() async {
_database.execute("PRAGMA table_info('sqlite_master')");
}
}

class _SharedContext implements SqliteReadContext {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ class SqliteDatabaseImpl
await isInitialized;
return _connection.getAutoCommit();
}

@override
Future<void> refreshSchema() async {
await _connection.refreshSchema();
}
}
Loading