infoalloggi-monorepo/apps/db/migrations/016_payments.up.sql
Marco Pedone 66ef4a7fbd feat(database): Add migrations for chat labels, messages, services, interests, announcements, pricing, orders, payments, and miscellaneous data
- Created tables for `chats_etichette`, `messages`, `servizio`, `servizio_interessi`, `servizio_annunci`, `prezziario`, `ordini`, `payments`, and `banlist`.
- Established primary and foreign key constraints for data integrity.
- Introduced enums for various types including `TipologiaPosizioneEnum`, `OrderTypeEnum`, and `PaymentStatusEnum`.
- Seeded initial data for banners, labels, flags, pricing, strings, and users.
- Implemented unique constraints where necessary to prevent duplicate entries.
2025-10-29 16:59:20 +01:00

61 lines
No EOL
1.9 KiB
SQL

-- Payments Status Enum
DO $$ BEGIN
CREATE TYPE public."PaymentStatusEnum" AS ENUM ('processing', 'success', 'failed');
EXCEPTION
WHEN duplicate_object THEN NULL;
END $$;
-- Payments Table
CREATE TABLE IF NOT EXISTS public.payments (
id UUID DEFAULT gen_random_uuid () NOT NULL,
userid UUID NOT NULL,
amount_cent integer NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
paymentmethod TEXT,
paymentname TEXT NOT NULL,
paid_at TIMESTAMP WITHOUT TIME ZONE,
intent_id TEXT,
paymentstatus public."PaymentStatusEnum",
servizio_id UUID NOT NULL,
ordine_id UUID NOT NULL
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.payments
ADD CONSTRAINT payments_pkey PRIMARY KEY (id);
EXCEPTION
WHEN invalid_table_definition THEN
RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Foreign Key to Ordini
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'payments_ordine_fkey'
) THEN
ALTER TABLE public.payments
ADD CONSTRAINT payments_ordine_fkey
FOREIGN KEY (ordine_id) REFERENCES public.ordini (ordine_id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;
-- Foreign Key to Servizio
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'payments_servizio_fkey'
) THEN
ALTER TABLE public.payments
ADD CONSTRAINT payments_servizio_fkey
FOREIGN KEY (servizio_id) REFERENCES public.servizio (servizio_id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;
-- Foreign Key to Users
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'payments_userid_fkey'
) THEN
ALTER TABLE public.payments
ADD CONSTRAINT payments_userid_fkey
FOREIGN KEY (userid) REFERENCES public.users (id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;