forked from zanfranceschi/rinha-de-backend-2024-q1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sql
114 lines (99 loc) · 3.61 KB
/
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
"MigrationId" character varying(150) NOT NULL,
"ProductVersion" character varying(32) NOT NULL,
CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);
START TRANSACTION;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306010311_versao_inicial') THEN
CREATE TABLE "Clientes" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
"Limite" integer NOT NULL,
"Saldo" integer NOT NULL,
CONSTRAINT "PK_Clientes" PRIMARY KEY ("Id")
);
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306010311_versao_inicial') THEN
INSERT INTO "Clientes" ("Id", "Limite", "Saldo")
VALUES (1, 100000, 0);
INSERT INTO "Clientes" ("Id", "Limite", "Saldo")
VALUES (2, 80000, 0);
INSERT INTO "Clientes" ("Id", "Limite", "Saldo")
VALUES (3, 1000000, 0);
INSERT INTO "Clientes" ("Id", "Limite", "Saldo")
VALUES (4, 10000000, 0);
INSERT INTO "Clientes" ("Id", "Limite", "Saldo")
VALUES (5, 500000, 0);
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306010311_versao_inicial') THEN
PERFORM setval(
pg_get_serial_sequence('"Clientes"', 'Id'),
GREATEST(
(SELECT MAX("Id") FROM "Clientes") + 1,
nextval(pg_get_serial_sequence('"Clientes"', 'Id'))),
false);
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306010311_versao_inicial') THEN
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20240306010311_versao_inicial', '8.0.2');
END IF;
END $EF$;
COMMIT;
START TRANSACTION;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306101043_adiciona_transacoes') THEN
ALTER TABLE "Clientes" DROP CONSTRAINT "PK_Clientes";
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306101043_adiciona_transacoes') THEN
ALTER TABLE "Clientes" RENAME TO clientes;
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306101043_adiciona_transacoes') THEN
ALTER TABLE clientes ADD CONSTRAINT "PK_clientes" PRIMARY KEY ("Id");
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306101043_adiciona_transacoes') THEN
CREATE TABLE transacoes (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
"ClienteId" integer NOT NULL,
"Data" timestamp with time zone NOT NULL,
"Valor" integer NOT NULL,
"Tipo" character varying(1) NOT NULL,
"Descricao" character varying(10) NOT NULL,
CONSTRAINT "PK_transacoes" PRIMARY KEY ("Id"),
CONSTRAINT "FK_transacoes_clientes_ClienteId" FOREIGN KEY ("ClienteId") REFERENCES clientes ("Id") ON DELETE CASCADE
);
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306101043_adiciona_transacoes') THEN
CREATE INDEX "IX_transacoes_ClienteId" ON transacoes ("ClienteId");
END IF;
END $EF$;
DO $EF$
BEGIN
IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20240306101043_adiciona_transacoes') THEN
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20240306101043_adiciona_transacoes', '8.0.2');
END IF;
END $EF$;
COMMIT;