Replies: 1 comment 10 replies
-
External representations like json or csv should work. When there's a lot of data, you may have to write it in small chunks. However, if you just need backup/restore, I think the easiest way is to just use sqlite3 to copy the data over. I haven't tried it, but I think something like this should work: extension CreateBackup on GeneratedDatabase {
Future<void> createBackup(File to, List<TableInfo> tables) async {
await customStatement('ATTACH DATABASE ? AS backup_target', [to.path]);
final migrator = Migrator(this);
for (final table in tables) {
final sourceName = table.actualTableName;
final targetName = 'backup_target.${table.actualTableName}';
final aliased = alias(table, targetName) as TableInfo;
await migrator.createTable(aliased);
await customStatement(
'INSERT INTO $targetName SELECT * FROM $sourceName');
}
await customStatement('DETACH backup_target');
}
} To restore a backup, you could once again attach the additional database and copy values over in the other direction. |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
There is a discussion on how to restore and backup in flutter moor here..
It works only for backup and restore entire database.
But what I want is to export and restore specific tables only.
Hence I wonder is there any simple way to do this?
I am thinking of using csv, but it seem complicated when the tables are related with each other. Eg, User table, and User entry where user entry have foreign key of user.
Other option would be json file.
And what about the performance of the process, if the table has, say more than 10k of entries?
Beta Was this translation helpful? Give feedback.
All reactions