restoreBackup method
Implementation
Future<void> restoreBackup(String backupPath) async {
final file = File(backupPath);
if (!await file.exists()) {
throw Exception('Backup file not found at $backupPath');
}
final jsonString = await file.readAsString();
final Map<String, dynamic> fullDump = jsonDecode(jsonString);
final currentTables = await adapter.getTables();
for (final table in currentTables) {
final currentRecords = await adapter.getRecords(table);
if (currentRecords.isNotEmpty) {
final columns = currentRecords.first.keys.toList();
final pkColumn = _getPrimaryKeyColumn(columns);
for (final record in currentRecords) {
final pkValue = record[pkColumn];
if (pkValue != null) {
await adapter.deleteRecord(table, pkValue);
}
}
}
}
for (final table in fullDump.keys) {
final List<dynamic> records = fullDump[table];
for (final record in records) {
if (record is Map<String, dynamic>) {
await adapter.insertRecord(table, record);
}
}
}
}