Skip to content
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

Fix integer nullable cast in protocol serializer #2707

Conversation

Brunobnahorny
Copy link
Contributor

Hi. I'm using the package flutter_foreground_task to generate a background flutter engine attached to an Native Notification.
I setup the isolates as below(btw i will try to sugest improvements in the documentation for this case).

LazyDatabase _openConnection() {
  return LazyDatabase(() async {
    final dbFolder = await getApplicationDocumentsDirectory();
    final file = File(path.join(dbFolder.path, 'dbname.sqlite'));

    final isolate = await DriftIsolate.spawn(
        () => LazyDatabase(() => NativeDatabase(file)));

    IsolateNameServer.registerPortWithName(
      isolate.connectPort,
      DriftDb.databasePortName,
    );

    return DatabaseConnection.delayed(
      DriftIsolate.fromConnectPort(isolate.connectPort).connect(),
    );
  });
}

QueryExecutor? _tryConnectToDriftIsolate() {
  final connectPort =
      IsolateNameServer.lookupPortByName(DriftDb.databasePortName);

  if (connectPort != null) {
    return DatabaseConnection.delayed(
      DriftIsolate.fromConnectPort(
        connectPort,
      ).connect(
        singleClientMode: true,
      ),
    );
  }

  return null;
}


class DriftDb extends _$DriftDb {
  // we tell the database where to store the data with this constructor
  DriftDb([QueryExecutor? executor])
      : super(_tryConnectToDriftIsolate() ?? _openConnection());
}

ps: i will improve the lookupPortByName after

I was getting this error for the cast specific on this line.
Since the class ExecuteBatchedStatement accepts nullable executorId it looks like it should cast to nullable int.

It only occours on connecting with serialize: true.

Since the class ExecuteBatchedStatement accepts nullable executorId it looks like it should cast to nullable int.

It occours using Isolate from multiple Flutter engines.
@simolus3
Copy link
Owner

simolus3 commented Nov 4, 2023

Thanks for the fix. I think it would be good to have a test for this as well, I think this should cover it (at least it fails at the moment :D)

diff --git a/drift/test/remote_test.dart b/drift/test/remote_test.dart
index fba747e5..d9845fc4 100644
--- a/drift/test/remote_test.dart
+++ b/drift/test/remote_test.dart
@@ -149,6 +149,15 @@ void main() {
           (e) => e.remoteCause, 'remoteCause', 'UnimplementedError: error!')),
     );
 
+    final statements =
+        BatchedStatements(['SELECT 1'], [ArgumentsForBatchedStatement(0, [])]);
+    when(executor.runBatched(any)).thenAnswer((i) => Future.value());
+    // Not using db.batch because that starts a transaction, we want to test
+    // this working with the default executor.
+    // Regression test for: https://github.com/simolus3/drift/pull/2707
+    await db.executor.runBatched(statements);
+    verify(executor.runBatched(statements));
+
     await db.close();
   });
 

@Brunobnahorny
Copy link
Contributor Author

Commited the test as mentioned, all drift/test passed. Thanks for the fast reply!

Copy link
Owner

@simolus3 simolus3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@simolus3 simolus3 merged commit 5426ad8 into simolus3:develop Nov 5, 2023
10 checks passed
@simolus3
Copy link
Owner

simolus3 commented Nov 5, 2023

Released with drift: ^2.13.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants