lightninglabs / taproot-assets

A layer 1 daemon, for the Taproot Assets Protocol specification, written in Go (golang)
MIT License
457 stars 110 forks source link

tapdb: backup sqlite db before running database migration #968

Closed ffranr closed 3 months ago

ffranr commented 3 months ago

This commit adds functionality to back up any existing SQLite database file before attempting a database migration.

The process involves creating a backup file, migrating the database, and removing the backup if no changes occurred to the database file. This approach avoids unnecessary backups but results in a backup being created and deleted each time tapd starts.

ffranr commented 3 months ago

I think the unit test is failing because the backup copies the file on disk but the backup procedure doesn't force sqlite to dump the db state to that file.

It might be wise to run the command sqlite3 my_database.db .dump > backup.sql rather than try to copy. And the command should run before we open a handle to the db.

ffranr commented 3 months ago

I think this PR should be ready for reviews and merge now. I got rid of the big downside risk I was worried about (naively copying the database file). We now execute a VACUUM INTO query from the source db. See https://sqlite.org/lang_vacuum.html . This is a documented sqlite database backup strategy.

I've also added a unit test which checks the backup.