2023-07-06 17:18:41 +03:30
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:drift/drift.dart';
|
|
|
|
|
import 'package:drift/native.dart';
|
2023-09-08 20:11:31 +03:30
|
|
|
import 'package:hiddify/data/local/dao/dao.dart';
|
2023-10-02 18:51:14 +03:30
|
|
|
import 'package:hiddify/data/local/schema_versions.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/data/local/tables.dart';
|
|
|
|
|
import 'package:hiddify/data/local/type_converters.dart';
|
2023-10-02 18:51:14 +03:30
|
|
|
import 'package:hiddify/domain/profiles/profiles.dart';
|
2023-08-19 22:27:23 +03:30
|
|
|
import 'package:hiddify/services/files_editor_service.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:path/path.dart' as p;
|
|
|
|
|
|
|
|
|
|
part 'database.g.dart';
|
|
|
|
|
|
|
|
|
|
@DriftDatabase(tables: [ProfileEntries], daos: [ProfilesDao])
|
|
|
|
|
class AppDatabase extends _$AppDatabase {
|
|
|
|
|
AppDatabase({required QueryExecutor connection}) : super(connection);
|
|
|
|
|
|
|
|
|
|
AppDatabase.connect() : super(_openConnection());
|
|
|
|
|
|
|
|
|
|
@override
|
2023-10-02 18:51:14 +03:30
|
|
|
int get schemaVersion => 2;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
MigrationStrategy get migration {
|
|
|
|
|
return MigrationStrategy(
|
|
|
|
|
onCreate: (Migrator m) async {
|
|
|
|
|
await m.createAll();
|
|
|
|
|
},
|
|
|
|
|
onUpgrade: stepByStep(
|
|
|
|
|
// add type column to profile entries table
|
|
|
|
|
// make url column nullable
|
|
|
|
|
from1To2: (m, schema) async {
|
|
|
|
|
await m.alterTable(
|
|
|
|
|
TableMigration(
|
|
|
|
|
schema.profileEntries,
|
|
|
|
|
columnTransformer: {
|
|
|
|
|
schema.profileEntries.type: const Constant<String>("remote"),
|
|
|
|
|
},
|
|
|
|
|
newColumns: [schema.profileEntries.type],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LazyDatabase _openConnection() {
|
|
|
|
|
return LazyDatabase(() async {
|
2023-08-19 22:27:23 +03:30
|
|
|
final dbDir = await FilesEditorService.getDatabaseDirectory();
|
|
|
|
|
final file = File(p.join(dbDir.path, 'db.sqlite'));
|
2023-07-06 17:18:41 +03:30
|
|
|
return NativeDatabase.createInBackground(file);
|
|
|
|
|
});
|
|
|
|
|
}
|