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

New SQLite driver: capacitor-sqlite #484

Merged
merged 27 commits into from
Oct 5, 2023
Merged

Conversation

gregzo
Copy link
Contributor

@gregzo gregzo commented Sep 22, 2023

This PR adds compatibility with capacitor-community/sqlite via a new adapter and accompanying plumbing and tests.

We could envisage asking capacitor-community/sqlite's maintainer to have a quick look to ensure that there's no flagrant misuse of its APIs. As it stands, all tests pass but using a mock database.

This adapter implementation is built on my understanding of electric and capacitor-community/sqlite documentation and codebase. Much more extensive real-world testing is imo required.

Copy link
Contributor

@kevin-dp kevin-dp left a comment

Choose a reason for hiding this comment

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

Thanks again for your contribution!
I reviewed your PR and mostly have minor comments to improve readability. However, i do have one major concern. The transaction<T> method of the adapter is managing the transaction manually by calling BEGIN and COMMIT/ABORT.

This is prone to concurrency issues if one would call transaction<T> before the previous transaction finished; in which case the transactions will get interleaved. Even if we assume that clients won't do that, a similar issue could still occur when clients call transaction<T> (e.g. as a result of calling db.items.create) and concurrently there is a transaction coming in via the replication stream and being applied.

We've had similar issues with the wa-sqlite driver and solved them by using a mutex as to ensure that the adapter never tries to run two transactions concurrently. I suggest you have a look at the implementation of transaction<T> in the wa-sqlite driver.

clients/typescript/src/drivers/capacitor-sqlite/adapter.ts Outdated Show resolved Hide resolved
clients/typescript/src/drivers/capacitor-sqlite/adapter.ts Outdated Show resolved Hide resolved
const wrapInTransaction = true;

