Skip to content

Commit

Permalink
sqlc: migration up script for payments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ziggie1984 committed Oct 8, 2024
1 parent bf49bff commit 5882edb
Show file tree
Hide file tree
Showing 3 changed files with 481 additions and 0 deletions.
73 changes: 73 additions & 0 deletions sqldb/sqlc/migrations/000005_payments.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
-- Drop hop_custom_records index and table.
DROP INDEX IF EXISTS hop_custom_records_hop_id_idx;
DROP TABLE IF EXISTS hop_custom_records;

-- Drop htlc_settle_info index and table.
DROP INDEX IF EXISTS htlc_settle_info_htlc_attempt_id_idx;
DROP TABLE IF EXISTS htlc_settle_info;

-- Drop htlc_fail_info index and table.
DROP INDEX IF EXISTS htlc_fail_info_htlc_attempt_id_idx;
DROP TABLE IF EXISTS htlc_fail_info;

-- Drop htlc_fail_reason_types index and table.
DROP INDEX IF EXISTS htlc_fail_reason_types_htlc_fail_reason_type_idx;
DROP TABLE IF EXISTS htlc_fail_reason_types;

-- Drop blinded_data index and table.
DROP INDEX IF EXISTS blinded_data_hop_id_idx;
DROP TABLE IF EXISTS blinded_data;

-- Drop mpp_record index and table.
DROP INDEX IF EXISTS mpp_record_payment_addr_idx;
DROP TABLE IF EXISTS mpp_record;

-- Drop amp_record index and table.
DROP INDEX IF EXISTS amp_record_set_id_idx;
DROP TABLE IF EXISTS amp_record;

-- Drop hop index and table.
DROP INDEX IF EXISTS hop_route_id_idx;
DROP TABLE IF EXISTS hop;

-- Drop route index and table.
DROP INDEX IF EXISTS route_htlc_attempt_id_idx;
DROP TABLE IF EXISTS route;

-- Drop htlc_attempt index and table.
DROP INDEX IF EXISTS htlc_attempt_payment_id_idx;
DROP TABLE IF EXISTS htlc_attempt;

-- Drop first_hop_custom_records index and table.
DROP INDEX IF EXISTS first_hop_custom_records_payment_id_idx;
DROP TABLE IF EXISTS first_hop_custom_records;

-- Drop mpp_state index and table.
DROP INDEX IF EXISTS mpp_state_payment_id_idx;
DROP TABLE IF EXISTS mpp_state;

-- Drop mpp_payments index and table.
DROP INDEX IF EXISTS mpp_payments_payment_id_idx;
DROP TABLE IF EXISTS mpp_payments;

-- Drop amp_payments index and table.
DROP INDEX IF EXISTS amp_payments_payment_id_idx;
DROP TABLE IF EXISTS amp_payments;

-- Drop legacy_payments index and table.
DROP INDEX IF EXISTS legacy_payments_payment_hash_idx;
DROP TABLE IF EXISTS legacy_payments;

-- Drop payment_types index and table.
DROP INDEX IF EXISTS payment_types_name_idx;
DROP TABLE IF EXISTS payment_types;

-- Drop payment_status_types index and table.
DROP INDEX IF EXISTS payment_status_types_name_idx;
DROP TABLE IF EXISTS payment_status_types;

-- Drop payments index and table.
DROP INDEX IF EXISTS payments_payment_status_idx;
DROP INDEX IF EXISTS payments_payment_type_idx;
DROP INDEX IF EXISTS payments_created_at_idx;
DROP TABLE IF EXISTS payments;
291 changes: 291 additions & 0 deletions sqldb/sqlc/migrations/000005_payments.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
-- payments table contains all the main information for a payment.
CREATE TABLE IF NOT EXISTS payments (
-- The id of the mpp payment.
id BIGINT PRIMARY KEY,

-- payment_status is the status of the payment.
payment_status INTEGER NOT NULL REFERENCES payment_status_types(id) ON DELETE CASCADE,

-- payment_type is the type of the payment.
payment_type INTEGER NOT NULL REFERENCES payment_types(id) ON DELETE CASCADE,

-- amount_msat is the amount of the payment in milli-satoshis.
amount_msat BIGINT NOT NULL,

-- payment_request is the payment request of the payment.
payment_request BLOB UNIQUE NOT NULL,

-- created_at is the timestamp of the payment.
created_at TIMESTAMP NOT NULL
);

CREATE INDEX IF NOT EXISTS payments_payment_status_idx ON payments(payment_status);
CREATE INDEX IF NOT EXISTS payments_payment_type_idx ON payments(payment_type);
CREATE INDEX IF NOT EXISTS payments_created_at_idx ON payments(created_at);


