-
Notifications
You must be signed in to change notification settings - Fork 101
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
feat: cleanup database #518
Conversation
16b91d6
to
afcb55a
Compare
dac31bf
to
79c5bf7
Compare
aeaee80
to
339c41f
Compare
79c5bf7
to
a1a55aa
Compare
aa05a28
to
f2b2e34
Compare
a1a55aa
to
61ab151
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## feat/ledger-stateless-next #518 +/- ##
==============================================================
+ Coverage 82.87% 83.13% +0.25%
==============================================================
Files 118 110 -8
Lines 6927 6088 -839
==============================================================
- Hits 5741 5061 -680
+ Misses 902 779 -123
+ Partials 284 248 -36 ☔ View full report in Codecov by Sentry. |
61ab151
to
8e50b29
Compare
f2b2e34
to
f25ea6e
Compare
46e6979
to
5cdd87c
Compare
452739b
to
37a1ed9
Compare
5cdd87c
to
eb09975
Compare
37a1ed9
to
bbb2ca0
Compare
eb09975
to
c413d13
Compare
bbb2ca0
to
35fe5f5
Compare
c413d13
to
e852b25
Compare
35fe5f5
to
42d49a5
Compare
d7cf763
to
bc54d6f
Compare
42d49a5
to
c2476bd
Compare
bc54d6f
to
84b5473
Compare
c2476bd
to
728eae8
Compare
09c21e1
to
0cf8b15
Compare
ad4e6cf
to
a8a6683
Compare
faa996c
to
a8b5140
Compare
b6271da
to
fb5213f
Compare
a8b5140
to
ee88022
Compare
fb5213f
to
0a057fb
Compare
ee88022
to
8f07a8c
Compare
2cd7e4e
to
f9f750e
Compare
8f07a8c
to
009df0d
Compare
f9f750e
to
f91b6c4
Compare
009df0d
to
2d3e66e
Compare
ac50890
to
1b49c3f
Compare
2d3e66e
to
2e50464
Compare
2e50464
to
0538189
Compare
WalkthroughThis pull request introduces several significant changes to the ledger storage system, focusing on database schema modifications, query optimizations, and code cleanup. The changes include updating configuration metadata handling, modifying database migrations, simplifying transaction and account querying, and removing legacy storage components. The modifications aim to streamline the ledger system's data management and improve database interaction efficiency. Changes
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
0538189
to
87fcd13
Compare
There was a problem hiding this 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 (5)
internal/storage/ledger/balances.go (2)
52-54
: Use clear naming or inline comments for CTE.
The variable name"ins"
might be confusing when reading the query. Consider using a more descriptive alias for clarity, e.g."insert_zero_balances"
.- .With("ins", + .With("insert_zero_balances",
55-57
: Grammar nitpick in comments.
Replace “It the complete sql transaction fail” with “If the complete SQL transaction fails” for clarity.- // It the complete sql transaction fail, the account volumes will not be inserted. + // If the complete SQL transaction fails, the account volumes will not be inserted.internal/storage/ledger/adapters.go (2)
14-16
: Properly encapsulate store pointer.
Embedding*Store
inDefaultStoreAdapter
is fine, but if you need to swap the underlying store or guard access, consider a more explicit field instead.
18-20
: Method name clarity.
IsUpToDate
describes an internal check on minimal schema. The name is acceptable, though “HasMinimalSchemaVersion” might be more transparent.internal/storage/bucket/migrations/27-clean-database/up.sql (1)
83-121
: Scrutinize potential performance impact for set_log_hash function
This function hashes the new log entry with the previous log’s hash. Ensure that the ordering onlogs
byid desc
is supported by an index for quick lookups.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (3)
go.mod
is excluded by!**/*.mod
internal/storage/bucket/migrations/26-accounts-recreate-unique-index/notes.yaml
is excluded by!**/*.yaml
internal/storage/bucket/migrations/27-clean-database/notes.yaml
is excluded by!**/*.yaml
📒 Files selected for processing (29)
internal/README.md
(1 hunks)internal/ledger.go
(1 hunks)internal/storage/bucket/default_bucket.go
(2 hunks)internal/storage/bucket/migrations/26-accounts-recreate-unique-index/up.sql
(1 hunks)internal/storage/bucket/migrations/27-clean-database/up.sql
(1 hunks)internal/storage/driver/adapters.go
(2 hunks)internal/storage/driver/driver.go
(0 hunks)internal/storage/ledger/adapters.go
(1 hunks)internal/storage/ledger/balances.go
(1 hunks)internal/storage/ledger/balances_test.go
(1 hunks)internal/storage/ledger/legacy/accounts.go
(0 hunks)internal/storage/ledger/legacy/accounts_test.go
(0 hunks)internal/storage/ledger/legacy/adapters.go
(0 hunks)internal/storage/ledger/legacy/balances.go
(0 hunks)internal/storage/ledger/legacy/balances_test.go
(0 hunks)internal/storage/ledger/legacy/debug.go
(0 hunks)internal/storage/ledger/legacy/errors.go
(0 hunks)internal/storage/ledger/legacy/logs.go
(0 hunks)internal/storage/ledger/legacy/logs_test.go
(0 hunks)internal/storage/ledger/legacy/main_test.go
(0 hunks)internal/storage/ledger/legacy/queries.go
(0 hunks)internal/storage/ledger/legacy/store.go
(0 hunks)internal/storage/ledger/legacy/transactions.go
(0 hunks)internal/storage/ledger/legacy/transactions_test.go
(0 hunks)internal/storage/ledger/legacy/utils.go
(0 hunks)internal/storage/ledger/legacy/volumes.go
(0 hunks)internal/storage/ledger/legacy/volumes_test.go
(0 hunks)internal/storage/ledger/resource_aggregated_balances.go
(1 hunks)internal/storage/system/migrations.go
(1 hunks)
💤 Files with no reviewable changes (18)
- internal/storage/driver/driver.go
- internal/storage/ledger/legacy/logs_test.go
- internal/storage/ledger/legacy/errors.go
- internal/storage/ledger/legacy/balances_test.go
- internal/storage/ledger/legacy/volumes_test.go
- internal/storage/ledger/legacy/debug.go
- internal/storage/ledger/legacy/balances.go
- internal/storage/ledger/legacy/transactions_test.go
- internal/storage/ledger/legacy/main_test.go
- internal/storage/ledger/legacy/accounts_test.go
- internal/storage/ledger/legacy/logs.go
- internal/storage/ledger/legacy/store.go
- internal/storage/ledger/legacy/volumes.go
- internal/storage/ledger/legacy/transactions.go
- internal/storage/ledger/legacy/utils.go
- internal/storage/ledger/legacy/accounts.go
- internal/storage/ledger/legacy/queries.go
- internal/storage/ledger/legacy/adapters.go
✅ Files skipped from review due to trivial changes (1)
- internal/storage/ledger/resource_aggregated_balances.go
🔇 Additional comments (21)
internal/storage/ledger/balances.go (1)
58-70
: Validate concurrency and locking strategy.
Locking the volumes rows withFOR UPDATE
can help avoid race conditions when multiple transactions concurrently insert or update balances. Ensure that this approach aligns with your broader concurrency model, especially if there's a chance of deadlocks.Consider running an analysis to see if other code paths also lock rows in a possibly inconsistent ordering:
✅ Verification successful
Let me search for more context around locking patterns to ensure a comprehensive verification.
Let me search for more specific context around the locking implementation.
Locking strategy is properly implemented with consistent ordering
The verification confirms that the locking strategy is well-implemented:
- The
FOR UPDATE
lock is only used in one place in the codebase (balances.go)- The code explicitly maintains a consistent locking order through the
Order("accounts_address", "asset")
clause, which is documented with the comment "Keep order, it ensures consistent locking order and limit deadlocks"- The codebase includes proper deadlock detection and handling, as evidenced by the presence of
postgres.ErrDeadlockDetected
error handling and deadlock metrics tracking- The ordering is consistently applied whenever the rows are locked, preventing circular wait conditions that could cause deadlocks
The implementation follows best practices for preventing deadlocks by ensuring a consistent order of locking operations across the application.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Searching for other statements containing "FOR UPDATE" to verify consistent ordering rg "For\(\"update\"\)"Length of output: 77
Script:
#!/bin/bash # Search for any ordering patterns in SQL queries that might be related to locking rg -B 3 -A 3 "Order\(" # Also search for potential deadlock-related comments rg -i "deadlock"Length of output: 53955
Script:
#!/bin/bash # Search for the implementation of the volumes-related functions to understand the locking strategy ast-grep --pattern 'func ($_, $_) Volumes() $_' # Also search for any other FOR UPDATE clauses in SQL queries rg -B 5 -A 5 'For\("update"\)'Length of output: 934
internal/storage/driver/adapters.go (2)
5-5
: Import usage acknowledgment.
Importingledgerstore
frominternal/storage/ledger
is appropriate for returning a newDefaultStoreAdapter
. No issues found.
22-22
: Confirm removal of bucket 'up-to-date' checks.
Previously, calls tostore.GetBucket().IsUpToDate(ctx)
might have ensured correct schema version. Removing these checks can lead to runtime errors if the schema is out-of-date. Confirm that these checks are no longer needed.✅ Verification successful
Let me gather more context about the schema version checks.
Let me check the context around the store adapter creation.
Let me check how the schema version is handled in the OpenLedger function.
Let me try a different approach to check the OpenLedger implementation.
Let me check the driver's Initialize method to see how schema version is handled during initialization.
Let me check one final thing about how migrations are handled in the driver.
Schema version checks are still properly enforced
Based on the code analysis, the schema version checks are still maintained through a robust system:
- The driver's
Initialize
method performs schema migrations and rollback detection- Schema version is checked in multiple places:
- System store checks via
systemStore.IsUpToDate
- Bucket-level checks via
bucket.IsUpToDate
- Migrations are handled during initialization with
systemStore.Migrate
- The
detectRollbacks
function specifically prevents schema downgradesThe removal of direct
IsUpToDate
calls in the store adapter creation is safe because schema version consistency is enforced at a higher level in the driver initialization and operation flow.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for IsUpToDate calls to confirm that they're handled elsewhere or truly removed rg "IsUpToDate"Length of output: 3904
Script:
#!/bin/bash # Check how schema version is handled in the driver rg -A 5 "systemStore.IsUpToDate" internal/storage/driver/Length of output: 447
Script:
#!/bin/bash # Check the context around NewDefaultStoreAdapter rg -B 5 -A 5 "NewDefaultStoreAdapter" internal/storage/Length of output: 1457
Script:
#!/bin/bash # Check the OpenLedger implementation to see how schema version is handled ast-grep --pattern 'func $_($_ context.Context, $_ string) ($_, $_, error) { $$$ }' internal/storage/driver/driver.goLength of output: 121
Script:
#!/bin/bash # Check the OpenLedger implementation rg -B 5 -A 15 "func.*OpenLedger" internal/storage/driver/driver.goLength of output: 698
Script:
#!/bin/bash # Check the Initialize implementation and schema version handling rg -B 2 -A 20 "func.*Initialize.*Context.*error" internal/storage/driver/driver.goLength of output: 948
Script:
#!/bin/bash # Check where migrations are handled in the driver rg -B 5 -A 10 "func.*detectRollbacks|func.*Migrate" internal/storage/driver/Length of output: 5865
internal/storage/ledger/adapters.go (8)
1-9
: Package separation looks good.
Creating a separate package for ledger adapters clarifies responsibilities. No immediate issues noted.
10-12
: Embed store pointer carefully.
Embedding*Store
inTX
is convenient but watch for potential nil dereferences ifTX
is used after a rollback or commit. Ensure that usage patterns avoid referencing a closed transaction.
22-31
: Transactional begin success.
Starting a transaction and returning a newDefaultStoreAdapter
is consistent. Ensure that callers handle rollback logic carefully to avoid leaks.
33-35
: Transaction commit trust.
Committing the underlying store transaction works as expected. Validate that any references tod.Store
after commit are safe.
37-39
: Transaction rollback safety.
Same caution as Commit: ensure that references tod.Store
after rollback handle the closed transaction scenario.
41-43
: Method naming alignment.
AggregatedBalances()
returningd.AggregatedVolumes()
is consistent, but if this method is commonly used, confirm that the naming is self-explanatory in the broader context.
45-49
: Constructor for adapter is straightforward.
The constructor is succinct and adequate. No issues found.
51-51
: Interface conformance.
ConfirmingDefaultStoreAdapter
implementsledgercontroller.Store
is good.internal/ledger.go (1)
87-87
: Validate null metadata usage.
ChangingMetadata
tonullzero
is beneficial for optional metadata. Confirm any queries or JSON marshalling logic handlenil
fields gracefully.internal/storage/bucket/default_bucket.go (3)
18-18
: Schema version jump.
RaisingMinimalSchemaVersion
from 12 to 27 is significant. Verify that your migrations support this jump to avoid blocking users on older versions.
21-22
: Field reordering.
Reordering struct fields typically doesn’t cause issues. Just be mindful if reflection-based logic or field tags rely on a specific order.
84-85
: Constructor ordering.
Adjusting the initialization order (db first, name second) matches the struct fields. This is consistent and avoids potential overshadowing.internal/storage/system/migrations.go (1)
218-229
: Ensure consistency with existing metadata handling
By setting the default metadata to'{}'::jsonb
, newly inserted rows will always have a JSON object, potentially streamlining queries that assume non-null metadata. However, verify that no part of the application expects aNULL
value inmetadata
.internal/storage/ledger/balances_test.go (1)
46-70
: Great test coverage for non-existing accounts
This test scenario correctly ensures that a not-yet-existing account is automatically created with zero volumes, improving confidence in system consistency.internal/storage/bucket/migrations/26-accounts-recreate-unique-index/up.sql (1)
1-9
: Concurrent index creation is advisable and well-implemented
Creating the index concurrently avoids lengthy table locks. Ensure that client code checks for potential partial index availability in case of concurrency issues or failures.internal/storage/bucket/migrations/27-clean-database/up.sql (2)
1-82
: Validate that all dropped objects are truly unused
This script removes numerous functions, aggregates, and columns. Confirm that every reference to them has been removed from the codebase and no external integrations still rely on them.
123-123
: Confirm no references to seq remain in the logs table
Dropping theseq
column finalizes the cleanup. Verify that no leftover code or queries still rely onlogs.seq
.internal/README.md (1)
248-248
: LGTM! Verify handling of null metadata values.The addition of
nullzero
to the Metadata field's bun tag aligns with the database cleanup objective. This change allows empty metadata to be stored as NULL in the database instead of an empty object.Let's verify the handling of null metadata values in the codebase:
✅ Verification successful
LGTM! The nullzero tag addition is safe and well-handled
The addition of
nullzero
to the Configuration's Metadata field is safe based on the codebase analysis:
The Configuration struct is primarily used for ledger configuration and initialization, with default values ensuring metadata is never nil:
- Default initialization sets empty metadata:
Metadata: metadata.Metadata{}
- No direct metadata field access without proper initialization was found
The database layer already handles null metadata values:
- Existing migrations ensure metadata is not null:
set metadata = '{}'::jsonb where metadata is null
- Query operations use
coalesce
when retrieving metadata:coalesce(accounts_metadata.metadata, '{}'::jsonb)
The
nullzero
tag aligns with the codebase's handling of empty metadata as NULL in the database while maintaining the empty object representation in the application layer.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Check for direct metadata access that might assume non-null values ast-grep --pattern 'Configuration { $$$ $x.Metadata$$$ }' # Check for potential SQL queries that might need NULL handling rg -A 2 "metadata.*FROM.*configurations"Length of output: 106
Script:
#!/bin/bash # Let's try a different approach to find metadata usage # Find files containing "Configuration" struct references rg -l "Configuration.*struct" # Search for metadata field access patterns rg -i "metadata" --type go # Look for SQL operations on configurations table rg -i "configurations.*metadata|metadata.*configurations" --type goLength of output: 77321
Script:
#!/bin/bash # Let's check for Configuration struct and its metadata field usage ast-grep --pattern 'type Configuration struct { $$$ Metadata $$$ $$$ }' # Look for metadata field access in code rg -A 2 'Configuration.*Metadata' --type go # Check for database migrations or schema related to configurations table rg -A 5 'CREATE TABLE.*configurations|ALTER TABLE.*configurations' --type sqlLength of output: 579
Script:
#!/bin/bash # Let's check how the Configuration struct is used rg -A 3 'Configuration\{' --type go # Look for database operations involving Configuration rg -A 3 'Configuration.*metadata' --type go # Check for any existing null metadata handling rg -A 3 'metadata.*null|null.*metadata' --type goLength of output: 13615
No description provided.