Skip to content

Use Drift and SQLite for data storage #22

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

Merged
merged 11 commits into from
Apr 27, 2023
Merged

Use Drift and SQLite for data storage #22

merged 11 commits into from
Apr 27, 2023

Conversation

gnprice
Copy link
Member

@gnprice gnprice commented Mar 3, 2023

Fixes #13.
Fixes #34.


originally:

Toward #13.

This installs Drift as a dependency, sets up a schema for an accounts table, and performs a migration on that table. It also adds tests for the database schema and the migration, to let us see how those look.

I won't want to merge something like this until I've tested it more end-to-end, which will require some other work including #20 and #21. But I think the branch is otherwise ready, apart from a couple of polish issues.

@gnprice gnprice marked this pull request as draft March 3, 2023 02:14
@gnprice
Copy link
Member Author

gnprice commented Apr 13, 2023

I've rebased this a couple of times as the related work has progressed, and made some further revisions earlier this week. Just pushed the current version.

This still needs end-to-end testing before I'd want to merge it. That's a lot closer now that #11 is done, and #55. The end-to-end testing will basically consist of implementing #34, storing login credentials.

gnprice added 9 commits April 19, 2023 17:28
Just the other day we were talking about this get-server-settings
property, and I explained why it's important for a mobile client
to use it:
  https://chat.zulip.org/#narrow/stream/378-api-design/topic/uri.20to.20url.20compatibility/near/1549242

But apparently when I wrote this code a couple of weeks earlier,
I'd forgotten about that, oops.  Fix it.

It's particularly useful that we fix this before we start keeping
the list of accounts in a database, so that there's no legacy of
some people's devices having a non-canonicalized URL lying around.
This removes the one place where we really use the fact that
we've had Account as a subtype of Auth.  We'll be giving up
that subtyping as part of getting Account from a database ORM.
This frees us up to switch to an Account type that's generated
to correspond to a database table.
This will let LiveGlobalStore and TestGlobalStore diverge in how
the data is ultimately stored and how the account IDs are assigned,
while sharing the code to maintain the in-Dart cache.
Also add a .gitattributes file, excluding `*.g.dart` files from
diffs.  We didn't really need this when the only such files were
the JSON serialization/deserialization files in the API bindings,
because those are pretty compact; but this generated ORM code is
much longer and makes it more necessary.
This will keep things simple when we start storing these
in the database.
@gnprice gnprice changed the title [wip] Try Drift for data storage Use Drift and SQLite for data storage Apr 20, 2023
@gnprice gnprice marked this pull request as ready for review April 20, 2023 01:00
@gnprice
Copy link
Member Author

gnprice commented Apr 20, 2023

OK! This is now tested end to end, and I think it's complete. Please take a look.

Copy link
Collaborator

@chrisbobbe chrisbobbe left a comment

Choose a reason for hiding this comment

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

Exciting! 🎉

See one comment below, then please merge at will.

Comment on lines +25 to +29
@override
List<Set<Column<Object>>> get uniqueKeys => [
{realmUrl, userId},
{realmUrl, email},
];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could add a TODO in lib/widgets/login.dart to handle exceptions thrown because of this, or maybe check GlobalStore.accountIds before sending a bad insert to the database.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good thought. I added one TODO(log) to log database errors (which we should do in a general way down in the Drift layer), and also a TODO(#35) in lib/widgets/login.dart to give the user feedback if the account is a duplicate.

gnprice added 2 commits April 26, 2023 20:36
We didn't really need to do this as a migration, because we
already know we need this column and so we could have included it
from the start.  But this lets us exercise Drift's support for
migrations, and for testing migrations.
@gnprice gnprice merged commit 066fe25 into zulip:main Apr 27, 2023
@gnprice
Copy link
Member Author

gnprice commented Apr 27, 2023

Thanks for the review! Done.

@gnprice gnprice deleted the pr-drift branch April 27, 2023 03:42
gnprice added a commit to gnprice/zulip-flutter that referenced this pull request May 30, 2023
This happens automatically if you rerun build_runner.  Seems like
its omission was basically a mismerge between 901a99e, when it
was merged early from zulip#84:
  zulip#84 (comment)
and 780b092 / zulip#22, which added this file.

Chalk it up as another occasion where zulip#60 would have helped keep
things clean.
gnprice added a commit to gnprice/zulip-flutter that referenced this pull request May 31, 2023
This happens automatically if you rerun build_runner.  Seems like
its omission was basically a mismerge between 901a99e, when it
was merged early from zulip#84:
  zulip#84 (comment)
and 780b092 / zulip#22, which added this file.

Chalk it up as another occasion where zulip#60 would have helped keep
things clean.
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.

Store login credentials on device Store some user data locally
2 participants