-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathinit.sql
52 lines (49 loc) · 1.64 KB
/
init.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
CREATE UNLOGGED TABLE IF NOT EXISTS "balances" (
"id" serial PRIMARY KEY NOT NULL,
"costumerId" integer,
"value" integer DEFAULT 0 NOT NULL
);
--> statement-breakpoint
CREATE UNLOGGED TABLE IF NOT EXISTS "costumers" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar NOT NULL,
"limit" integer NOT NULL
);
--> statement-breakpoint
CREATE UNLOGGED TABLE IF NOT EXISTS "transactions" (
"id" serial PRIMARY KEY NOT NULL,
"costumerId" integer,
"value" integer DEFAULT 0 NOT NULL,
"transactionType" varchar,
"description" varchar(10),
"createdAt" timestamp DEFAULT now()
);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "costumerBalanceIdx" ON "balances" ("costumerId");--> statement-breakpoint
CREATE INDEX IF NOT EXISTS "costumerTransactionIdx" ON "transactions" ("costumerId");--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "balances" ADD CONSTRAINT "balances_costumerId_costumers_id_fk" FOREIGN KEY ("costumerId") REFERENCES "costumers"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "transactions" ADD CONSTRAINT "transactions_costumerId_costumers_id_fk" FOREIGN KEY ("costumerId") REFERENCES "costumers"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
DO $$
BEGIN
INSERT INTO "costumers" ("id", "name", "limit") VALUES
(1, 'Eduardo', 100000),
(2, 'José', 80000),
(3, 'Maria', 1000000),
(4, 'Paula', 10000000),
(5, 'Pedro', 500000);
INSERT INTO "balances" ("id", "costumerId", "value") VALUES
(1, 1, 0),
(2, 2, 0),
(3, 3, 0),
(4, 4, 0),
(5, 5, 0);
END $$;