Skip to content

Commit 0d3e152

Browse files
committed
Add ON DELETE CASCADE for testing query
1 parent 9234c2c commit 0d3e152

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

backend/embed/migrations/mysql/20201013035318_initial_schema.sql

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ CREATE TABLE IF NOT EXISTS `user_has_capability`
3434
`user_id` INT NOT NULL,
3535
`capability_name` VARCHAR(50) NOT NULL,
3636
UNIQUE (`user_id`, `capability_name`),
37-
FOREIGN KEY (`capability_name`) REFERENCES `capability`(`name`)
37+
FOREIGN KEY (`capability_name`) REFERENCES `capability`(`name`) ON DELETE CASCADE
3838
);
3939

4040
CREATE TABLE IF NOT EXISTS `auth`
@@ -46,7 +46,7 @@ CREATE TABLE IF NOT EXISTS `auth`
4646
`user_id` INT NOT NULL,
4747
`type` VARCHAR(50) NOT NULL,
4848
`secret` VARCHAR(255) NOT NULL,
49-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
49+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
5050
UNIQUE (`user_id`, `type`)
5151
);
5252

@@ -73,7 +73,7 @@ CREATE TABLE IF NOT EXISTS `audit_log`
7373
`object_id` INT NOT NULL,
7474
`action` VARCHAR(50) NOT NULL,
7575
`meta` TEXT NOT NULL,
76-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`)
76+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
7777
);
7878

7979
CREATE TABLE IF NOT EXISTS `certificate_authority`
@@ -101,7 +101,7 @@ CREATE TABLE IF NOT EXISTS `dns_provider`
101101
`acmesh_name` VARCHAR(50) NOT NULL,
102102
`dns_sleep` INT NOT NULL DEFAULT 0,
103103
`meta` TEXT NOT NULL,
104-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`)
104+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
105105
);
106106

107107
CREATE TABLE IF NOT EXISTS certificate
@@ -121,9 +121,9 @@ CREATE TABLE IF NOT EXISTS certificate
121121
`error_message` TEXT NOT NULL,
122122
`meta` TEXT NOT NULL,
123123
`is_ecc` BOOLEAN NOT NULL DEFAULT FALSE,
124-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
125-
FOREIGN KEY (`certificate_authority_id`) REFERENCES `certificate_authority`(`id`),
126-
FOREIGN KEY (`dns_provider_id`) REFERENCES `dns_provider`(`id`)
124+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
125+
FOREIGN KEY (`certificate_authority_id`) REFERENCES `certificate_authority`(`id`) ON DELETE CASCADE,
126+
FOREIGN KEY (`dns_provider_id`) REFERENCES `dns_provider`(`id`) ON DELETE CASCADE
127127
);
128128

129129
CREATE TABLE IF NOT EXISTS `stream`
@@ -139,7 +139,7 @@ CREATE TABLE IF NOT EXISTS `stream`
139139
`udp_forwarding` INT NOT NULL DEFAULT 0,
140140
`advanced_config` TEXT NOT NULL,
141141
`is_disabled` BOOLEAN NOT NULL DEFAULT FALSE,
142-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`)
142+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
143143
);
144144

145145
CREATE TABLE IF NOT EXISTS `nginx_template`
@@ -173,8 +173,8 @@ CREATE TABLE IF NOT EXISTS `upstream`
173173
`advanced_config` TEXT NOT NULL,
174174
`status` VARCHAR(50) NOT NULL DEFAULT '',
175175
`error_message` TEXT NOT NULL,
176-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
177-
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template`(`id`)
176+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
177+
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template`(`id`) ON DELETE CASCADE
178178
);
179179

180180
CREATE TABLE IF NOT EXISTS `upstream_server`
@@ -190,7 +190,7 @@ CREATE TABLE IF NOT EXISTS `upstream_server`
190190
`max_fails` INT NOT NULL DEFAULT 0,
191191
`fail_timeout` INT NOT NULL DEFAULT 0,
192192
`is_backup` BOOLEAN NOT NULL DEFAULT FALSE,
193-
FOREIGN KEY (`upstream_id`) REFERENCES `upstream`(`id`)
193+
FOREIGN KEY (`upstream_id`) REFERENCES `upstream`(`id`) ON DELETE CASCADE
194194
);
195195

196196
CREATE TABLE IF NOT EXISTS `access_list`
@@ -202,7 +202,7 @@ CREATE TABLE IF NOT EXISTS `access_list`
202202
`user_id` INT NOT NULL,
203203
`name` VARCHAR(50) NOT NULL,
204204
`meta` TEXT NOT NULL,
205-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`)
205+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE
206206
);
207207

