@@ -5,9 +5,21 @@ import 'package:sqlite3/common.dart';
55
66import '../log.dart' ;
77import 'schema_versions.g.dart' ;
8+ import 'settings.dart' ;
89
910part 'database.g.dart' ;
1011
12+ /// The table of the user's chosen settings independent of account, on this
13+ /// client.
14+ ///
15+ /// These apply across all the user's accounts on this client (i.e. on this
16+ /// install of the app on this device).
17+ @DataClassName ('GlobalSettingsData' )
18+ class GlobalSettings extends Table {
19+ Column <String > get themeSetting => textEnum <ThemeSetting >()
20+ .nullable ()();
21+ }
22+
1123/// The table of [Account] records in the app's database.
1224class Accounts extends Table {
1325 /// The ID of this account in the app's local database.
@@ -59,12 +71,14 @@ VersionedSchema _getSchema({
5971 switch (schemaVersion) {
6072 case 2 :
6173 return Schema2 (database: database);
74+ case 3 :
75+ return Schema3 (database: database);
6276 default :
6377 throw Exception ('unknown schema version: $schemaVersion ' );
6478 }
6579}
6680
67- @DriftDatabase (tables: [Accounts ])
81+ @DriftDatabase (tables: [GlobalSettings , Accounts ])
6882class AppDatabase extends _$AppDatabase {
6983 AppDatabase (super .e);
7084
@@ -79,7 +93,7 @@ class AppDatabase extends _$AppDatabase {
7993 // * Write a migration in `onUpgrade` below.
8094 // * Write tests.
8195 @override
82- int get schemaVersion => 2 ; // See note.
96+ int get schemaVersion => 3 ; // See note.
8397
8498 Future <void > _dropAndCreateAll (Migrator m, {
8599 required int schemaVersion,
@@ -128,10 +142,32 @@ class AppDatabase extends _$AppDatabase {
128142 from1To2: (m, schema) async {
129143 await m.addColumn (schema.accounts, schema.accounts.ackedPushToken);
130144 },
145+ from2To3: (m, schema) async {
146+ await m.createTable (schema.globalSettings);
147+ },
131148 ));
132149 });
133150 }
134151
152+ Future <GlobalSettingsData > ensureGlobalSettings () async {
153+ final settings = await select (globalSettings).get ();
154+ // TODO(db): Enforce the singleton constraint more robustly.
155+ if (settings.isNotEmpty) {
156+ if (settings.length > 1 ) {
157+ assert (debugLog ('Expected one globalSettings, got multiple: $settings ' ));
158+ }
159+ return settings.first;
160+ }
161+
162+ final rowsAffected = await into (globalSettings).insert (GlobalSettingsCompanion .insert ());
163+ assert (rowsAffected == 1 );
164+ final result = await select (globalSettings).get ();
165+ if (result.length > 1 ) {
166+ assert (debugLog ('Expected one globalSettings, got multiple: $result ' ));
167+ }
168+ return result.first;
169+ }
170+
135171 Future <int > createAccount (AccountsCompanion values) async {
136172 try {
137173 return await into (accounts).insert (values);
0 commit comments