delphi.market/db/init.sql

69 lines
2.4 KiB
MySQL
Raw Normal View History

2024-07-09 12:33:56 +02:00
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
2023-09-09 22:52:50 +02:00
CREATE TABLE lnauth(
k1 VARCHAR(64) NOT NULL PRIMARY KEY,
lnurl TEXT NOT NULL,
2023-09-09 22:52:50 +02:00
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
2023-09-09 22:52:50 +02:00
session_id VARCHAR(48) NOT NULL DEFAULT encode(gen_random_uuid()::text::bytea, 'base64')
);
2023-09-09 22:52:50 +02:00
CREATE TABLE users(
pubkey TEXT PRIMARY KEY,
2023-12-02 22:08:01 +01:00
last_seen TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
msats BIGINT NOT NULL DEFAULT 0
2023-09-09 22:52:50 +02:00
);
2023-09-09 22:52:50 +02:00
CREATE TABLE sessions(
2023-09-09 22:52:50 +02:00
pubkey TEXT NOT NULL REFERENCES users(pubkey),
2023-09-09 22:52:50 +02:00
session_id VARCHAR(48)
);
2023-09-09 22:52:51 +02:00
CREATE TABLE invoices(
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
pubkey TEXT NOT NULL REFERENCES users(pubkey),
msats BIGINT NOT NULL,
msats_received BIGINT,
preimage TEXT NOT NULL UNIQUE,
hash TEXT NOT NULL UNIQUE,
bolt11 TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
expires_at TIMESTAMP WITH TIME ZONE NOT NULL,
confirmed_at TIMESTAMP WITH TIME ZONE,
2023-11-09 03:47:24 +01:00
held_since TIMESTAMP WITH TIME ZONE,
description TEXT
2023-09-09 22:52:51 +02:00
);
2023-11-20 14:18:50 +01:00
CREATE TABLE markets(
id SERIAL PRIMARY KEY,
description TEXT NOT NULL,
end_date TIMESTAMP WITH TIME ZONE NOT NULL,
2023-12-03 23:52:24 +01:00
settled_at TIMESTAMP WITH TIME ZONE,
2023-12-03 07:22:13 +01:00
pubkey TEXT NOT NULL REFERENCES users(pubkey),
2023-11-20 14:18:50 +01:00
invoice_id UUID NOT NULL UNIQUE REFERENCES invoices(id)
);
CREATE TABLE shares(
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
2023-12-04 04:56:01 +01:00
market_id INTEGER NOT NULL REFERENCES markets(id),
2023-12-04 02:25:41 +01:00
win BOOLEAN,
2023-11-20 14:18:50 +01:00
description TEXT NOT NULL
);
2023-09-09 22:52:50 +02:00
CREATE TYPE order_side AS ENUM ('BUY', 'SELL');
2023-09-09 22:52:50 +02:00
CREATE TABLE orders(
2023-09-09 22:52:50 +02:00
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
2023-09-09 22:52:51 +02:00
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
2023-12-02 22:08:01 +01:00
deleted_at TIMESTAMP WITH TIME ZONE,
share_id UUID NOT NULL REFERENCES shares(id),
2023-09-09 22:52:50 +02:00
pubkey TEXT NOT NULL REFERENCES users(pubkey),
side ORDER_SIDE NOT NULL,
quantity BIGINT NOT NULL,
price BIGINT NOT NULL,
2023-11-29 18:41:24 +01:00
invoice_id UUID REFERENCES invoices(id),
2023-11-27 17:45:07 +01:00
order_id UUID REFERENCES orders(id)
2023-09-09 22:52:50 +02:00
);
2023-09-09 22:52:51 +02:00
ALTER TABLE orders ADD CONSTRAINT order_price CHECK(price > 0 AND price < 100);
2023-11-26 18:31:22 +01:00
ALTER TABLE orders ADD CONSTRAINT order_quantity CHECK(quantity > 0);
2023-12-03 06:21:57 +01:00
CREATE TABLE withdrawals(
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE,
pubkey TEXT NOT NULL REFERENCES users(pubkey),
bolt11 TEXT NOT NULL UNIQUE,
paid_at TIMESTAMP WITH TIME ZONE
);