Save database after `RETURNING` write (#2206)

This commit is contained in:
Simon Binder 2022-12-18 20:42:29 +01:00
parent e2265eb597
commit 4644bce9dd
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 23 additions and 1 deletions

View File

@ -181,7 +181,7 @@ class _WebDelegate extends DatabaseDelegate {
}
@override
Future<QueryResult> runSelect(String statement, List<Object?> args) {
Future<QueryResult> runSelect(String statement, List<Object?> args) async {
_checkArgs(args);
// todo at least for stream queries we should cache prepared statements.
final stmt = _db.prepare(statement)..executeWith(args);
@ -197,6 +197,11 @@ class _WebDelegate extends DatabaseDelegate {
columnNames ??= []; // assume no column names when there were no rows
stmt.free();
if (statement.contains('RETURNING')) {
await _handlePotentialUpdate();
}
return Future.value(QueryResult(columnNames, rows));
}

View File

@ -64,4 +64,21 @@ void main() {
final results = await db.customSelect('SELECT * FROM x1;').get();
expect(results.length, 1);
});
test('saves after returning', () async {
final executor = WebExecutor();
var db = Database(executor.createConnection());
addTearDown(() => executor.clearDatabaseAndClose(db));
await db.users.insertReturning(
UsersCompanion.insert(name: 'my new user', birthDate: DateTime.now()));
await db.close();
// Open a new database, the user should exist
db = Database(executor.createConnection());
final users = await db.users.select().get();
expect(users,
contains(isA<User>().having((e) => e.name, 'name', 'my new user')));
});
}