2023-07-06 17:18:41 +03:30
|
|
|
import 'package:drift/drift.dart';
|
2023-12-31 12:04:41 +03:30
|
|
|
// ignore: depend_on_referenced_packages
|
|
|
|
|
import 'package:drift_dev/api/migrations.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2023-12-01 12:56:24 +03:30
|
|
|
import 'package:hiddify/core/database/connection/database_connection.dart';
|
|
|
|
|
import 'package:hiddify/core/database/converters/duration_converter.dart';
|
|
|
|
|
import 'package:hiddify/core/database/schema_versions.dart';
|
|
|
|
|
import 'package:hiddify/core/database/tables/database_tables.dart';
|
2023-11-25 22:00:40 +03:30
|
|
|
import 'package:hiddify/features/geo_asset/data/geo_asset_data_mapper.dart';
|
|
|
|
|
import 'package:hiddify/features/geo_asset/model/default_geo_assets.dart';
|
|
|
|
|
import 'package:hiddify/features/geo_asset/model/geo_asset_entity.dart';
|
2023-11-26 21:20:58 +03:30
|
|
|
import 'package:hiddify/features/profile/model/profile_entity.dart';
|
2023-12-31 12:04:41 +03:30
|
|
|
import 'package:hiddify/utils/custom_loggers.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-12-01 12:56:24 +03:30
|
|
|
part 'app_database.g.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-11-25 22:00:40 +03:30
|
|
|
@DriftDatabase(tables: [ProfileEntries, GeoAssetEntries])
|
2023-12-31 12:04:41 +03:30
|
|
|
class AppDatabase extends _$AppDatabase with InfraLogger {
|
2023-07-06 17:18:41 +03:30
|
|
|
AppDatabase({required QueryExecutor connection}) : super(connection);
|
|
|
|
|
|
2023-12-01 12:56:24 +03:30
|
|
|
AppDatabase.connect() : super(openConnection());
|
2023-07-06 17:18:41 +03:30
|
|
|
|
|
|
|
|
@override
|
2023-11-17 21:30:09 +03:30
|
|
|
int get schemaVersion => 3;
|
2023-10-02 18:51:14 +03:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
MigrationStrategy get migration {
|
|
|
|
|
return MigrationStrategy(
|
|
|
|
|
onCreate: (Migrator m) async {
|
|
|
|
|
await m.createAll();
|
2023-11-17 21:30:09 +03:30
|
|
|
await _prePopulateGeoAssets();
|
2023-10-02 18:51:14 +03:30
|
|
|
},
|
|
|
|
|
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-11-17 21:30:09 +03:30
|
|
|
from2To3: (m, schema) async {
|
|
|
|
|
await m.createTable(schema.geoAssetEntries);
|
|
|
|
|
await _prePopulateGeoAssets();
|
|
|
|
|
},
|
2023-10-02 18:51:14 +03:30
|
|
|
),
|
2023-12-31 12:04:41 +03:30
|
|
|
beforeOpen: (details) async {
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
|
await validateDatabaseSchema();
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-10-02 18:51:14 +03:30
|
|
|
);
|
|
|
|
|
}
|
2023-11-17 21:30:09 +03:30
|
|
|
|
|
|
|
|
Future<void> _prePopulateGeoAssets() async {
|
2023-12-31 12:04:41 +03:30
|
|
|
loggy.debug("populating default geo assets");
|
2023-11-17 21:30:09 +03:30
|
|
|
await transaction(() async {
|
2023-11-25 22:00:40 +03:30
|
|
|
final geoAssets = defaultGeoAssets.map((e) => e.toEntry());
|
2023-11-17 21:30:09 +03:30
|
|
|
for (final geoAsset in geoAssets) {
|
2023-12-31 12:04:41 +03:30
|
|
|
await into(geoAssetEntries)
|
|
|
|
|
.insert(geoAsset, mode: InsertMode.insertOrIgnore);
|
2023-11-17 21:30:09 +03:30
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-07-06 17:18:41 +03:30
|
|
|
}
|