-- payment_status_types stores the different types of a mpp payment.
CREATE TABLE IF NOT EXISTS payment_status_types(
-- id is the id of the payment status type.
id INTEGER PRIMARY KEY,

-- description is the description of the payment status type.
description TEXT NOT NULL
);


-- invoice_event_types defines the different types of invoice events.
INSERT INTO payment_status_types (id, description)
VALUES
-- status_unknown is a deprecated status and describes the unknown payment
-- status.
(0, 'status_unknown'),

-- status_initiated is the status of a payment that has been initiated.
(1, 'status_initiated'),

-- status_inflight is the status of a payment that is in flight.
(2, 'status_inflight'),

-- status_succeeded is the status of a payment that has been succeeded.
(3, 'status_succeeded'),

-- status_failed is the status of a payment that has been failed.
(4, 'status_failed');


-- payment_types table stores the different types of a payment.
CREATE TABLE IF NOT EXISTS payment_types(
-- id is the id of the payment type.
id INTEGER PRIMARY KEY,

-- description is the description of the payment type.
description TEXT NOT NULL
);


-- Insert the payment types into the payment_types table.
INSERT INTO payment_types (id, description)
VALUES
-- legacy is a payment type that is a legacy payment.
(0, 'legacy'),

-- mpp is a payment type that is a mpp payment.
(1, 'mpp'),

-- amp is a payment type that is an amp payment.
(2, 'amp'),

-- blinded is a payment type that is a blinded payment.
(3, 'blinded');


-- legacy_payments table stores the legacy payments payment_hash.
CREATE TABLE legacy_payments (
payment_id INTEGER PRIMARY KEY REFERENCES payments(id) ON DELETE CASCADE,

payment_hash BLOB UNIQUE NOT NULL
);

CREATE INDEX IF NOT EXISTS legacy_payments_payment_hash_idx ON legacy_payments(payment_hash);


