MoonMiners/sql/tables-sqlite.sql
Joseph Ferano 9b753cf103 Porting database from SQLite to PostgreSQL
- New Postgres table schemas
- Using Stored Procedures with transactions that validate business logic
- User Ids now use UUID
- Updated and simplified all endpoints to call the stored procedures

Notes: There are still a few things missing that broke because of the migration,
in particular, because we moved a lot of the business logic into the database,
we now require that certain data that lived in the game-config.json to be
present in the database as well, to prevent cheating and truly have a single
source of truth.
2023-03-30 14:13:30 +07:00

89 lines
2.6 KiB
SQL

PRAGMA foreign_keys = ON;
CREATE TABLE users (
id integer primary key autoincrement,
wallet varchar,
name varchar(32) not null
);
CREATE TABLE staking_source(
id integer primary key autoincrement,
name varchar not null,
description varchar not null,
user_id int not null,
address varchar(128) not null,
created_at timestamp DEFAULT (current_timestamp || 'Z'),
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id)
);
CREATE TABLE resource_well(
id integer primary key autoincrement,
resname varchar not null,
source_id int not null,
supply int not null,
CONSTRAINT fk_sid FOREIGN KEY(source_id)
REFERENCES staking_source(id)
ON DELETE CASCADE
);
CREATE TABLE inventory_item(
id integer primary key autoincrement,
user_id int not null,
tier int not null default 0,
store_item_id varchar not null unique,
created_at timestamp DEFAULT (current_timestamp || 'Z'),
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id)
);
CREATE TABLE upgrade_event(
id integer primary key autoincrement,
inventory_item_id int not null,
created_at timestamp DEFAULT (current_timestamp || 'Z'),
CONSTRAINT fk_iid FOREIGN KEY(inventory_item_id)
REFERENCES inventory_item(id)
);
CREATE TABLE staking_event(
id integer primary key autoincrement,
user_id int not null,
well_id int not null,
inventory_item_id int not null,
duration_in_mins int not null,
stake_amount int not null,
created_at timestamp DEFAULT (current_timestamp || 'Z'),
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id)
CONSTRAINT fk_wid FOREIGN KEY(well_id)
REFERENCES resource_well(id)
CONSTRAINT fk_iiid FOREIGN KEY(inventory_item_id)
REFERENCES inventory_item(id)
);
CREATE TABLE claim_event(
id integer primary key autoincrement,
staking_event_id int not null,
claim_amount int not null,
created_at timestamp DEFAULT (current_timestamp || 'Z'),
CONSTRAINT fk_se_id FOREIGN KEY(staking_event_id)
REFERENCES staking_event(id)
);
CREATE TABLE bank_account(
id integer primary key autoincrement,
user_id int not null,
balance int not null default 0 CHECK (balance >= 0),
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id)
);
CREATE TABLE resource_account(
id integer primary key autoincrement,
resname varchar not null,
user_id int not null,
balance int not null default 0 CHECK (balance >= 0),
CONSTRAINT fk_user FOREIGN KEY(user_id)
REFERENCES users(id)
);