Skip to content

Commit

Permalink
update database schema
Browse files Browse the repository at this point in the history
  • Loading branch information
thecotne committed Feb 5, 2024
1 parent 03a076f commit 7738d1a
Show file tree
Hide file tree
Showing 22 changed files with 594 additions and 545 deletions.
3 changes: 0 additions & 3 deletions db/init-materialized-views.sql

This file was deleted.

168 changes: 0 additions & 168 deletions db/migrations/20240113081901_init.sql

This file was deleted.

19 changes: 19 additions & 0 deletions db/migrations/20240204202436_event.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- migrate:up
CREATE EXTENSION IF NOT EXISTS moddatetime;
CREATE TABLE public.event(
"blockHash" character varying(65),
"blockIndex" integer,
"transactionHash" character varying(65) NOT NULL,
"transactionIndex" integer NOT NULL,
"eventIndex" integer NOT NULL,
"eventName" character varying NOT NULL,
"eventData" jsonb NOT NULL,
"createdAt" timestamp without time zone DEFAULT now() NOT NULL,
"updatedAt" timestamp without time zone DEFAULT now() NOT NULL
);
ALTER TABLE public.event
ADD CONSTRAINT "event_pkey" PRIMARY KEY ("transactionHash", "eventIndex");
CREATE TRIGGER mdt_event BEFORE
UPDATE ON event FOR EACH ROW EXECUTE PROCEDURE moddatetime ("updatedAt");
-- migrate:down
DROP TABLE public.event;
51 changes: 51 additions & 0 deletions db/migrations/20240204202447_balance.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- migrate:up
CREATE VIEW public.transfer_events AS (
SELECT event."eventData"->>'from' AS "transferFrom",
event."eventData"->>'to' AS "transferTo",
(event."eventData"->>'value')::numeric AS "transferAmount",
event."blockHash",
event."blockIndex",
event."transactionHash",
event."transactionIndex",
event."eventIndex",
event."createdAt",
event."updatedAt"
FROM public.event
WHERE event."eventName" = 'Transfer'
ORDER BY "blockIndex" DESC,
"transactionIndex" DESC,
"eventIndex" DESC
);
CREATE VIEW public.balance_events AS (
SELECT transfer_events."transferTo" AS "userId",
transfer_events."transferAmount" AS "balance",
transfer_events."blockHash",
transfer_events."blockIndex",
transfer_events."transactionHash",
transfer_events."transactionIndex",
transfer_events."eventIndex",
transfer_events."createdAt",
transfer_events."updatedAt"
FROM public.transfer_events
UNION ALL
SELECT transfer_events."transferFrom" AS "userId",
0 - transfer_events."transferAmount" AS "balance",
transfer_events."blockHash",
transfer_events."blockIndex",
transfer_events."transactionHash",
transfer_events."transactionIndex",
transfer_events."eventIndex",
transfer_events."createdAt",
transfer_events."updatedAt"
FROM public.transfer_events
);
CREATE VIEW public.balance AS (
SELECT "userId",
sum(balance) AS balance
from balance_events
GROUP BY "userId"
);
-- migrate:down
drop VIEW public.balance;
drop VIEW public.balance_events;
drop VIEW public.transfer_events;
73 changes: 73 additions & 0 deletions db/migrations/20240204204718_game.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
-- migrate:up
CREATE VIEW public.game_events AS (
SELECT event."blockHash",
event."blockIndex",
event."transactionHash",
event."transactionIndex",
event."eventData"->>'user_id' AS "transactionOwner",
CASE
event."blockIndex"
WHEN NULL THEN 'PENDING'
ELSE 'ACCEPTED_ON_L2'
END as "transactionStatus",
event."eventIndex",
event."eventName",
event."eventData",
event."createdAt",
event."updatedAt",
CASE
WHEN event."eventData" ? 'game_id' THEN event."eventData"->>'game_id'
ELSE '0x7300100008000000000000000000000000'
END as "gameId",
CASE
WHEN event."eventData" ? 'generation' THEN (event."eventData"->>'generation')::numeric
ELSE 1
END as "gameGeneration",
(event."eventData"->>'state')::numeric AS "gameState",
(event."eventData"->>'cell_index')::numeric AS "revivedCellIndex",
(event."eventData"->>'state')::numeric = 0 as "gameOver"
FROM public.event
WHERE event."eventName" in (
'GameCreated',
'GameEvolved',
'CellRevived'
)
order by event."blockIndex" desc,
event."transactionIndex" desc,
event."eventIndex" desc
);
CREATE VIEW public.game_state AS (
SELECT e."gameId",
e."gameGeneration",
e."gameState",
e."gameState" = 0 as "gameOver",
(
SELECT jsonb_agg(
jsonb_build_object(
'cell_index',
rci."revivedCellIndex",
'user_id',
rci."transactionOwner"
)
)
from public.game_events rci
WHERE rci."gameId" = e."gameId"
AND rci."gameGeneration" = e."gameGeneration"
AND rci."eventName" = 'CellRevived'
) as "revivedCells"
FROM public.game_events e
WHERE "eventName" in ('GameCreated', 'GameEvolved')
ORDER BY "blockIndex" DESC,
"transactionIndex" DESC,
"eventIndex" DESC
);
CREATE VIEW public.game AS (
SELECT distinct on ("gameId") *
FROM public.game_state
ORDER BY "gameId",
"gameGeneration" DESC
);
-- migrate:down
DROP VIEW public.game;
DROP VIEW public.game_state;
DROP VIEW public.game_events;
Loading

0 comments on commit 7738d1a

Please sign in to comment.