-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add metamorph records clearing #217
Changes from 11 commits
d9ff887
c000368
8996f57
ad90d79
882f5f9
c5ebd6e
fae2680
37f507d
1689f47
788bc83
92868e3
475b2a0
0bcd8cb
53ebaae
cdebed2
aab927a
d3f5f07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,42 @@ | ||||
package jobs | ||||
|
||||
import ( | ||||
"fmt" | ||||
"github.com/jmoiron/sqlx" | ||||
) | ||||
|
||||
func runDelete(table string, params ClearRecordsParams) error { | ||||
Log(INFO, "Connecting to database ...") | ||||
conn, err := sqlx.Open(params.Scheme(), params.String()) | ||||
if err != nil { | ||||
Log(ERROR, fmt.Sprintf("unable to create connection %s", err)) | ||||
return err | ||||
} | ||||
interval := fmt.Sprintf("%d days", params.RecordRetentionDays) | ||||
|
||||
stmt, err := conn.Preparex(fmt.Sprintf("DELETE FROM %s WHERE inserted_at <= (CURRENT_DATE - $1::interval)", table)) | ||||
if err != nil { | ||||
Log(ERROR, fmt.Sprintf("unable to prepare statement %s", err)) | ||||
return err | ||||
} | ||||
|
||||
res, err := stmt.Exec(interval) | ||||
if err != nil { | ||||
Log(ERROR, "unable to delete block rows") | ||||
return err | ||||
} | ||||
rows, _ := res.RowsAffected() | ||||
Log(INFO, fmt.Sprintf("Successfully deleted %d rows", rows)) | ||||
return nil | ||||
} | ||||
|
||||
func ClearMetamorph(params ClearRecordsParams) error { | ||||
if err := runDelete("blocks", params); err != nil { | ||||
return err | ||||
} | ||||
if err := runDelete("transactions", params); err != nil { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same here
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, why do we need to hardcode the database name? |
||||
return err | ||||
} | ||||
|
||||
return nil | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package jobs | ||
|
||
import ( | ||
. "github.com/bitcoin-sv/arc/database_testing" | ||
"github.com/bitcoin-sv/arc/metamorph/store" | ||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type ClearMetamorphSuite struct { | ||
MetamorphDBTestSuite | ||
} | ||
|
||
func (s *ClearMetamorphSuite) Test() { | ||
|
||
for i := 0; i < 5; i++ { | ||
blk := GetTestMMBlock() | ||
blk.InsertedAt = time.Now().Add(-20 * 24 * time.Hour) | ||
s.InsertBlock(blk) | ||
} | ||
|
||
for i := 0; i < 5; i++ { | ||
blk := GetTestMMBlock() | ||
blk.InsertedAt = time.Now().Add(-1 * 24 * time.Hour) | ||
s.InsertBlock(blk) | ||
} | ||
|
||
for i := 0; i < 5; i++ { | ||
tx := GetTestMMTransaction() | ||
tx.InsertedAt = time.Now().Add(-20 * 24 * time.Hour) | ||
|
||
s.InsertTransaction(tx) | ||
} | ||
|
||
for i := 0; i < 5; i++ { | ||
tx := GetTestMMTransaction() | ||
tx.InsertedAt = time.Now().Add(-1 * 24 * time.Hour) | ||
|
||
s.InsertTransaction(tx) | ||
} | ||
|
||
err := ClearMetamorph(ClearRecordsParams{ | ||
DBConnectionParams: DefaultMMParams, | ||
RecordRetentionDays: 14, | ||
}) | ||
|
||
require.NoError(s.T(), err) | ||
|
||
db, err := sqlx.Open("postgres", DefaultMMParams.String()) | ||
require.NoError(s.T(), err) | ||
|
||
var blks []store.Block | ||
require.NoError(s.T(), db.Select(&blks, "SELECT * from blocks")) | ||
|
||
assert.Len(s.T(), blks, 5) | ||
|
||
var stx []store.Transaction | ||
require.NoError(s.T(), db.Select(&stx, "SELECT * from transactions")) | ||
assert.Len(s.T(), stx, 5) | ||
|
||
} | ||
|
||
func TestRunClearMM(t *testing.T) { | ||
s := new(ClearMetamorphSuite) | ||
suite.Run(t, s) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE transactions; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you copy these files from this path: metamorph/store/postgresql/migrations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The migrations are still not exaclty the same. This schema is missing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why does it have to have custom schema? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
CREATE TABLE transactions | ||
( | ||
hash BYTEA PRIMARY KEY, | ||
stored_at TIMESTAMPTZ, | ||
announced_at TIMESTAMPTZ, | ||
mined_at TIMESTAMPTZ, | ||
status INTEGER, | ||
block_height BIGINT, | ||
block_hash BYTEA, | ||
callback_url TEXT, | ||
callback_token TEXT, | ||
merkle_proof TEXT, | ||
reject_reason TEXT, | ||
raw_tx BYTEA, | ||
locked_by TEXT, | ||
inserted_at TIMESTAMPTZ | ||
); | ||
|
||
CREATE INDEX ix_metamorph_transactions_locked_by ON transactions (locked_by); | ||
CREATE INDEX ix_metamorph_transactions_inserted_at_num ON transactions (inserted_at); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE blocks; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE blocks | ||
( | ||
hash BYTEA PRIMARY KEY, | ||
processed_at TIMESTAMPTZ, | ||
inserted_at TIMESTAMPTZ | ||
); | ||
|
||
CREATE INDEX ix_metamorph_blocks_inserted_at ON blocks (inserted_at); |
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.
After adding the schema the from clause would be
metamorph.blocks
like herearc/metamorph/store/postgresql/postgres.go
Line 603 in fbe2265
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.
I'm not sure it's a good idea to hardcode database name inside the query. Because if the name inside db connection can be different from the one inside the query.