Fix zoned executors messing with other databases

This commit is contained in:
Simon Binder 2022-03-01 20:40:10 +01:00
parent cb46e2c07e
commit 7e798572a0
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 24 additions and 1 deletions

View File

@ -84,7 +84,13 @@ abstract class DatabaseConnectionUser {
/// but run them in a transaction-specific executor.
@internal
DatabaseConnectionUser get resolvedEngine {
return (Zone.current[_zoneRootUserKey] as DatabaseConnectionUser?) ?? this;
final fromZone = Zone.current[_zoneRootUserKey] as DatabaseConnectionUser?;
if (fromZone != null && fromZone.attachedDatabase == attachedDatabase) {
return fromZone;
} else {
return this;
}
}
/// Marks the [tables] as updated.

View File

@ -93,4 +93,21 @@ void main() {
final db = TodoDb(executor);
expect(db.customSelect('SELECT 1').getSingle(), throwsA('error'));
});
test('zone database is ignored for operations on another database', () async {
final ex1 = MockExecutor();
final ex2 = MockExecutor();
final db1 = TodoDb(ex1);
final db2 = TodoDb(ex2);
addTearDown(db1.close);
addTearDown(db2.close);
await db1.transaction(() async {
await db2.customSelect('SELECT 1').get();
});
verify(ex2.runSelect('SELECT 1', []));
verifyNever(ex2.runSelect(any, any));
});
}