return this.db.executeSet(set, wrapInTransaction).then( (result: capSQLiteChanges) => {
// TODO: unsure how capacitor-sqlite populates the changes value (additive?), and what is expected of electric here.
Copy link
Contributor

Choose a reason for hiding this comment

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

We expect the changes to be additive, i.e. the total amount of changes accumulated over all the statements in this transaction. For example, in the better-sqlite3 adapter we have to accumulate those changes manually. Could you write a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd need to write a test against a real db, not the mock. What I can do for now is ask @samwillis to double check this in his example project.

clients/typescript/src/drivers/capacitor-sqlite/adapter.ts Outdated Show resolved Hide resolved
clients/typescript/src/drivers/capacitor-sqlite/adapter.ts Outdated Show resolved Hide resolved
clients/typescript/src/drivers/capacitor-sqlite/adapter.ts Outdated Show resolved Hide resolved
clients/typescript/src/drivers/capacitor-sqlite/adapter.ts Outdated Show resolved Hide resolved
clients/typescript/src/drivers/capacitor-sqlite/mock.ts Outdated Show resolved Hide resolved
clients/typescript/src/drivers/capacitor-sqlite/mock.ts Outdated Show resolved Hide resolved
clients/typescript/src/drivers/capacitor-sqlite/mock.ts Outdated Show resolved Hide resolved
@gregzo
Copy link
Contributor Author

gregzo commented Sep 29, 2023

All requested changes implemented, all tests still pass. Thank you for the review! The only change that is more than a stylistic or readability refactor is the mutex implementation. I've left that conversation unresolved.

@samwillis
Copy link
Contributor

Hey @gregzo, good news, with the two small changes I've comment on this worked with my demo app (I've tested iOS but not android yet, but I don't think that will change anything) 😁

It looks like you also have an issue with pnpm-lock.yaml not being top to date (see the failed CI tests), I think all you need to do is run pnpm install to update it.

@gregzo
Copy link
Contributor Author

gregzo commented Oct 3, 2023

@samwillis pnpm-lock now up to date. FYI ran into this known pnpm issue: pnpm/pnpm#1801
Manually created missing folders and js files to work around it.

Copy link
Contributor

@kevin-dp kevin-dp left a comment

Choose a reason for hiding this comment

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

Good job @gregzo!
Driver looks great, i'm waiting for the tests to finish and will merge afterwards.
Thanks again for your amazing contribution 💯

@kevin-dp
Copy link
Contributor

kevin-dp commented Oct 3, 2023

@gregzo Could you run yarn format in clients/typescript please?

Copy link
Member

@alco alco left a comment

Choose a reason for hiding this comment

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

I see you merged main into your PR branch. We don't do that normally and instead rebase PR branches on top of main.

Please rebase your branch on top of the latest main and remove the merge commit and the Version Packages one. Let me know if you need a hand with that.

clients/typescript/CHANGELOG.md Outdated Show resolved Hide resolved
clients/typescript/package.json Outdated Show resolved Hide resolved
clients/typescript/src/version/index.ts Outdated Show resolved Hide resolved
@alco
Copy link
Member

alco commented Oct 4, 2023

The E2E test failures seem to be caused by the fact that your branch is based on an older version of the server. When you rebase it on top of the latest main, the E2E problems should be resolved.

@gregzo gregzo force-pushed the main branch 3 times, most recently from 4637899 to 8a301ac Compare October 4, 2023 20:19
@alco
Copy link
Member

alco commented Oct 5, 2023

@gregzo Rebase looks great. Thanks!

I was just about to comment about the pnpm lockfile update. I've pushed a commit for it here. But you beat me to it.

Did you also get these warnings when you ran pnpm install?

$ pnpm install
Scope: all 6 workspace projects
clients/typescript                       |  WARN  deprecated @types/react-native-uuid@2.0.0
clients/typescript                       |  WARN  deprecated squel@5.13.0
e2e/satellite_client                     |  WARN  deprecated tslint@6.1.3
e2e/satellite_client                     |  WARN  deprecated @tsmodule/tsmodule@40.19.0
Downloading registry.npmjs.org/typescript/5.1.3: 7.15 MB/7.15 MB, done
Downloading registry.npmjs.org/typescript/4.9.5: 11.62 MB/11.62 MB, done
Downloading registry.npmjs.org/react-native/0.68.0: 21.14 MB/21.14 MB, done
Downloading registry.npmjs.org/hermes-engine/0.11.0: 15.39 MB/15.39 MB, done
Downloading registry.npmjs.org/sql.js/1.8.0: 7.45 MB/7.45 MB, done
Downloading registry.npmjs.org/@stencil/core/4.4.0: 8.75 MB/8.75 MB, done
Downloading registry.npmjs.org/jsc-android/250230.2.1: 36.09 MB/36.09 MB, done
Downloading registry.npmjs.org/dprint-node/1.0.7: 10.55 MB/10.55 MB, done
 WARN  14 deprecated subdependencies found: @babel/plugin-proposal-async-generator-functions@7.20.7, @babel/plugin-proposal-private-property-in-object@7.21.0, @babel/plugin-proposal-unicode-property-regex@7.18.6, @npmcli/move-file@1.1.2, har-validator@5.1.5, request-promise-native@1.0.9, request@2.88.2, resolve-url@0.2.1, source-map-resolve@0.5.3, source-map-url@0.4.1, uglify-es@3.3.9, urix@0.1.0, uuid@3.4.0, w3c-hr-time@1.0.2
Packages: +12
++++++++++++
Progress: resolved 1844, reused 0, downloaded 1759, added 12, done
 WARN  Issues with peer dependencies found
clients/typescript
└─┬ react-native 0.68.0
  ├── ✕ unmet peer react@17.0.2: found 18.2.0
  └─┬ react-shallow-renderer 16.14.1
    └── ✕ unmet peer react@"^16.0.0 || ^17.0.0": found 18.2.0

Done in 34.6s

I'm wondering if there are any fixes we need to apply to our deps. And it's curious that the lockfile version dropped to 6.0 from 6.1, even though I had just updated my pnpm to the latest version yesterday.

@balegas @kevin-dp @samwillis

@gregzo
Copy link
Contributor Author

gregzo commented Oct 5, 2023

The E2E test failures seem to be caused by the fact that your branch is based on an older version of the server. When you rebase it on top of the latest main, the E2E problems should be resolved.

Rebased on electric/main and added changeset. E2E tests still fail though - happy to follow your lead if there's anything else I can do.

Edit: all tests pass. Got 3 failure notifications in my inbox after pushing, among them E2E. All seems well now.

Copy link
Member

@alco alco left a comment

Choose a reason for hiding this comment

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

🏅

@gregzo
Copy link
Contributor Author

gregzo commented Oct 5, 2023

@alco Yes, I encountered the same warnings.

@samwillis samwillis merged commit 3d98c1f into electric-sql:main Oct 5, 2023
11 checks passed
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.

None yet

4 participants