pref migrator initial
This commit is contained in:
+54
@@ -0,0 +1,54 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 Fabian Freund.
|
||||||
|
*
|
||||||
|
* This file is part of WebLibre
|
||||||
|
* (see https://weblibre.eu).
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
typedef PreferenceMigrationStep = Future<void> Function();
|
||||||
|
|
||||||
|
class PreferenceMigrationRunner {
|
||||||
|
const PreferenceMigrationRunner({
|
||||||
|
required this.schemaVersion,
|
||||||
|
required this.steps,
|
||||||
|
});
|
||||||
|
|
||||||
|
final int schemaVersion;
|
||||||
|
final Map<int, PreferenceMigrationStep> steps;
|
||||||
|
|
||||||
|
Future<int> run({
|
||||||
|
required Future<int> Function() readVersion,
|
||||||
|
required Future<void> Function(int version) writeVersion,
|
||||||
|
}) async {
|
||||||
|
var version = await readVersion();
|
||||||
|
|
||||||
|
while (version < schemaVersion) {
|
||||||
|
final step = steps[version];
|
||||||
|
if (step == null) {
|
||||||
|
throw StateError(
|
||||||
|
'Missing preference migration from version '
|
||||||
|
'$version to ${version + 1}',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await step();
|
||||||
|
version++;
|
||||||
|
await writeVersion(version);
|
||||||
|
}
|
||||||
|
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
}
|
||||||
+65
-2
@@ -21,20 +21,83 @@ import 'dart:async';
|
|||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
import 'package:collection/collection.dart';
|
import 'package:collection/collection.dart';
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
|
import 'package:flutter_mozilla_components/flutter_mozilla_components.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
import 'package:rxdart/rxdart.dart';
|
import 'package:rxdart/rxdart.dart';
|
||||||
import 'package:weblibre/core/logger.dart';
|
import 'package:weblibre/core/logger.dart';
|
||||||
|
import 'package:weblibre/features/geckoview/features/preferences/data/repositories/preference_migrations.dart';
|
||||||
import 'package:weblibre/features/geckoview/features/preferences/data/models/preference_setting.dart';
|
import 'package:weblibre/features/geckoview/features/preferences/data/models/preference_setting.dart';
|
||||||
import 'package:weblibre/features/geckoview/features/tabs/utils/setting_groups_serializer.dart';
|
import 'package:weblibre/features/geckoview/features/tabs/utils/setting_groups_serializer.dart';
|
||||||
|
import 'package:weblibre/features/user/data/providers.dart';
|
||||||
|
|
||||||
part 'preference_settings.g.dart';
|
part 'preference_settings.g.dart';
|
||||||
|
|
||||||
|
const _preferenceMigrationVersionKey = 'preferenceMigrationVersion';
|
||||||
|
const _preferenceMigrationPartitionKey = 'preferenceMigration';
|
||||||
|
const _preferenceMigrationSchemaVersion = 1;
|
||||||
|
const _mainProcessDisableJitPref =
|
||||||
|
'javascript.options.main_process_disable_jit';
|
||||||
|
|
||||||
@Riverpod(keepAlive: true)
|
@Riverpod(keepAlive: true)
|
||||||
class StartupPreferenceEnforcementService
|
class StartupPreferenceEnforcementService
|
||||||
extends _$StartupPreferenceEnforcementService {
|
extends _$StartupPreferenceEnforcementService {
|
||||||
|
final _prefManager = GeckoPrefService();
|
||||||
|
|
||||||
|
PreferenceMigrationRunner get _migrationRunner => PreferenceMigrationRunner(
|
||||||
|
schemaVersion: _preferenceMigrationSchemaVersion,
|
||||||
|
steps: {
|
||||||
|
0: () => _prefManager.resetPrefs([_mainProcessDisableJitPref]),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<int> _readPreferenceMigrationVersion() async {
|
||||||
|
final db = ref.read(userDatabaseProvider);
|
||||||
|
final value = await db.settingDao.getSettingValue(
|
||||||
|
_preferenceMigrationVersionKey,
|
||||||
|
);
|
||||||
|
|
||||||
|
return value?.readAs(DriftSqlType.int, db.typeMapping) ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _writePreferenceMigrationVersion(int version) async {
|
||||||
|
await ref
|
||||||
|
.read(userDatabaseProvider)
|
||||||
|
.settingDao
|
||||||
|
.updateSetting(
|
||||||
|
_preferenceMigrationVersionKey,
|
||||||
|
_preferenceMigrationPartitionKey,
|
||||||
|
version,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _runMigrations() async {
|
||||||
|
final previousVersion = await _readPreferenceMigrationVersion();
|
||||||
|
final currentVersion = await _migrationRunner.run(
|
||||||
|
readVersion: _readPreferenceMigrationVersion,
|
||||||
|
writeVersion: _writePreferenceMigrationVersion,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (currentVersion > previousVersion) {
|
||||||
|
logger.i(
|
||||||
|
'Applied preference migrations '
|
||||||
|
'$previousVersion -> $currentVersion',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> apply() async {
|
Future<void> apply() async {
|
||||||
|
try {
|
||||||
|
await _runMigrations();
|
||||||
|
} catch (e, s) {
|
||||||
|
logger.w(
|
||||||
|
'Failed to run preference migrations, will retry on next launch',
|
||||||
|
error: e,
|
||||||
|
stackTrace: s,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
final content = await ref.read(_preferenceSettingContentProvider.future);
|
final content = await ref.read(_preferenceSettingContentProvider.future);
|
||||||
final groups = deserializePreferenceSettingGroups(
|
final groups = deserializePreferenceSettingGroups(
|
||||||
PreferencePartition.user,
|
PreferencePartition.user,
|
||||||
@@ -58,7 +121,7 @@ class StartupPreferenceEnforcementService
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final currentPrefs = await GeckoPrefService().getPrefs(
|
final currentPrefs = await _prefManager.getPrefs(
|
||||||
startupPrefs.keys.toList(),
|
startupPrefs.keys.toList(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -77,7 +140,7 @@ class StartupPreferenceEnforcementService
|
|||||||
|
|
||||||
if (prefsToEnforce.isNotEmpty) {
|
if (prefsToEnforce.isNotEmpty) {
|
||||||
try {
|
try {
|
||||||
await GeckoPrefService().applyPrefs(prefsToEnforce);
|
await _prefManager.applyPrefs(prefsToEnforce);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.w(
|
logger.w(
|
||||||
'Failed to enforce startup preferences, will retry on next launch',
|
'Failed to enforce startup preferences, will retry on next launch',
|
||||||
|
|||||||
@@ -29,6 +29,11 @@ import 'package:weblibre/features/user/data/database/definitions.drift.dart';
|
|||||||
class SettingDao extends DatabaseAccessor<UserDatabase> with $SettingDaoMixin {
|
class SettingDao extends DatabaseAccessor<UserDatabase> with $SettingDaoMixin {
|
||||||
SettingDao(super.attachedDatabase);
|
SettingDao(super.attachedDatabase);
|
||||||
|
|
||||||
|
Future<DriftAny?> getSettingValue(String key) async {
|
||||||
|
final query = db.setting.select()..where((r) => r.key.equals(key));
|
||||||
|
return (await query.getSingleOrNull())?.value;
|
||||||
|
}
|
||||||
|
|
||||||
Future<int> updateSetting(String key, String? partitionKey, Object? value) {
|
Future<int> updateSetting(String key, String? partitionKey, Object? value) {
|
||||||
final normalizedValue = (value is Iterable || value is Map)
|
final normalizedValue = (value is Iterable || value is Map)
|
||||||
? jsonEncode(value)
|
? jsonEncode(value)
|
||||||
|
|||||||
Reference in New Issue
Block a user