MoonMiners/sql/queries-psql.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

90 lines
2.7 KiB
SQL

select * from users;
select * from resource;
select * from resource_account;
select * from bank_account;
select * from resource_account
join users on resource_account.user_id = users.id
join resource on resource.id = resource_account.resource_id
where users.name = 'Harry';
select * from bank_account where user_id = 'c40ef029-9c31-4bf8-8bb4-d6f63caeb351';
delete from users where id = 'c40ef029-9c31-4bf8-8bb4-d6f63caeb351';
select * from get_accounts( 'c40ef029-9c31-4bf8-8bb4-d6f63caeb351');
update bank_account set balance = 1200 from users where bank_account.user_id = users.id AND name = 'Joe';
select * from inventory_item;
select * from upgrade_event;
delete from upgrade_event;
update inventory_item set tier = 0;
select * from staking_event;
update staking_event set created_at = '2023-03-30 05:05:39.696926+00';
select * from claim_event;
delete from claim_event;
-- Grab a user's bank account plus their resource accounts
EXPLAIN ANALYZE
SELECT
bank_account.id as bank_account_id,
bank_account.balance as primary_balance,
json_agg(json_build_object('resourceType', resource.name, 'balance', resource_account.balance)) as resource_accounts
FROM bank_account
JOIN resource_account ON bank_account.user_id = resource_account.user_id
JOIN resource ON resource.id = resource_account.resource_id
JOIN users ON bank_account.user_id = users.id
WHERE users.name = 'Joe'
GROUP BY bank_account.id;
-- Grab a user's bank account plus their resource accounts
EXPLAIN ANALYZE
SELECT
bank_account.id as bank_account_id,
bank_account.balance as primary_balance,
resname as resourceType,
resource_account.balance,
resource_account.id as resId
FROM bank_account
JOIN resource_account ON 1 = resource_account.user_id
WHERE bank_account.user_id = 1;
-- Grab a staking source with all its resource wells and active stakes
SELECT
staking_source.id as id, name, description,
json_agg(
json_build_object(
'id', resource_well.id, 'resouceType', resource_well.resname, 'supply', supply )) as "resourceWells",
json_agg(
json_build_object(
'id', staking_event.id, 'resouceType', resource_well.resname, 'startTime', staking_event.created_at)) as "activeStakes"
FROM staking_source
INNER JOIN resource_well ON resource_well.source_id = staking_source.id
INNER JOIN staking_event ON staking_event.well_id = resource_well.id
WHERE staking_source.user_id = 1
GROUP BY staking_source.id;
SELECT
inventory_item.id AS inv_id,
staking_event.id AS stake_id,
staking_event.created_at AS stake_created_at
FROM inventory_item
LEFT JOIN staking_event ON inventory_item_id = inventory_item.id
WHERE inventory_item.id = 1 AND inventory_item.user_id = 1
ORDER BY stake_created_at DESC
LIMIT 1;