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

feat: Refactor storage using triggers #168

Merged
merged 13 commits into from
Mar 18, 2022

Conversation

gfyrag
Copy link
Contributor

@gfyrag gfyrag commented Mar 14, 2022

Refactor storage.

This PR add triggers on "transactions" table to automatically compute balances and volumes of accounts in "balances" and "volumes" tables.
It also create entries in a new table called "accounts". This table will replace "addresses" table.
Now, transaction postings are directly persist as json in the transactions table, as well as metadata.
Account metadata are persisted in "accounts" table, also with json type.

Previously, the GET /accounts was not retrieving accounts which was not part of a transaction.
The new table "accounts" fix defacto this issue.

Also, there is another new table, called "log" which is designed to model an event based store. The code now always write to this table, then a set of triggers generates
entries in the transaction table, or updates metadata of either accounts or transactions following the log "type" column.
The hash on transactions was removed and add on "log" table. As a result we have a complete hash chain taking in account, metadata set on transactions or accounts.
To complete the modification on hash, the "id" property of transactions has been changed from id to a string, and is set to be a uuid by the code.
Old transactions will not have a uuid , just a int to string convert is done by the migration, but since the id is an opaque string, it should not cause any problem. A column named "ord" has been added to keep transactions ordering without the need to read all logs. It is generate by triggers, and not by the code.

@gfyrag gfyrag requested a review from a team as a code owner March 14, 2022 20:27
@gfyrag gfyrag requested a review from reslene March 14, 2022 20:27
@gfyrag gfyrag requested a review from flemzord as a code owner March 15, 2022 10:44
@codecov
Copy link

codecov bot commented Mar 15, 2022

Codecov Report

Merging #168 (b2408b1) into main (4b0b4de) will decrease coverage by 3.26%.
The diff coverage is 74.10%.

@@            Coverage Diff             @@
##             main     #168      +/-   ##
==========================================
- Coverage   74.37%   71.11%   -3.27%     
==========================================
  Files          86       88       +2     
  Lines        3345     3389      +44     
==========================================
- Hits         2488     2410      -78     
- Misses        679      800     +121     
- Partials      178      179       +1     
Impacted Files Coverage Δ
cmd/container.go 75.57% <0.00%> (-1.18%) ⬇️
cmd/script_exec.go 12.96% <0.00%> (ø)
pkg/storage/remember_config.go 0.00% <0.00%> (ø)
pkg/storage/sqlstorage/metadata.go 0.00% <0.00%> (-72.61%) ⬇️
pkg/core/transaction.go 71.42% <42.85%> (-28.58%) ⬇️
pkg/storage/storage.go 49.18% <44.44%> (-15.23%) ⬇️
pkg/opentelemetry/opentelemetrytraces/storage.go 66.08% <50.00%> (-11.08%) ⬇️
pkg/core/log.go 55.26% <55.26%> (ø)
pkg/core/posting.go 76.00% <57.14%> (-24.00%) ⬇️
pkg/core/metadata.go 60.00% <64.70%> (+10.00%) ⬆️
... and 17 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 90fd81c...b2408b1. Read the comment docs.

@gfyrag gfyrag force-pushed the feature/num-427-ledger-db-triggers branch from 17752af to 83b4d45 Compare March 15, 2022 13:00
@gfyrag gfyrag changed the title feature/num 427 ledger db triggers feat: Refactor storage using triggers Mar 15, 2022
@gfyrag gfyrag force-pushed the feature/num-427-ledger-db-triggers branch 2 times, most recently from a7a430d to 8eb5129 Compare March 16, 2022 13:31
Add following tables :
* log
* account
* volumes
* balances

Now, we only write on the "log" table.
There is a set of triggers which are in charge of filling tables when a write is make on "log" table.
Thus, the hash is now on logs instead of transactions.
This way the hash take in account, metadata applied to transactions or accounts.
The automatic migration fill this log table with the existing data.

Since, the new code use some json function, the code must be compiled with "json1" build tag to enable support on sqlite.
@gfyrag gfyrag force-pushed the feature/num-427-ledger-db-triggers branch from 8eb5129 to f703ed6 Compare March 18, 2022 12:52
@flemzord flemzord merged commit 4ad8cbf into main Mar 18, 2022
@flemzord flemzord deleted the feature/num-427-ledger-db-triggers branch March 18, 2022 15:19
@flemzord flemzord restored the feature/num-427-ledger-db-triggers branch March 18, 2022 15:22
@flemzord
Copy link
Member

flemzord commented Mar 18, 2022

Revert this merge temporarily (updating docs beforehand as the migration process needs to be documented)

flemzord pushed a commit that referenced this pull request Jun 7, 2023
flemzord pushed a commit that referenced this pull request Sep 20, 2023
flemzord pushed a commit that referenced this pull request Dec 4, 2023
flemzord pushed a commit that referenced this pull request Sep 25, 2024
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.

2 participants