Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions db/db_postgres.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,34 @@ static u64 db_postgres_column_u64(struct db_stmt *stmt, int col)

static s64 db_postgres_column_int(struct db_stmt *stmt, int col)
{
PGresult *res = (PGresult*)stmt->inner_stmt;
be32 bin;
size_t expected = sizeof(bin), actual = PQgetlength(res, stmt->row, col);

if (expected != actual)
db_fatal(stmt->db,
"s32 field doesn't match size: expected %zu, actual %zu\n",
expected, actual);

memcpy(&bin, PQgetvalue(res, stmt->row, col), sizeof(bin));
return be32_to_cpu(bin);
PGresult *res = (PGresult*)stmt->inner_stmt;
Oid col_type = PQftype(res, col); // Get the column's type OID

if (col_type == INT4OID) {
/* This is a 32-bit integer (PG INT or CRDB INT4) */
if (PQgetlength(res, stmt->row, col) != 4) {
/* This check is now for safety/correctness */
db_fatal(stmt->db, "INT4 field size not 4 bytes\n");
}
be32 bin;
memcpy(&bin, PQgetvalue(res, stmt->row, col), sizeof(bin));
return (s64)be32_to_cpu(bin); // Cast to s64 for return
}
else if (col_type == INT8OID) {
/* This is a 64-bit integer (PG BIGINT or CRDB INT) */
if (PQgetlength(res, stmt->row, col) != 8) {
db_fatal(stmt->db, "INT8 field size not 8 bytes\n");
}
be64 bin;
memcpy(&bin, PQgetvalue(res, stmt->row, col), sizeof(bin));
return be64_to_cpu(bin);
}

/* This function was called on an unsupported column type */
db_fatal(stmt->db,
"integer field type unexpected: expected INT4 (23) or INT8 (20), actual %u\n",
col_type);
return 0;
}

static size_t db_postgres_column_bytes(struct db_stmt *stmt, int col)
Expand Down
8 changes: 4 additions & 4 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ static struct migration dbmigrations[] = {
{SQL("UPDATE invoices SET pay_index=id WHERE state=1;"),
NULL}, /* only paid invoice */
/* Create next_pay_index variable (highest pay_index). */
{SQL("INSERT INTO vars(name, val)"
{SQL("INSERT INTO vars(name, val)"
" VALUES('next_pay_index', "
" COALESCE((SELECT MAX(pay_index) FROM invoices WHERE state=1), 0) "
"+ 1"
" CAST(COALESCE((SELECT MAX(pay_index) FROM invoices WHERE state=1), 0) "
"+ 1 AS TEXT)"
" );"),
NULL},
/* Create first_block field; initialize from channel id if any.
Expand Down Expand Up @@ -943,7 +943,7 @@ static struct migration dbmigrations[] = {
" in_channel_scid"
", COALESCE("
" (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.in_htlc_id),"
" -_ROWID_"
" -row_number() OVER ()"
Comment on lines -939 to +946
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think you missed the translation layer?

See devtools/sql-rewrite.py...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that sql-rewrite.py rewrite _ROWID_, but I don't get what is exactly the flow to use it, how do I bootstrap a lightning node with cockroach db?

" )"
", out_channel_scid"
", (SELECT channel_htlc_id FROM channel_htlcs WHERE id = forwarded_payments.out_htlc_id)"
Expand Down
Loading