Files
simplex-chat/apps/simplex-badge-service/src/BadgeService/Store/Postgres/Migrations.hs
T

86 lines
2.6 KiB
Haskell

{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module BadgeService.Store.Postgres.Migrations (badgeServiceSchemaMigrations) where
import Data.List (sortOn)
import Data.Text (Text)
import Simplex.Chat.Store.Postgres.Migrations.M20260915_user_badges (badgeSchema, badgeSchemaDown, withPrefix)
import Simplex.Messaging.Agent.Store.Shared (Migration (..))
import Text.RawString.QQ (r)
badgeServiceSchemaMigrations :: [Migration]
badgeServiceSchemaMigrations = sortOn name $ map migration schemaMigrations
where
migration (name, up, down) = Migration {name, up, down}
schemaMigrations :: [(String, Text, Maybe Text)]
schemaMigrations =
[ ("20260806_badge_service_schema", m20260806_badge_service_schema, Just down_m20260806_badge_service_schema)
]
-- the client tables are in the same database, so the service tables are the same names with this prefix
servicePrefix :: Text
servicePrefix = "sx_badge_service_"
m20260806_badge_service_schema :: Text
m20260806_badge_service_schema =
badgeSchema servicePrefix
<> withPrefix
servicePrefix
[r|
CREATE TABLE @badge_codes(
badge_code_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
code_hash BYTEA NOT NULL,
badge_type TEXT NOT NULL,
months SMALLINT NOT NULL,
code_payment_status TEXT NOT NULL,
redeemed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL,
UNIQUE(code_hash)
);
ALTER TABLE @badge_purchases ADD COLUMN badge_code_id BIGINT REFERENCES @badge_codes;
CREATE UNIQUE INDEX @idx_badge_purchases_code ON @badge_purchases(badge_code_id);
CREATE TABLE @badge_code_invoices(
invoice_id TEXT NOT NULL PRIMARY KEY REFERENCES @invoices ON DELETE CASCADE,
price_id TEXT NOT NULL REFERENCES @badge_prices,
offer_id TEXT REFERENCES @badge_offers,
months SMALLINT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX @idx_badge_code_invoices_offer ON @badge_code_invoices(offer_id);
CREATE INDEX @idx_badge_code_invoices_price ON @badge_code_invoices(price_id);
|]
down_m20260806_badge_service_schema :: Text
down_m20260806_badge_service_schema =
withPrefix
servicePrefix
[r|
DROP INDEX @idx_badge_code_invoices_offer;
DROP INDEX @idx_badge_code_invoices_price;
DROP TABLE @badge_code_invoices;
DROP INDEX @idx_badge_purchases_code;
|]
<> badgeSchemaDown servicePrefix
<> withPrefix
servicePrefix
[r|
DROP TABLE @badge_codes;
|]
{- TODO [badges] deferred with the draft in M20260915_user_badges, service only.
ALTER TABLE @payments ADD COLUMN receipt_hash BYTEA;
-- down
ALTER TABLE @payments DROP COLUMN receipt_hash;
-}