-
Notifications
You must be signed in to change notification settings - Fork 309
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
91d7102
login: Use canonical realm URL from server
gnprice 8f94abc
api [nfc]: Let authHeader take email and apiKey separately
gnprice 8eea74d
store [nfc]: Make Account not extend Auth
gnprice b3d1af5
store [nfc]: Separate insertAccount from per-impl doInsertAccount
gnprice cf62cc6
deps: Upgrade with `flutter pub upgrade`
gnprice 483a3ef
deps: Add drift, sqlite3_flutter_libs, path_provider, path, dev:drift…
gnprice 780b092
db: Write a first Drift schema
gnprice 9bd35bf
db: Set up for migrations in Drift
gnprice 62c332f
store [nfc]: Use Account type generated for database
gnprice ea14c73
store: Keep login credentials in the database
gnprice 066fe25
db: Add a migration, and a migration test
gnprice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Suppress noisy generated files in diffs. | ||
|
||
# Dart files generated from the files next to them: | ||
*.g.dart -diff | ||
|
||
# Generated files for testing migrations: | ||
test/model/schemas/*.dart -diff | ||
test/model/schemas/*.json -diff | ||
|
||
# On the other hand, keep diffs for pubspec.lock. It contains | ||
# information independent of any non-generated file in the tree. | ||
# And thankfully it's much less verbose than, say, a yarn.lock. | ||
#pubspec.lock -diff |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:drift/drift.dart'; | ||
import 'package:drift/native.dart'; | ||
import 'package:path/path.dart' as path; | ||
import 'package:path_provider/path_provider.dart'; | ||
|
||
part 'database.g.dart'; | ||
|
||
class Accounts extends Table { | ||
Column<int> get id => integer().autoIncrement()(); | ||
|
||
Column<String> get realmUrl => text().map(const UriConverter())(); | ||
Column<int> get userId => integer()(); | ||
|
||
Column<String> get email => text()(); | ||
Column<String> get apiKey => text()(); | ||
|
||
Column<String> get zulipVersion => text()(); | ||
Column<String> get zulipMergeBase => text().nullable()(); | ||
Column<int> get zulipFeatureLevel => integer()(); | ||
|
||
Column<String> get ackedPushToken => text().nullable()(); | ||
|
||
@override | ||
List<Set<Column<Object>>> get uniqueKeys => [ | ||
{realmUrl, userId}, | ||
{realmUrl, email}, | ||
]; | ||
} | ||
|
||
class UriConverter extends TypeConverter<Uri, String> { | ||
const UriConverter(); | ||
@override String toSql(Uri value) => value.toString(); | ||
@override Uri fromSql(String fromDb) => Uri.parse(fromDb); | ||
} | ||
|
||
LazyDatabase _openConnection() { | ||
return LazyDatabase(() async { | ||
// TODO decide if this path is the right one to use | ||
final dbFolder = await getApplicationDocumentsDirectory(); | ||
final file = File(path.join(dbFolder.path, 'db.sqlite')); | ||
return NativeDatabase.createInBackground(file); | ||
}); | ||
} | ||
|
||
@DriftDatabase(tables: [Accounts]) | ||
class AppDatabase extends _$AppDatabase { | ||
AppDatabase(QueryExecutor e) : super(e); | ||
|
||
AppDatabase.live() : this(_openConnection()); | ||
|
||
// When updating the schema: | ||
// * Make the change in the table classes, and bump schemaVersion. | ||
// * Export the new schema: | ||
// $ dart run drift_dev schema dump lib/model/database.dart test/model/schemas/ | ||
// * Generate test migrations from the schemas: | ||
// $ dart run drift_dev schema generate --data-classes --companions test/model/schemas/ test/model/schemas/ | ||
// * Write a migration in `onUpgrade` below. | ||
// * Write tests. | ||
// TODO run those `drift_dev schema` commands in CI: https://github.com/zulip/zulip-flutter/issues/60 | ||
@override | ||
int get schemaVersion => 2; // See note. | ||
|
||
@override | ||
MigrationStrategy get migration { | ||
return MigrationStrategy( | ||
onCreate: (Migrator m) async { | ||
await m.createAll(); | ||
}, | ||
onUpgrade: (Migrator m, int from, int to) async { | ||
if (from > to) { | ||
// TODO(log): log schema downgrade as an error | ||
// This should only ever happen in dev. As a dev convenience, | ||
// drop everything from the database and start over. | ||
for (final entity in allSchemaEntities) { | ||
// This will miss any entire tables (or indexes, etc.) that | ||
// don't exist at this version. For a dev-only feature, that's OK. | ||
await m.drop(entity); | ||
} | ||
await m.createAll(); | ||
return; | ||
} | ||
assert(1 <= from && from <= to && to <= schemaVersion); | ||
|
||
if (from < 2 && 2 <= to) { | ||
await m.addColumn(accounts, accounts.ackedPushToken); | ||
} | ||
// New migrations go here. | ||
} | ||
); | ||
} | ||
|
||
Future<int> createAccount(AccountsCompanion values) { | ||
return into(accounts).insert(values); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.