Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FKs on sqllite migrations #2083

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

mpoindexter
Copy link
Contributor

@mpoindexter mpoindexter commented Aug 28, 2024

My take on the right way to solve #2074 and #2063 that leaves the DB in a consistent state whether it's new or migrated

  • have read the CONTRIBUTING.md file
  • raised a GitHub issue or discussed it on the projects chat beforehand
  • added unit tests
  • added integration tests
  • updated documentation if needed
  • updated CHANGELOG.md

Summary by CodeRabbit

  • New Features

    • Improved database migration process, ensuring more reliable management of routes during updates.
    • Added functionality to temporarily disable foreign key constraints during migrations, enhancing data integrity.
  • Bug Fixes

    • Addressed potential data loss by removing invalid routes associated with non-existent nodes before migration.

Copy link
Contributor

coderabbitai bot commented Aug 28, 2024

Walkthrough

The changes made to the database migration logic in hscontrol/db/db.go streamline the handling of the Route table by ensuring the AutoMigrate function is always executed. Additionally, a new function called runMigrations has been introduced to manage migration operations more effectively, including the temporary disabling of foreign key constraints in SQLite. This update enhances the management of invalid routes and foreign key constraints during database updates.

Changes

Files Change Summary
hscontrol/db/db.go Modified the NewHeadscaleDatabase function to always call AutoMigrate for the Route table; added runMigrations function to handle migration logic and foreign key constraints.

Poem

In the database, we hop with glee,
Routes now migrate, wild and free.
Constraints are tamed, no more to fear,
With each new change, we cheer and cheer!
A happy rabbit, we dance and play,
For smooth migrations make our day! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f4427dd and 7aaab7f.

Files selected for processing (1)
  • hscontrol/db/db.go (3 hunks)
Additional comments not posted (2)
hscontrol/db/db.go (2)

126-135: Verify the impact of always calling AutoMigrate for the Route table.

The removal of invalid routes ensures data integrity. However, always calling AutoMigrate for the Route table might have unintended side effects. Verify if this change affects other parts of the codebase or introduces any performance issues.

Run the following script to verify the impact:

Verification successful

The impact of always calling AutoMigrate for the Route table is isolated.

The AutoMigrate call for the Route table is only present in the hscontrol/db/db.go file, indicating that the change is isolated to this location. There are no other occurrences in the codebase, suggesting that there are no broader implications or performance issues introduced by this change.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of always calling `AutoMigrate` for the `Route` table.

# Test: Search for the `AutoMigrate` function usage. Expect: Only occurrences of the new usage.
rg --type go -A 5 $'AutoMigrate(&types.Route{})'

Length of output: 184


Script:

#!/bin/bash
# Description: Verify the impact of always calling `AutoMigrate` for the `Route` table.

# Test: Search for the `AutoMigrate` function usage with escaped parentheses.
rg --type go -A 5 $'AutoMigrate\\(&types\\.Route\\{\\}\\)'

Length of output: 297


543-605: Verify the correctness of temporarily disabling foreign key constraints.

The runMigrations function improves the robustness of the migration process by ensuring that foreign key constraints are respected. However, the temporary disabling of foreign key constraints should be carefully reviewed to avoid potential data integrity issues.

Run the following script to verify the correctness:

@kradalby
Copy link
Collaborator

Much appreciated, thank you, I'm sorry for not getting to this quicker, a lot of things is taking my time so very happy with the help. Is there any way we can add a test that will fail without this change so we ensure changes to this code does not regress? Like using one of the databases added in the previous PR and validating that the FK is there?

I'm happy to have a look when I next have time, but if you have an opportunity that would also be helpful.

@mpoindexter
Copy link
Contributor Author

I guess the existing tests you've added will cover the regression case: without the proper FK handling, the tests will fail with an empty routes table now that the conditions on the AutoMigrate are removed. I suppose I could write a test that a particular constraint exists in a migrated database, but that seems like an overly specific test for a general fix. I guess the only proper test would be to ensure that the schema of a migrated DB and new DB are the same, but that seems like a lot of work.

@kradalby
Copy link
Collaborator

Sure, make sense, thank you. I will review this later today/tomorrow properly and get it in as the tests pass.

@kradalby kradalby merged commit 34361c6 into juanfont:main Aug 30, 2024
110 of 111 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.

2 participants