Fladder/lib/util/migration/isar_drift_migration.dart
2025-07-27 10:54:29 +02:00

80 lines
2.8 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:drift/drift.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isar/isar.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import 'package:fladder/models/syncing/database_item.dart';
import 'package:fladder/models/syncing/i_synced_item.dart';
import 'package:fladder/models/syncing/sync_item.dart';
Future<void> isarMigration(Ref ref, AppDatabase db, String savePath) async {
if (kIsWeb) return;
//Return if the database is already migrated
final isNotEmtpy = await db.select(db.databaseItems).get().then((value) => value.isNotEmpty);
if (isNotEmtpy) {
log('Isar database is not empty, skipping migration');
return;
}
//Open isar database
final applicationDirectory = await getApplicationDocumentsDirectory();
final isarPath = Directory(path.joinAll([applicationDirectory.path, 'Fladder', 'Database']));
await isarPath.create(recursive: true);
final isar = Isar.open(
schemas: [ISyncedItemSchema],
directory: isarPath.path,
);
//Fetch all synced items from the old database
List<SyncedItem> items = isar.iSyncedItems
.where()
.findAll()
.map((e) => SyncedItem.fromIsar(e, path.joinAll([savePath, e.userId ?? "Unkown User"])))
.toList();
//Clear any missing paths
items = items.where((e) => e.path != null ? Directory(e.path!).existsSync() : false).toList();
//Convert to drift database items
final driftItems = items.map(
(item) => DatabaseItemsCompanion(
id: Value(item.id),
parentId: Value(item.parentId),
syncing: Value(item.syncing),
userId: Value(item.userId),
path: Value(item.path),
fileSize: Value(item.fileSize),
sortName: Value(item.sortName),
videoFileName: Value(item.videoFileName),
trickPlayModel: Value(item.fTrickPlayModel != null ? jsonEncode(item.fTrickPlayModel?.toJson()) : null),
mediaSegments: Value(item.mediaSegments != null ? jsonEncode(item.mediaSegments?.toJson()) : null),
images: Value(item.fImages != null ? jsonEncode(item.fImages?.toJson()) : null),
chapters: Value(jsonEncode(item.fChapters.map((e) => e.toJson()).toList())),
subtitles: Value(jsonEncode(item.subtitles.map((e) => e.toJson()).toList())),
userData: Value(item.userData != null ? jsonEncode(item.userData?.toJson()) : null),
),
);
await db.batch((batch) {
batch.insertAll(
db.databaseItems,
driftItems,
mode: InsertMode.insertOrReplace,
);
});
//Delete database file
final baseFolder = Directory(path.join(applicationDirectory.path, 'Fladder'));
if (await baseFolder.exists()) {
log('Deleting old Fladder base folder: ${baseFolder.path}');
// await baseFolder.delete(recursive: true);
}
}