Files
umbrix/lib/core/database/app_database.dart

79 lines
2.7 KiB
Dart
Raw Normal View History

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';
// import 'package:hiddify/features/geo_asset/data/geo_asset_data_mapper.dart';
// import 'package:hiddify/features/geo_asset/model/default_geo_assets.dart';
2023-11-25 22:00:40 +03:30
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
int get schemaVersion => 4;
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();
},
from3To4: (m, schema) async {
// TODO: check if column exists, if not then add column
try {
await m.addColumn(profileEntries, profileEntries.testUrl);
} on Exception catch (err) {
loggy.debug(err);
}
},
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 {
// final geoAssets = defaultGeoAssets.map((e) => e.toEntry());
// for (final geoAsset in geoAssets) {
// await into(geoAssetEntries)
// .insert(geoAsset, mode: InsertMode.insertOrIgnore);
// }
2023-11-17 21:30:09 +03:30
});
}
2023-07-06 17:18:41 +03:30
}