Skip to content

Commit 7bb71be

Browse files
committed
Removed exclusiveLock, simplified implementation of refreshSchema.
1 parent 03bca29 commit 7bb71be

File tree

5 files changed

+20
-80
lines changed

5 files changed

+20
-80
lines changed

packages/sqlite_async/CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## 0.8.2
22

3-
- Added `refreshSchema()` and `exclusiveLock()` to `SqliteDatabaseMixin`, allowing queries and watch calls to work against updated schemas.
3+
- Added `refreshSchema()`, allowing queries and watch calls to work against updated schemas.
44

55
## 0.8.1
66

packages/sqlite_async/lib/src/common/sqlite_database.dart

-15
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,6 @@ mixin SqliteDatabaseMixin implements SqliteConnection, SqliteQueries {
3838
///
3939
/// Use this to access the database in background isolates.
4040
IsolateConnectionFactory isolateConnectionFactory();
41-
42-
/// TODO Improve on this definition by supporting a writeable context.
43-
Future<void> exclusiveLock<T>(
44-
Future<T> Function(SqliteReadContext ctx) callback,
45-
) {
46-
return writeLock(callback);
47-
}
48-
49-
/// Ensures that all connections are aware of the latest schema changes applied (if any).
50-
/// Queries and watch calls can potentially use outdated schema information after a schema update.
51-
Future<void> refreshSchema() {
52-
return exclusiveLock((ctx) async {
53-
return ctx.get("PRAGMA table_info('sqlite_master')");
54-
});
55-
}
5641
}
5742

5843
/// A SQLite database instance.

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

+11-61
Original file line numberDiff line numberDiff line change
@@ -54,67 +54,6 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
5454
_writeConnection?.updates?.forEach(updatesController.add);
5555
}
5656

57-
/// Executes a provided callback function exclusively across all read and
58-
/// write connections in the pool.
59-
///
60-
/// This function first locks all read and write connections, collecting their
61-
/// contexts. It then executes the provided [callback] function on each of these
62-
/// contexts. After the [callback] completes for each context, the locks are released.
63-
///
64-
/// Example usage:
65-
/// ```dart
66-
/// await runExclusive((ctx) async {
67-
/// // Perform some database operation with the ctx
68-
/// await ctx.execute('PRAGMA schema_version');
69-
/// });
70-
/// ```
71-
exclusiveLock<T>(
72-
Future<T> Function(SqliteReadContext tx) callback,
73-
) async {
74-
final List<Completer<SqliteReadContext>> completers = [];
75-
final List<Completer<void>> releasers = [];
76-
77-
for (final read in _allReadConnections) {
78-
final completer = Completer<SqliteReadContext>();
79-
80-
completers.add(completer);
81-
read.readLock((ctx) async {
82-
completer.complete(ctx);
83-
84-
final releaser = Completer();
85-
releasers.add(releaser);
86-
87-
// Keep this active, close the context when finished
88-
await releaser.future;
89-
});
90-
}
91-
92-
final writeCompleter = Completer<SqliteReadContext>();
93-
completers.add(writeCompleter);
94-
_writeConnection?.writeLock((ctx) async {
95-
writeCompleter.complete(ctx);
96-
97-
final releaser = Completer();
98-
releasers.add(releaser);
99-
await releaser.future;
100-
});
101-
102-
// Get all the connection contexts and execute the callback on each of them
103-
final List<SqliteReadContext> contexts = [];
104-
for (final completer in completers) {
105-
contexts.add(await completer.future);
106-
}
107-
108-
for (final c in contexts) {
109-
await callback(c);
110-
}
111-
112-
// Release all the releasers
113-
for (final r in releasers) {
114-
r.complete();
115-
}
116-
}
117-
11857
/// Returns true if the _write_ connection is currently in autocommit mode.
11958
@override
12059
Future<bool> getAutoCommit() async {
@@ -282,6 +221,17 @@ class SqliteConnectionPool with SqliteQueries implements SqliteConnection {
282221
// read-only connections first.
283222
await _writeConnection?.close();
284223
}
224+
225+
@override
226+
Future<void> refreshSchema() async {
227+
final toRefresh = _allReadConnections.toList();
228+
229+
await _writeConnection?.refreshSchema();
230+
231+
for (var connection in toRefresh) {
232+
await connection.refreshSchema();
233+
}
234+
}
285235
}
286236

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

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ class SqliteDatabaseImpl
168168
}
169169

170170
@override
171-
Future<void> exclusiveLock<T>(
172-
Future<T> Function(SqliteReadContext ctx) callback) {
173-
return _pool.exclusiveLock(callback);
171+
Future<void> refreshSchema() {
172+
return _pool.refreshSchema();
174173
}
175174
}

packages/sqlite_async/lib/src/sqlite_queries.dart

+6
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,10 @@ mixin SqliteQueries implements SqliteWriteContext, SqliteConnection {
137137
return tx.executeBatch(sql, parameterSets);
138138
});
139139
}
140+
141+
/// Ensures that all connections are aware of the latest schema changes applied (if any).
142+
/// Queries and watch calls can potentially use outdated schema information after a schema update.
143+
Future<void> refreshSchema() {
144+
return get("PRAGMA table_info('sqlite_master')");
145+
}
140146
}

0 commit comments

Comments
 (0)