-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathscript.sql
36 lines (35 loc) · 1022 Bytes
/
script.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
DO $$ BEGIN
CREATE TYPE "transaction_type" AS ENUM('c', 'd');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "clients" (
"id" serial PRIMARY KEY NOT NULL,
"balance" integer DEFAULT 0 NOT NULL,
"limit" integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "transactions" (
"id" serial PRIMARY KEY NOT NULL,
"client_id" integer NOT NULL,
"amount" integer NOT NULL,
"transaction_type" "transaction_type" NOT NULL,
"description" varchar(10) NOT NULL,
"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_client_id_clients_id_fk" FOREIGN KEY ("client_id") REFERENCES "clients"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
DO $$ BEGIN
INSERT INTO clients("limit")
VALUES
(1000 * 100),
(800 * 100),
(10000 * 100),
(100000 * 100),
(5000 * 100);
END $$;