Skip to content

feat: Add option to dump and load database snapshots, while keeping relationships on foreign tables.#1873

Merged
gustavobtflores merged 1 commit intokernelci:mainfrom
profusion:feat/snapshot_command
May 5, 2026
Merged

feat: Add option to dump and load database snapshots, while keeping relationships on foreign tables.#1873
gustavobtflores merged 1 commit intokernelci:mainfrom
profusion:feat/snapshot_command

Conversation

@alanpeixinho
Copy link
Copy Markdown
Contributor

@alanpeixinho alanpeixinho commented Apr 22, 2026

Include a command to dump and load database snapshots.

Summary

  • Add snapshot and restore subcommands to update_db management command, replacing the previous direct DB-to-DB migration flow
  • Snapshots are stored as .tar.gz archives containing per-table CSV files, preserving foreign key relationships on restore

How to Test

Important

For these tests, especially for writing, use a local database, instead of pointing to production.

  1. Populate a dabase with poetry run python3 manage.py seed_test_data --clear --yes, or use an existing database.
  2. Run the snapshot command, with desired parameters, as in poetry run python3 manage.py update_db snapshot --start-interval "100 days" --end-interval "0 minute" --filepath ./my-snapshot.tar.gz
  3. Check that data is properly saved inside tables on my-snapshot.tar.gz.
  4. Clear the database with run python3 manage.py seed_test_data --clear --yes --checkouts 0 --issues 0
  5. Run the restore command with desired parameters, as poetry run python3 manage.py restore snapshot --start-interval "100 days" --end-interval "0 minute" --filepath ./my-snapshot.tar.gz
  6. Check that the data on the snapshot was properly inserted in the database.

Closes #1884

Copy link
Copy Markdown
Collaborator

@MarceloRobert MarceloRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok, I'll test later

Comment thread backend/kernelCI_app/management/commands/update_db.py Outdated
Comment thread backend/kernelCI_app/management/commands/update_db.py Outdated
@alanpeixinho alanpeixinho force-pushed the feat/snapshot_command branch 2 times, most recently from 2cbd0d8 to e765709 Compare April 29, 2026 14:06
@alanpeixinho alanpeixinho marked this pull request as ready for review April 29, 2026 14:32
@alanpeixinho alanpeixinho force-pushed the feat/snapshot_command branch from e765709 to be9e90c Compare April 29, 2026 14:39
Copy link
Copy Markdown
Collaborator

@MarceloRobert MarceloRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it worked for a small interval, but for an interval of 1 day I got an error of

Processed 1610 tests records: 1.53 MiB
Error updating database: read
CommandError: Command failed

I assume it's because it went over the size limit of 32MiB?

Also left some other comments

Comment thread backend/kernelCI_app/management/commands/update_db.py
Comment thread backend/kernelCI_app/management/commands/update_db.py Outdated
@alanpeixinho alanpeixinho force-pushed the feat/snapshot_command branch from be9e90c to dd1cfa2 Compare April 30, 2026 19:40
@gustavobtflores
Copy link
Copy Markdown
Contributor

I think we could remove https://github.com/kernelci/dashboard/blob/main/backend/scripts/copy_db_data.sh as it no longer makes sense after the changes on the command

try:
match table:
case None:
self.restore_incidents()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be better to restore incidents last here? restore_incidents calls insert_incidents_data that checks for existing issues, builds and tests and could skip incidents incorrectly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Done.

@alanpeixinho
Copy link
Copy Markdown
Contributor Author

it worked for a small interval, but for an interval of 1 day I got an error of

Processed 1610 tests records: 1.53 MiB
Error updating database: read
CommandError: Command failed

I assume it's because it went over the size limit of 32MiB?

Also left some other comments

I have managed to find the problem, when spilling the 32MiB, the temporary file is moved to disk, and this was causing the later reads to fail.
The best solution is to actually open the file with read and write permissions.
But I also included a little more buffer (64MiB) to avoid (for most of the cases) the spilling.

@alanpeixinho alanpeixinho force-pushed the feat/snapshot_command branch 2 times, most recently from 44c6032 to b575a66 Compare April 30, 2026 21:26
Copy link
Copy Markdown
Collaborator

@MarceloRobert MarceloRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to be all working, just left some nits

Comment thread backend/kernelCI_app/management/commands/update_db.py Outdated
Comment thread backend/kernelCI_app/management/commands/update_db.py Outdated
@alanpeixinho alanpeixinho force-pushed the feat/snapshot_command branch 2 times, most recently from a48c6a7 to e36569b Compare May 5, 2026 14:42
relationships on foreign tables.

    * Replace direct DB-to-DB migration with snapshot (dump to tar.gz) and restore
(load from archive) subcommands, using CSV serialization and SpooledTemporaryFile
for memory-efficient streaming.
@alanpeixinho alanpeixinho force-pushed the feat/snapshot_command branch from e36569b to 8cd6a92 Compare May 5, 2026 17:46
Copy link
Copy Markdown
Collaborator

@MarceloRobert MarceloRobert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested again and is working well

@gustavobtflores gustavobtflores added this pull request to the merge queue May 5, 2026
Merged via the queue into kernelci:main with commit 529bb16 May 5, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add database snapshot and restore functionality with foreign key preservation

3 participants