Skip to content

Commit e50b68d

Browse files
committed
settings: Make general bool global settings, so as to add without migrations
This new table to read looks to add something like 5-8ms to the app's startup time. Not nothing; but not a big cost compared to the time we spend loading server data right afterward. Specifically, results on my Pixel 8 (compare to those in the previous commit): db load time 117.9ms total: 15.2ms init, 89.2ms settings, 5.1ms bool-settings, 8.3ms accounts db load time 90.9ms total: 1.3ms init, 78.5ms settings, 8.3ms bool-settings, 2.8ms accounts db load time 87.2ms total: 1.4ms init, 71.8ms settings, 5.9ms bool-settings, 8.1ms accounts db load time 85.7ms total: 1.2ms init, 72.8ms settings, 4.9ms bool-settings, 6.8ms accounts db load time 91.3ms total: 1.4ms init, 80.0ms settings, 7.5ms bool-settings, 2.5ms accounts db load time 83.8ms total: 1.1ms init, 70.0ms settings, 5.0ms bool-settings, 7.7ms accounts
1 parent 85df9d7 commit e50b68d

13 files changed

+1684
-11
lines changed

lib/model/database.dart

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,44 @@ class GlobalSettings extends Table {
2323

2424
Column<String> get browserPreference => textEnum<BrowserPreference>()
2525
.nullable()();
26+
27+
// If adding a new column to this table, consider whether [BoolGlobalSettings]
28+
// can do the job instead (by adding a value to the [BoolGlobalSetting] enum).
29+
// That way is more convenient, when it works, because
30+
// it avoids a migration and therefore several added copies of our schema
31+
// in the Drift generated files.
32+
}
33+
34+
/// The table of the user's bool-valued, account-independent settings.
35+
///
36+
/// These apply across all the user's accounts on this client
37+
/// (i.e. on this install of the app on this device).
38+
///
39+
/// Each row is a [BoolGlobalSettingRow],
40+
/// referring to a possible setting from [BoolGlobalSetting].
41+
/// For settings in [BoolGlobalSetting] without a row in this table,
42+
/// the setting's value is that of [BoolGlobalSetting.default_].
43+
@DataClassName('BoolGlobalSettingRow')
44+
class BoolGlobalSettings extends Table {
45+
/// The setting's name, a possible name from [BoolGlobalSetting].
46+
///
47+
/// The table may have rows where [name] is not the name of any
48+
/// enum value in [BoolGlobalSetting].
49+
/// This happens if the app has previously run at a future or modified
50+
/// version which had additional values in that enum,
51+
/// and the user set one of those additional settings.
52+
/// The app ignores any such unknown rows.
53+
Column<String> get name => text()();
54+
55+
/// The user's chosen value for the setting.
56+
///
57+
/// This is non-nullable; if the user wants to revert to
58+
/// following the app's default for the setting,
59+
/// that can be expressed by deleting the row.
60+
Column<bool> get value => boolean()();
61+
62+
@override
63+
Set<Column<Object>>? get primaryKey => {name};
2664
}
2765

2866
/// The table of [Account] records in the app's database.
@@ -82,12 +120,14 @@ VersionedSchema _getSchema({
82120
return Schema4(database: database);
83121
case 5:
84122
return Schema5(database: database);
123+
case 6:
124+
return Schema6(database: database);
85125
default:
86126
throw Exception('unknown schema version: $schemaVersion');
87127
}
88128
}
89129

90-
@DriftDatabase(tables: [GlobalSettings, Accounts])
130+
@DriftDatabase(tables: [GlobalSettings, BoolGlobalSettings, Accounts])
91131
class AppDatabase extends _$AppDatabase {
92132
AppDatabase(super.e);
93133

@@ -102,7 +142,7 @@ class AppDatabase extends _$AppDatabase {
102142
// * Write a migration in `_migrationSteps` below.
103143
// * Write tests.
104144
@override
105-
int get schemaVersion => 5; // See note.
145+
int get schemaVersion => 6; // See note.
106146

107147
static Future<void> _dropAndCreateAll(Migrator m, {
108148
required int schemaVersion,
@@ -156,6 +196,9 @@ class AppDatabase extends _$AppDatabase {
156196
}
157197
});
158198
},
199+
from5To6: (m, schema) async {
200+
await m.createTable(schema.boolGlobalSettings);
201+
},
159202
);
160203

161204
@override
@@ -186,6 +229,17 @@ class AppDatabase extends _$AppDatabase {
186229
return await (select(globalSettings)..limit(1)).getSingle();
187230
}
188231

232+
Future<Map<BoolGlobalSetting, bool>> getBoolGlobalSettings() async {
233+
final result = <BoolGlobalSetting, bool>{};
234+
final rows = await select(boolGlobalSettings).get();
235+
for (final row in rows) {
236+
final setting = BoolGlobalSetting.byName(row.name);
237+
if (setting == null) continue;
238+
result[setting] = row.value;
239+
}
240+
return result;
241+
}
242+
189243
Future<int> createAccount(AccountsCompanion values) async {
190244
try {
191245
return await into(accounts).insert(values);

0 commit comments

Comments
 (0)