Skip to content

Commit 3d34040

Browse files
committed
Added refreshSchema() to SqliteConnection interface.
1 parent 9428ad9 commit 3d34040

9 files changed

+48
-1
lines changed

packages/sqlite_async/CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
## 0.8.2
2+
3+
- Added `refreshSchema()` to `SqliteConnection` and its implementations, allowing queries and watch calls to work against update schemas.
4+
15
## 0.8.1
26

3-
- Added Navigator locks for web `Mutex`s.
7+
- Added Navigator locks for web `Mutex`s.
48

59
## 0.8.0
610

packages/sqlite_async/lib/src/common/connection/sync_sqlite_connection.dart

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class SyncSqliteConnection extends SqliteConnection with SqliteQueries {
5050
Future<bool> getAutoCommit() async {
5151
return db.autocommit;
5252
}
53+
54+
@override
55+
Future<void> refreshSchema() async {
56+
db.execute("PRAGMA table_info('sqlite_master')");
57+
}
5358
}
5459

5560
class SyncReadContext implements SqliteReadContext {

packages/sqlite_async/lib/src/impl/stub_sqlite_database.dart

+5
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ class SqliteDatabaseImpl
6464
Future<bool> getAutoCommit() {
6565
throw UnimplementedError();
6666
}
67+
68+
@override
69+
Future<void> refreshSchema() {
70+
throw UnimplementedError();
71+
}
6772
}

packages/sqlite_async/lib/src/native/database/connection_pool.dart

+9
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
221221
// read-only connections first.
222222
await _writeConnection?.close();
223223
}
224+
225+
@override
226+
Future<void> refreshSchema() async {
227+
final toRefresh = _allReadConnections.toList();
228+
229+
for (var connection in toRefresh) {
230+
await connection.refreshSchema();
231+
}
232+
}
224233
}
225234

226235
typedef ReadCallback<T> = Future<T> Function(SqliteReadContext tx);

packages/sqlite_async/lib/src/native/database/native_sqlite_connection_impl.dart

+5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ class SqliteConnectionImpl
158158
});
159159
}, timeout: lockTimeout);
160160
}
161+
162+
@override
163+
Future<void> refreshSchema() async {
164+
await get("PRAGMA table_info('sqlite_master')");
165+
}
161166
}
162167

163168
int _nextCtxId = 1;

packages/sqlite_async/lib/src/native/database/native_sqlite_database.dart

+5
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,9 @@ class SqliteDatabaseImpl
166166
readOnly: false,
167167
openFactory: openFactory);
168168
}
169+
170+
@override
171+
Future<void> refreshSchema() async {
172+
await _pool.refreshSchema();
173+
}
169174
}

packages/sqlite_async/lib/src/sqlite_connection.dart

+4
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ abstract class SqliteConnection extends SqliteWriteContext {
128128
Future<T> writeLock<T>(Future<T> Function(SqliteWriteContext tx) callback,
129129
{Duration? lockTimeout, String? debugContext});
130130

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

133137
/// Returns true if the connection is closed

packages/sqlite_async/lib/src/web/database.dart

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ class WebDatabase
129129
}
130130
}
131131
}
132+
133+
@override
134+
Future<void> refreshSchema() async {
135+
_database.execute("PRAGMA table_info('sqlite_master')");
136+
}
132137
}
133138

134139
class _SharedContext implements SqliteReadContext {

packages/sqlite_async/lib/src/web/database/web_sqlite_database.dart

+5
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,9 @@ class SqliteDatabaseImpl
139139
await isInitialized;
140140
return _connection.getAutoCommit();
141141
}
142+
143+
@override
144+
Future<void> refreshSchema() async {
145+
await _connection.refreshSchema();
146+
}
142147
}

0 commit comments

Comments
 (0)