8000 fix: memento format by gfyrag · Pull Request #926 · formancehq/ledger · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: memento format #926

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

Merged
merged 1 commit into from
May 19, 2025
Merged

fix: memento format #926

merged 1 commit into from
May 19, 2025

Conversation

gfyrag
Copy link
Contributor
@gfyrag gfyrag commented May 19, 2025

No description provided.

@gfyrag gfyrag requested a review from a team as a code owner May 19, 2025 10:04
Copy link
coderabbitai bot commented May 19, 2025

Walkthrough

This update introduces a new PostgreSQL function, json_compact, via a migration, and adds a migration script that batch-updates the memento JSON field in the logs table for specific log types, ensuring compact formatting and proper character encoding. It also drops the txs_view table if present.

Changes

File(s) Change Summary
internal/storage/bucket/migrations/34-fix-memento-format/up.sql Adds a migration script that batch-processes the logs table, updating the memento JSON field for certain types, handling encoding, and drops txs_view.
internal/storage/system/migrations.go Registers a new migration to create the public.json_compact PostgreSQL function for recursively compacting JSON.

Sequence Diagram(s)

sequenceDiagram
    participant MigrationScript
    participant PostgreSQL
    loop For each batch in logs table
        MigrationScript->>PostgreSQL: Select batch of 1000 rows from logs
        PostgreSQL-->>MigrationScript: Return batch
        alt For each log entry of specific type
            MigrationScript->>PostgreSQL: Update memento JSON (compact, encode)
        else For other types
            MigrationScript->>PostgreSQL: Convert memento from UTF-8
        end
        MigrationScript->>PostgreSQL: Commit batch updates
        MigrationScript->>PostgreSQL: Send notification of progress
    end
    MigrationScript->>PostgreSQL: Drop txs_view if exists
Loading

Possibly related PRs

Suggested reviewers

  • paul-nicolas
  • Dav-14

Poem

In the warren of data, we hop and we fix,
Compacting our JSON with clever new tricks.
Mementos now tidy, no stray bytes to find,
With batches and scripts, all robustly aligned.
🐇✨
A hop, a skip, and a migration well-timed!

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1828952 and 8892a34.

⛔ Files ignored due to path filters (1)
  • internal/storage/bucket/migrations/34-fix-memento-format/notes.yaml is excluded by !**/*.yaml
📒 Files selected for processing (2)
  • internal/storage/bucket/migrations/34-fix-memento-format/up.sql (1 hunks)
  • internal/storage/system/migrations.go (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Tests
  • GitHub Check: Dirty
🔇 Additional comments (6)
internal/storage/system/migrations.go (2)

230-238: LGTM - New database migration added correctly

The migration adds a new PostgreSQL function json_compact that recursively compacts JSON data by removing unnecessary whitespace. This function will be useful for standardizing JSON storage format in the database.


264-322: The json_compact function implementation looks solid

The PL/pgSQL function correctly handles the recursive compaction of JSON objects, arrays, and simple values. It:

  1. Identifies the JSON type using json_typeof
  2. For objects, iterates through keys and recursively compacts each value
  3. For arrays, iterates through elements and recursively compacts each element
  4. For simple values, returns them as-is
  5. Uses the p_step parameter to track recursion depth

The commented-out newline insertions (lines 294, 310) are intentionally disabled to achieve compact formatting.

internal/storage/bucket/migrations/34-fix-memento-format/up.sql (4)

1-11: Good batch processing setup with error handling

The migration uses a batch processing approach with a reasonable batch size (1000 rows), and includes an early exit if no logs exist. This will help manage memory usage and transaction size during the migration.


12-13: Migration progress notification is helpful

Using pg_notify to track migration progress is a good practice, especially for potentially long-running operations like this one.


14-92: Well-structured JSON reconstruction with proper type handling

The migration correctly rebuilds the JSON structure for specific log types:

  1. For NEW_TRANSACTION, SET_METADATA, and REVERTED_TRANSACTION log types, it constructs compact JSON with proper field ordering
  2. Correctly casts numeric values using ::numeric
  3. Preserves existing memento content for other log types
  4. Uses the new json_compact function for consistent formatting

The character escaping for &, <, and > characters (lines 78-81) provides protection against potential XSS issues when this JSON might be used in web contexts.

The batch commit approach (line 91) ensures the database doesn't hold large transactions in memory.


94-94:

✅ Verification successful

Verify the impact of dropping txs_view

The migration drops the txs_view table at the end of the process. Please confirm this is intentional and won't impact any dependent functionality.


🏁 Script executed:

#!/bin/bash
# Check if there are any references to txs_view in the codebase
echo "Searching for references to txs_view in the codebase:"
rg -i "txs_view" --type go --type sql

Length of output: 3487


No dependent references for txs_view outside migration scripts—drop is safe

Verified that a case‐insensitive search for “txs_view” in Go and SQL files only surfaces its creation and drop within migration files (migrations/29 through 34). There are no references in application code or production views, so cleaning it up here is intentional and has no downstream impact.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch hotfix/v2.2/fix-memento-format
  • Post Copyable Unit Tests in Comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this 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.

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.

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.

@gfyrag gfyrag merged commit fa461c4 into release/v2.2 May 19, 2025
8 checks passed
@gfyrag gfyrag deleted the hotfix/v2.2/fix-memento-format branch May 19, 2025 11:56
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
0