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: too many loops while running migrations #618

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Dec 16, 2024

No description provided.

@gfyrag gfyrag requested a review from a team as a code owner December 16, 2024 14:32
Copy link

coderabbitai bot commented Dec 16, 2024

Walkthrough

The pull request involves modifications to three SQL migration scripts in the internal/storage/bucket/migrations directory. These scripts focus on improving loop iterations, error handling, and data integrity during data migration tasks. Key changes include adjusting loop conditions to prevent out-of-bounds errors, adding conditional checks to avoid unnecessary processing, and ensuring proper updates to various database tables.

Changes

File Change Summary
migrations/18-transactions-fill-inserted-at/up.sql Modified loop iteration from 0 to _count to 0 to _count-1 to prevent out-of-bounds errors. Created a temporary logs_transactions table, added an index, and updated the transactions table with inserted_at values.
migrations/19-transactions-fill-pcv/up.sql Added a conditional check to prevent execution if moves_view is empty. Repositioned initialization notification and added a constraint to ensure post_commit_volumes cannot be null.
migrations/20-accounts-volumes-fill-history/up.sql Adjusted loop iteration from 0.._count to 0.._count-1 to prevent off-by-one errors when processing tmp_volumes records.

Poem

🐰 Migrations dance, SQL scripts leap,
Indexes sharp and loops so neat,
Batches processed with rabbit's care,
No bounds shall catch us unaware!
Database magic, smooth and light,
CodeRabbit's touch makes data bright! 🔍


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 40b22c5 and da5b319.

📒 Files selected for processing (3)
  • internal/storage/bucket/migrations/18-transactions-fill-inserted-at/up.sql (1 hunks)
  • internal/storage/bucket/migrations/19-transactions-fill-pcv/up.sql (1 hunks)
  • internal/storage/bucket/migrations/20-accounts-volumes-fill-history/up.sql (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • internal/storage/bucket/migrations/20-accounts-volumes-fill-history/up.sql
  • internal/storage/bucket/migrations/18-transactions-fill-inserted-at/up.sql
  • internal/storage/bucket/migrations/19-transactions-fill-pcv/up.sql

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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

@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

🧹 Nitpick comments (2)
internal/storage/bucket/migrations/20-accounts-volumes-fill-history/up.sql (1)

34-34: LGTM! Loop condition fix prevents unnecessary iteration

The change from 0.._count to 0.._count-1 correctly aligns with SQL's 0-based OFFSET, preventing an unnecessary extra iteration that could cause out-of-bounds errors.

Consider adding a progress log showing the percentage complete:

-			perform pg_notify('migrations-{{ .Schema }}', 'continue: ' || _batch_size);
+			perform pg_notify('migrations-{{ .Schema }}', 'progress: ' || round((i::float/_count * 100))::text || '% (' || _batch_size || ' records)');
internal/storage/bucket/migrations/19-transactions-fill-pcv/up.sql (1)

27-32: LGTM! Early termination optimization and improved progress tracking

The addition of the early termination check is a good optimization that prevents unnecessary processing when there's no data to migrate. Moving the notification after the check ensures accurate progress reporting.

Consider adding a count to the notification message to help with monitoring:

-		perform pg_notify('migrations-{{ .Schema }}', 'init: ' || (select count(*) from moves_view));
+		with stats as (select count(*) as total, min(transactions_id) as min_id, max(transactions_id) as max_id from moves_view)
+		perform pg_notify('migrations-{{ .Schema }}', 'init: ' || total || ' records (ID range: ' || min_id || '-' || max_id || ')') from stats;
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4a5d23d and 40b22c5.

📒 Files selected for processing (3)
  • internal/storage/bucket/migrations/18-transactions-fill-inserted-at/up.sql (1 hunks)
  • internal/storage/bucket/migrations/19-transactions-fill-pcv/up.sql (1 hunks)
  • internal/storage/bucket/migrations/20-accounts-volumes-fill-history/up.sql (1 hunks)
🔇 Additional comments (1)
internal/storage/bucket/migrations/18-transactions-fill-inserted-at/up.sql (1)

27-27: LGTM! Loop condition fix and proper trigger handling

The change to 0.._count-1 prevents unnecessary iteration, and the migration properly handles triggers during the bulk update operation.

Let's verify that all relevant triggers are properly handled:

✅ Verification successful

LGTM! Loop condition and trigger handling are correctly implemented

The migration properly handles all triggers by setting session_replication_role = replica before the bulk update and resetting it to default afterward. This ensures that the five existing triggers on the transactions table (metadata history, addresses) are temporarily disabled during the migration, preventing unnecessary trigger executions during the bulk update operation. The loop condition fix to 0.._count-1 is also correct.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any other triggers on the transactions table that might need special handling
ast-grep --pattern 'create trigger $_ on transactions'

Length of output: 56


Script:

#!/bin/bash
# Let's try with ripgrep to search for trigger definitions
rg -i "create\s+trigger.*transactions" -g "*.sql"

Length of output: 1308


Script:

#!/bin/bash
# Let's verify if the migration properly handles session_replication_role
rg "session_replication_role" internal/storage/bucket/migrations/18-transactions-fill-inserted-at/up.sql

Length of output: 190

@gfyrag gfyrag force-pushed the fix/migration-too-many-loops branch from 40b22c5 to da5b319 Compare December 16, 2024 14:55
@gfyrag gfyrag added this pull request to the merge queue Dec 18, 2024
Merged via the queue into main with commit faf326e Dec 18, 2024
8 checks passed
@gfyrag gfyrag deleted the fix/migration-too-many-loops branch December 18, 2024 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants