Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Database extends _$Database {
Database([QueryExecutor? executor]) : super(executor ?? _openConnection());

@override
int get schemaVersion => 2;
int get schemaVersion => 3;

static LazyDatabase _openConnection() {
return LazyDatabase(() async {
Expand All @@ -52,6 +52,9 @@ class Database extends _$Database {
await _resetOrders();
await _migrateRules(m);
}
if (from < 3) {
await _repairLegacyProfileLabels();
}
},
beforeOpen: (details) async {
// final m = Migrator(this);
Expand Down Expand Up @@ -114,6 +117,12 @@ class Database extends _$Database {
await rulesDao.resetOrders();
}

Future<void> _repairLegacyProfileLabels() {
return customStatement(
"UPDATE profiles SET label = '' WHERE label IS NULL",
);
}

Future<void> restore(
List<Profile> profiles,
List<Script> scripts,
Expand Down
55 changes: 55 additions & 0 deletions test/database/database_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import 'dart:io';

import 'package:drift/native.dart';
import 'package:fl_clash/database/database.dart';
import 'package:sqlite3/sqlite3.dart' as sqlite;
import 'package:test/test.dart';

void main() {
test(
'repairs legacy profiles with a missing label before reading them',
() async {
final directory = await Directory.systemTemp.createTemp(
'flclash-db-test-',
);
final file = File('${directory.path}/data.sqlite');
final legacyDatabase = sqlite.sqlite3.open(file.path);
addTearDown(() async {
await directory.delete(recursive: true);
});

legacyDatabase.execute('''
CREATE TABLE profiles (
id INTEGER NOT NULL PRIMARY KEY,
label TEXT,
current_group_name TEXT,
url TEXT NOT NULL,
last_update_date INTEGER,
overwrite_type TEXT NOT NULL,
script_id INTEGER,
auto_update_duration_millis INTEGER NOT NULL,
subscription_info TEXT,
auto_update INTEGER NOT NULL,
selected_map TEXT NOT NULL,
unfold_set TEXT NOT NULL,
"order" INTEGER
);
''');
legacyDatabase.execute('''
INSERT INTO profiles (
id, label, url, overwrite_type, auto_update_duration_millis,
auto_update, selected_map, unfold_set
) VALUES (1, NULL, '', 'standard', 0, 0, '{}', '[]');
''');
legacyDatabase.execute('PRAGMA user_version = 2;');
legacyDatabase.dispose();

final database = Database(NativeDatabase(file));
addTearDown(database.close);

final profiles = await database.profilesDao.query().get();

expect(profiles.single.label, isEmpty);
},
);
}
Loading