@@ -5,9 +5,21 @@ import 'package:sqlite3/common.dart';
5
5
6
6
import '../log.dart' ;
7
7
import 'schema_versions.g.dart' ;
8
+ import 'settings.dart' ;
8
9
9
10
part 'database.g.dart' ;
10
11
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
+
11
23
/// The table of [Account] records in the app's database.
12
24
class Accounts extends Table {
13
25
/// The ID of this account in the app's local database.
@@ -59,12 +71,14 @@ VersionedSchema _getSchema({
59
71
switch (schemaVersion) {
60
72
case 2 :
61
73
return Schema2 (database: database);
74
+ case 3 :
75
+ return Schema3 (database: database);
62
76
default :
63
77
throw Exception ('unknown schema version: $schemaVersion ' );
64
78
}
65
79
}
66
80
67
- @DriftDatabase (tables: [Accounts ])
81
+ @DriftDatabase (tables: [GlobalSettings , Accounts ])
68
82
class AppDatabase extends _$AppDatabase {
69
83
AppDatabase (super .e);
70
84
@@ -79,7 +93,7 @@ class AppDatabase extends _$AppDatabase {
79
93
// * Write a migration in `onUpgrade` below.
80
94
// * Write tests.
81
95
@override
82
- int get schemaVersion => 2 ; // See note.
96
+ int get schemaVersion => 3 ; // See note.
83
97
84
98
Future <void > _dropAndCreateAll (Migrator m, {
85
99
required int schemaVersion,
@@ -128,10 +142,32 @@ class AppDatabase extends _$AppDatabase {
128
142
from1To2: (m, schema) async {
129
143
await m.addColumn (schema.accounts, schema.accounts.ackedPushToken);
130
144
},
145
+ from2To3: (m, schema) async {
146
+ await m.createTable (schema.globalSettings);
147
+ },
131
148
));
132
149
});
133
150
}
134
151
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
+
135
171
Future <int > createAccount (AccountsCompanion values) async {
136
172
try {
137
173
return await into (accounts).insert (values);
0 commit comments