@@ -5,6 +5,7 @@ import 'package:sqlite3/common.dart';
55
66import '../log.dart' ;
77import 'schema_versions.g.dart' ;
8+ import 'settings.dart' ;
89
910part 'database.g.dart' ;
1011
@@ -45,6 +46,17 @@ class Accounts extends Table {
4546 ];
4647}
4748
49+ /// The table of the user's chosen settings independent of account, on this
50+ /// client.
51+ ///
52+ /// These apply across all the user's accounts on this client (i.e. on this
53+ /// install of the app on this device).
54+ @DataClassName ('GlobalSettingsData' )
55+ class GlobalSettings extends Table {
56+ Column <String > get themeSetting => textEnum <ThemeSetting >()
57+ .nullable ()();
58+ }
59+
4860class UriConverter extends TypeConverter <Uri , String > {
4961 const UriConverter ();
5062 @override String toSql (Uri value) => value.toString ();
@@ -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,6 +142,9 @@ 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 }
@@ -147,6 +164,25 @@ class AppDatabase extends _$AppDatabase {
147164 rethrow ;
148165 }
149166 }
167+
168+ Future <GlobalSettingsData > ensureGlobalSettings () async {
169+ final settings = await select (globalSettings).get ();
170+ // TODO(db): Enforce the singleton constraint more robustly.
171+ if (settings.isNotEmpty) {
172+ if (settings.length > 1 ) {
173+ assert (debugLog ('Expected one globalSettings, got multiple: $settings ' ));
174+ }
175+ return settings.first;
176+ }
177+
178+ final rowsAffected = await into (globalSettings).insert (GlobalSettingsCompanion .insert ());
179+ assert (rowsAffected == 1 );
180+ final result = await select (globalSettings).get ();
181+ if (result.length > 1 ) {
182+ assert (debugLog ('Expected one globalSettings, got multiple: $result ' ));
183+ }
184+ return result.first;
185+ }
150186}
151187
152188class AccountAlreadyExistsException implements Exception {}
0 commit comments