-- mpp_payments table stores stores information about the mpp payments.
CREATE TABLE mpp_payments (
payment_id INTEGER PRIMARY KEY REFERENCES payments(id) ON DELETE CASCADE,

mpp_record_id INTEGER REFERENCES mpp_record(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS mpp_payments_payment_id_idx ON mpp_payments(payment_id);
CREATE INDEX IF NOT EXISTS mpp_payments_mpp_record_id_idx ON mpp_payments(mpp_record_id);


-- amp_payments table stores stores information about the amp payments.
CREATE TABLE amp_payments (
payment_id INTEGER PRIMARY KEY REFERENCES payments(id) ON DELETE CASCADE,

amp_record_id INTEGER REFERENCES amp_record(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS amp_payments_payment_id_idx ON amp_payments(payment_id);
CREATE INDEX IF NOT EXISTS amp_payments_amp_record_id_idx ON amp_payments(amp_record_id);


-- payment_state table stores the state of multi path payments (amp and mpp).
CREATE TABLE payment_state (
payment_id INTEGER PRIMARY KEY REFERENCES payments(id) ON DELETE CASCADE,

num_attempts_in_flight INTEGER,

remaining_amt BIGINT NOT NULL,

fees_paid BIGINT NOT NULL,

has_settled_htlc BOOLEAN NOT NULL,

payment_failed BOOLEAN NOT NULL
);

CREATE INDEX IF NOT EXISTS payment_state_payment_id_idx ON payment_state(payment_id);

-- htlc_attempt table stores the htlc attempt information.
CREATE TABLE htlc_attempt (
id INTEGER PRIMARY KEY,

session_key BLOB UNIQUE NOT NULL,

attempt_time TIMESTAMP NOT NULL,

payment_id INTEGER REFERENCES payments(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS htlc_attempt_payment_id_idx ON htlc_attempt(payment_id);

-- route table stores the route information of an htlc attempt.
CREATE TABLE route (
htlc_attempt_id INTEGER PRIMARY KEY REFERENCES htlc_attempt(id) ON DELETE CASCADE,

total_timeLock INTEGER,

total_amount BIGINT NOT NULL,

source_key BLOB NOT NULL,

first_hop_amount BIGINT
);

-- hop table stores the hop information which a route is composed of.
CREATE TABLE hop (
id INTEGER PRIMARY KEY,

route_id INTEGER REFERENCES route(id) ON DELETE CASCADE,

pub_key BLOB UNIQUE NOT NULL,

chan_id TEXT UNIQUE NOT NULL,

outgoing_time_lock INTEGER NOT NULL,

amt_to_forward BIGINT NOT NULL,

meta_data BLOB
);

CREATE INDEX IF NOT EXISTS hop_route_id_idx ON hop(route_id);

-- mpp_record table stores the mpp record information.
CREATE TABLE mpp_record (
hop_id INTEGER PRIMARY KEY REFERENCES hop(id) ON DELETE CASCADE,

payment_addr BLOB UNIQUE NOT NULL,

total_msat BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS mpp_record_payment_addr_idx ON mpp_record(payment_addr);

-- amp_record table stores the amp record information.
CREATE TABLE amp_record (
hop_id INTEGER PRIMARY KEY REFERENCES hop(id) ON DELETE CASCADE,

root_share BLOB UNIQUE NOT NULL,

set_id BLOB UNIQUE NOT NULL,

child_index INTEGER NOT NULL
);

CREATE INDEX IF NOT EXISTS amp_record_set_id_idx ON amp_record(set_id);

-- tlv_records table stores the tlv records for hops or payments.
CREATE TABLE tlv_records (
id INTEGER PRIMARY KEY,

key BIGINT NOT NULL,

value BLOB NOT NULL,
);

-- custom_records table stores the custom tlv records for a hop.
CREATE TABLE custom_records (
tlv_record_id INTEGER PRIMARY KEY REFERENCES tlv_records(id) ON DELETE CASCADE,

hop_id INTEGER REFERENCES hop(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS custom_records_tlv_record_id_idx ON custom_records(tlv_record_id );
CREATE INDEX IF NOT EXISTS custom_records_hop_id_idx ON custom_records(hop_id);

-- first_hop_custom_records table stores the first hop custom records which are
-- only send as a wire message to the first hop and are used for overlay
-- channels.
CREATE TABLE first_hop_custom_records (
tlv_record_id INTEGER PRIMARY KEY REFERENCES tlv_records(id) ON DELETE CASCADE,

payment_id INTEGER REFERENCES payments(id) ON DELETE CASCADE
);

CREATE INDEX IF NOT EXISTS first_hop_custom_records_tlv_record_id_idx ON first_hop_custom_records(tlv_record_id);
CREATE INDEX IF NOT EXISTS first_hop_custom_records_payment_id_idx ON first_hop_custom_records(payment_id);

-- blinded_data table stores the blinded data.
CREATE TABLE blinded_data (
hop_id INTEGER PRIMARY KEY REFERENCES hop(id) ON DELETE CASCADE,

encrypted_data BLOB UNIQUE NOT NULL,

blinding_point BLOB UNIQUE,

total_amt BIGINT NOT NULL
);

CREATE INDEX IF NOT EXISTS blinded_data_hop_id_idx ON blinded_data(hop_id);

-- htlc_settle_info table stores the htlc settle information.
CREATE TABLE htlc_settle_info (
htlc_attempt_id INTEGER PRIMARY KEY REFERENCES htlc_attempt(id) ON DELETE CASCADE,

preimage BLOB UNIQUE NOT NULL,

settle_time TIMESTAMP NOT NULL
);

CREATE INDEX IF NOT EXISTS htlc_settle_info_htlc_attempt_id_idx ON htlc_settle_info(htlc_attempt_id);
CREATE INDEX IF NOT EXISTS htlc_settle_info_preimage_idx ON htlc_settle_info(preimage);

-- htlc_fail_info table stores the htlc fail information.
CREATE TABLE htlc_fail_info (
htlc_attempt_id INTEGER PRIMARY KEY REFERENCES htlc_attempt(id) ON DELETE CASCADE,

htlc_fail_reason INTEGER NOT NULL REFERENCES htlc_fail_reason_types(id) ON DELETE CASCADE,

failure_msg TEXT
);

CREATE INDEX IF NOT EXISTS htlc_fail_info_htlc_attempt_id_idx ON htlc_fail_info(htlc_attempt_id);
CREATE INDEX IF NOT EXISTS htlc_fail_info_htlc_fail_reason_idx ON htlc_fail_info(htlc_fail_reason);

-- htlc_fail_reason_types describes the different types of htlc fail reasons.
CREATE TABLE IF NOT EXISTS htlc_fail_reason_types(
-- id is the id of the htlc fail reason type.
id INTEGER PRIMARY KEY,

-- description is the description of the htlc fail reason type.
description TEXT NOT NULL
);

INSERT INTO htlc_fail_reason_types (id, description)
VALUES
-- htlc_fail_reason_unknown is a htlc fail reason type that is unknown.
(0, 'htlc_fail_reason_unknown'),

-- htlc_fail_reason_timeout is a htlc fail reason type that is a timeout.
(1, 'htlc_fail_reason_unreadable'),

-- htlc_fail_reason_timeout is a htlc fail reason type that is a timeout.
(2, 'htlc_fail_reason_internal'),

-- htlc_fail_reason_timeout is a htlc fail reason type that is a timeout.
(3, 'htlc_fail_reason_message');

Loading

0 comments on commit 5882edb

Please sign in to comment.