208208
CREATE TABLE IF NOT EXISTS host
@@ -234,11 +234,11 @@ CREATE TABLE IF NOT EXISTS host
234234
`status` VARCHAR(50) NOT NULL DEFAULT '',
235235
`error_message` TEXT NOT NULL,
236236
`is_disabled` BOOLEAN NOT NULL DEFAULT FALSE,
237-
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
238-
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template`(`id`),
239-
FOREIGN KEY (`upstream_id`) REFERENCES `upstream`(`id`),
240-
FOREIGN KEY (`certificate_id`) REFERENCES `certificate`(`id`),
241-
FOREIGN KEY (`access_list_id`) REFERENCES `access_list`(`id`)
237+
FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
238+
FOREIGN KEY (`nginx_template_id`) REFERENCES `nginx_template`(`id`) ON DELETE CASCADE,
239+
FOREIGN KEY (`upstream_id`) REFERENCES `upstream`(`id`) ON DELETE CASCADE,
240+
FOREIGN KEY (`certificate_id`) REFERENCES `certificate`(`id`) ON DELETE CASCADE,
241+
FOREIGN KEY (`access_list_id`) REFERENCES `access_list`(`id`) ON DELETE CASCADE
242242
);
243243

244244
-- migrate:down

backend/embed/migrations/postgres/20201013035318_initial_schema.sql

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ CREATE TABLE "capability" (
2828

2929
CREATE TABLE "user_has_capability" (
3030
"user_id" INTEGER NOT NULL,
31-
"capability_name" TEXT NOT NULL REFERENCES "capability"("name"),
31+
"capability_name" TEXT NOT NULL REFERENCES "capability"("name") ON DELETE CASCADE,
3232
UNIQUE ("user_id", "capability_name")
3333
);
3434

@@ -37,7 +37,7 @@ CREATE TABLE "auth" (
3737
"created_at" BIGINT NOT NULL DEFAULT 0,
3838
"updated_at" BIGINT NOT NULL DEFAULT 0,
3939
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
40-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
40+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
4141
"type" VARCHAR(50) NOT NULL,
4242
"secret" VARCHAR(255) NOT NULL,
4343
UNIQUE ("user_id", "type")
@@ -59,7 +59,7 @@ CREATE TABLE "audit_log" (
5959
"created_at" BIGINT NOT NULL DEFAULT 0,
6060
"updated_at" BIGINT NOT NULL DEFAULT 0,
6161
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
62-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
62+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
6363
"object_type" VARCHAR(50) NOT NULL,
6464
"object_id" INTEGER NOT NULL,
6565
"action" VARCHAR(50) NOT NULL,
@@ -84,7 +84,7 @@ CREATE TABLE "dns_provider" (
8484
"created_at" BIGINT NOT NULL DEFAULT 0,
8585
"updated_at" BIGINT NOT NULL DEFAULT 0,
8686
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
87-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
87+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
8888
"name" VARCHAR(50) NOT NULL,
8989
"acmesh_name" VARCHAR(50) NOT NULL,
9090
"dns_sleep" INTEGER NOT NULL DEFAULT 0,
@@ -96,10 +96,10 @@ CREATE TABLE "certificate" (
9696
"created_at" BIGINT NOT NULL DEFAULT 0,
9797
"updated_at" BIGINT NOT NULL DEFAULT 0,
9898
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
99-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
99+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
100100
"type" VARCHAR(50) NOT NULL, -- custom,dns,http
101-
"certificate_authority_id" INTEGER REFERENCES "certificate_authority"("id"), -- 0 for a custom cert
102-
"dns_provider_id" INTEGER REFERENCES "dns_provider"("id"), -- 0, for a http or custom cert
101+
"certificate_authority_id" INTEGER REFERENCES "certificate_authority"("id") ON DELETE CASCADE, -- 0 for a custom cert
102+
"dns_provider_id" INTEGER REFERENCES "dns_provider"("id") ON DELETE CASCADE, -- 0, for a http or custom cert
103103
"name" VARCHAR(50) NOT NULL,
104104
"domain_names" TEXT NOT NULL,
105105
"expires_on" INTEGER DEFAULT 0,
@@ -114,7 +114,7 @@ CREATE TABLE "stream" (
114114
"created_at" BIGINT NOT NULL DEFAULT 0,
115115
"updated_at" BIGINT NOT NULL DEFAULT 0,
116116
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
117-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
117+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
118118
"listen_interface" VARCHAR(50) NOT NULL,
119119
"incoming_port" INTEGER NOT NULL,
120120
"tcp_forwarding" INTEGER NOT NULL DEFAULT 0,
@@ -128,7 +128,7 @@ CREATE TABLE "nginx_template" (
128128
"created_at" BIGINT NOT NULL DEFAULT 0,
129129
"updated_at" BIGINT NOT NULL DEFAULT 0,
130130
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
131-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
131+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
132132
"name" VARCHAR(50) NOT NULL,
133133
"type" VARCHAR(50) NOT NULL,
134134
"template" TEXT NOT NULL
@@ -141,7 +141,7 @@ CREATE TABLE "upstream" (
141141
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
142142
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
143143
"name" VARCHAR(50) NOT NULL,
144-
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id"),
144+
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id") ON DELETE CASCADE,
145145
"ip_hash" BOOLEAN NOT NULL DEFAULT FALSE,
146146
"ntlm" BOOLEAN NOT NULL DEFAULT FALSE,
147147
"keepalive" INTEGER NOT NULL DEFAULT 0,
@@ -158,7 +158,7 @@ CREATE TABLE "upstream_server" (
158158
"created_at" BIGINT NOT NULL DEFAULT 0,
159159
"updated_at" BIGINT NOT NULL DEFAULT 0,
160160
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
161-
"upstream_id" INTEGER NOT NULL REFERENCES "upstream"("id"),
161+
"upstream_id" INTEGER NOT NULL REFERENCES "upstream"("id") ON DELETE CASCADE,
162162
"server" VARCHAR(50) NOT NULL,
163163
"weight" INTEGER NOT NULL DEFAULT 0,
164164
"max_conns" INTEGER NOT NULL DEFAULT 0,
@@ -172,7 +172,7 @@ CREATE TABLE "access_list" (
172172
"created_at" BIGINT NOT NULL DEFAULT 0,
173173
"updated_at" BIGINT NOT NULL DEFAULT 0,
174174
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
175-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
175+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
176176
"name" VARCHAR(50) NOT NULL,
177177
"meta" TEXT NOT NULL
178178
);
@@ -182,17 +182,17 @@ CREATE TABLE "host" (
182182
"created_at" BIGINT NOT NULL DEFAULT 0,
183183
"updated_at" BIGINT NOT NULL DEFAULT 0,
184184
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
185-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
185+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
186186
"type" TEXT NOT NULL,
187-
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id"),
187+
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id") ON DELETE CASCADE,
188188
"listen_interface" TEXT NOT NULL DEFAULT '',
189189
"domain_names" TEXT NOT NULL,
190-
"upstream_id" INTEGER NOT NULL DEFAULT 0 REFERENCES "upstream"("id"),
190+
"upstream_id" INTEGER NOT NULL DEFAULT 0 REFERENCES "upstream"("id") ON DELETE CASCADE,
191191
"proxy_scheme" TEXT NOT NULL DEFAULT '',
192192
"proxy_host" TEXT NOT NULL DEFAULT '',
193193
"proxy_port" INTEGER NOT NULL DEFAULT 0,
194-
"certificate_id" INTEGER NOT NULL DEFAULT 0 REFERENCES "certificate"("id"),
195-
"access_list_id" INTEGER NOT NULL DEFAULT 0 REFERENCES "access_list"("id"),
194+
"certificate_id" INTEGER NOT NULL DEFAULT 0 REFERENCES "certificate"("id") ON DELETE CASCADE,
195+
"access_list_id" INTEGER NOT NULL DEFAULT 0 REFERENCES "access_list"("id") ON DELETE CASCADE,
196196
"ssl_forced" BOOLEAN NOT NULL DEFAULT FALSE,
197197
"caching_enabled" BOOLEAN NOT NULL DEFAULT FALSE,
198198
"block_exploits" BOOLEAN NOT NULL DEFAULT FALSE,

0 commit comments

Comments
 (0)