-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatabaseCreation.sql
620 lines (558 loc) · 13.8 KB
/
DatabaseCreation.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* Auto generated database creation code */
CREATE DATABASE [TEXIT_ARCHENEMY]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'TEXIT_ARCHENEMY', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\TEXIT_ARCHENEMY.mdf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
LOG ON
( NAME = N'TEXIT_ARCHENEMY_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL15.SQLEXPRESS\MSSQL\DATA\TEXIT_ARCHENEMY_log.ldf' , SIZE = 8192KB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET COMPATIBILITY_LEVEL = 150
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET ANSI_NULLS OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET ANSI_PADDING OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET ARITHABORT OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET AUTO_CREATE_STATISTICS ON(INCREMENTAL = OFF)
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET DISABLE_BROKER
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET READ_WRITE
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET RECOVERY SIMPLE
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET MULTI_USER
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [TEXIT_ARCHENEMY] SET DELAYED_DURABILITY = DISABLED
GO
USE [TEXIT_ARCHENEMY]
GO
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = Off;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = Primary;
GO
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = On;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = Primary;
GO
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = Off;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = Primary;
GO
USE [TEXIT_ARCHENEMY]
GO
IF NOT EXISTS (SELECT name FROM sys.filegroups WHERE is_default=1 AND name = N'PRIMARY') ALTER DATABASE [TEXIT_ARCHENEMY] MODIFY FILEGROUP [PRIMARY] DEFAULT
GO
/* End of autogeneration */
USE TEXIT_ARCHENEMY
GO
/* Table for Discord credentials */
CREATE TABLE
discord_auth(
token VARCHAR(60) NOT NULL
)
GO
GO
/* Twitter credentials */
CREATE TABLE
twitter_auth(
api_key VARCHAR(25) NOT NULL,
api_secret VARCHAR(50) NOT NULL,
api_token VARCHAR(116) NOT NULL
)
GO
/* Master table for voice / text channel types. Could also be threads? Idk if those are text */
CREATE TABLE
channel_types (
channel_type_id INT IDENTITY(1,1) PRIMARY KEY,
channel_type_description VARCHAR(10) NOT NULL
)
GO
/* Insert the types */
INSERT INTO
channel_types (channel_type_description)
VALUES
('text'),
('voice')
GO
/* The idea here is that on being kicked out of a guild / channel, or it being made useless, we can delete all of the messages and rules stored to it if we just save the channel as foreign keys */
/* This table is subject to expansion as I add bot features and bind them to channels */
CREATE TABLE
discord_channels(
channel_id VARCHAR(20) PRIMARY KEY,
guild_id VARCHAR(20) NOT NULL,
channel_type_id INT NOT NULL DEFAULT 1,
repost_check BIT NOT NULL DEFAULT 0,
pixiv_expand BIT NOT NULL DEFAULT 0,
FOREIGN KEY (channel_type_id) REFERENCES channel_types(channel_type_id)
)
GO
/* Store all the rules so they can be sent to twitter and so that we have no repeats */
CREATE TABLE
twitter_rules(
tag INT PRIMARY KEY IDENTITY(1,1),
rule_value VARCHAR(512) UNIQUE NOT NULL
)
GO
/* Bind the rules to channels */
CREATE TABLE
rule_channel_relation(
tag INT NOT NULL,
channel_id VARCHAR(20) NOT NULL,
PRIMARY KEY (tag, channel_id),
FOREIGN KEY (tag) REFERENCES twitter_rules(tag) ON DELETE CASCADE,
FOREIGN KEY (channel_id) REFERENCES discord_channels(channel_id) ON DELETE CASCADE
)
GO
/* I'm really not sure if Snowflakes or IDs would ever repeat over different art services but creating a link type just to make extra sure we don't tag two ids from different services as the same post is nice safety */
CREATE TABLE
link_types (
link_type_id INT IDENTITY(1,1) PRIMARY KEY,
link_type_description VARCHAR(10) NOT NULL UNIQUE
)
GO
INSERT INTO
link_types
VALUES
('Twitter'),
('Pixiv'),
('Artstation')
GO
/* Store all the IDs from different links posted in any one channel alongside with the message they were posted in (so we can point it out) and the type of art site they were in (read above table desc)*/
CREATE TABLE
repost_repository(
message_id VARCHAR(20) NOT NULL,
channel_id VARCHAR(20) NOT NULL,
link_id VARCHAR(20) NOT NULL,
link_type_id INT NOT NULL,
PRIMARY KEY(message_id, channel_id, link_id, link_type_id),
FOREIGN KEY(link_type_id) REFERENCES link_types(link_type_id),
FOREIGN KEY(channel_id) REFERENCES discord_channels(channel_id) ON DELETE CASCADE
)
GO
CREATE TABLE
draw_a_box_warmups(
warmup VARCHAR(50) NOT NULL,
lesson int NOT NULL
)
GO
INSERT INTO
draw_a_box_warmups (warmup, lesson)
VALUES
('Superimposed Lines',1),
('Table of Ellipses',1),
('Ellipses in Planes', 1),
('Funnels', 1),
('Plotted Perspective', 1),
('Rough Perspective',1),
('Rotated Boxes', 1),
('Organic Perspective', 1)
GO
INSERT INTO
draw_a_box_warmups (warmup, lesson)
VALUES
('Organic Arrows', 2),
('Organic Forms with Contour Lines', 2),
('Texture Analysis', 2),
('Dissections',2),
('Form Intersections', 2),
('Organic Intersections', 2)
GO
CREATE TABLE
draw_a_box_box_challenge(
user_id VARCHAR(20) PRIMARY KEY,
boxes_drawn int NOT NULL
)
GO
/* This one seems really bad and like it could be abstracted to reutilize the query code, but this is my first time writing a stored procedure so I have no idea how haha yeah */
/* Anyway this checks if a link was already posted and returns a table with either the message id of the post where it was posted or just '-1' if it wasn't (so -1 is actually the good result I guess? */
CREATE PROCEDURE check_repost
@channel_id VARCHAR(20),
@message_id VARCHAR(20),
@link_id VARCHAR(20),
@link_type_description VARCHAR(20)
AS
BEGIN
DECLARE @count INT
SET @count = (
SELECT
COUNT(*)
FROM
repost_repository
INNER JOIN link_types
ON repost_repository.link_type_id = link_types.link_type_id
WHERE
channel_id = @channel_id AND
link_id = @link_id AND
link_type_description = @link_type_description
)
IF @count > 0
BEGIN
SELECT
channel_id,
message_id
FROM
repost_repository
INNER JOIN link_types
ON repost_repository.link_type_id = link_types.link_type_id
WHERE
channel_id = @channel_id AND
link_id = @link_id AND
link_type_description = @link_type_description
RETURN
END
ELSE
BEGIN
INSERT INTO
repost_repository (channel_id, message_id, link_id, link_type_id)
VALUES
(@channel_id, @message_id, @link_id, (SELECT link_type_id FROM link_types WHERE link_type_description = @link_type_description))
SELECT '-1' AS channel_id, '-1' AS message_id
RETURN
END
END
GO
CREATE PROCEDURE preemptive_repost_check
@channel_id VARCHAR(20),
@link_id VARCHAR(20),
@link_type_description VARCHAR(20)
AS
BEGIN
SELECT
COUNT(*) AS repost_number
FROM
repost_repository
INNER JOIN link_types
ON repost_repository.link_type_id = link_types.link_type_id
WHERE
channel_id = @channel_id AND
link_id = @link_id AND
link_type_description = @link_type_description
END
GO
/* Yeet the channels that just aren't used. Only called in triggers */
CREATE PROCEDURE remove_useless_channels
AS
BEGIN
DELETE FROM
discord_channels
WHERE
discord_channels.pixiv_expand = 0
AND discord_channels.repost_check = 0
AND discord_channels.channel_id IN (
SELECT
discord_channels.channel_id
FROM
discord_channels
LEFT JOIN rule_channel_relation
ON discord_channels.channel_id = rule_channel_relation.channel_id
WHERE rule_channel_relation.tag IS NULL
)
END
GO
/* Adding a rule is actually surprisingly complex since we want no duplicates and we need to check FK integrity */
CREATE PROCEDURE add_twitter_rule
@channel_id VARCHAR(20),
@guild_id VARCHAR(20),
@rule_value VARCHAR(512)
AS
BEGIN
DECLARE @actually_added AS BIT
SET @actually_added = 0
IF (SELECT COUNT(*) FROM discord_channels WHERE channel_id = @channel_id) = 0
BEGIN
INSERT INTO
discord_channels (channel_id, guild_id)
VALUES
(@channel_id, @guild_id)
END
IF (SELECT COUNT(*) FROM twitter_rules WHERE rule_value = @rule_value) = 0
BEGIN
INSERT INTO
twitter_rules
VALUES
(@rule_value)
SET @actually_added = 1
END
BEGIN
DECLARE @tag INT
SET @tag = (SELECT tag FROM twitter_rules WHERE rule_value = @rule_value)
BEGIN TRY
INSERT INTO
rule_channel_relation(tag, channel_id)
VALUES
(@tag, @channel_id)
SELECT @tag AS tag, @actually_added AS added
RETURN
END TRY
BEGIN CATCH
SELECT -1 AS tag, 0 AS added
RETURN(-1)
END CATCH
END
END
GO
/* Delete a rule association. We will let the trigger handle deleting the actual rule if needed */
CREATE PROCEDURE delete_twitter_rule
@tag INT,
@channel_id VARCHAR(20)
AS
BEGIN
DELETE FROM
rule_channel_relation
WHERE
tag = @tag AND
channel_id = @channel_id
END
GO
/* Get the rules */
CREATE PROCEDURE get_twitter_rules
AS
BEGIN
SELECT
*
FROM
twitter_rules
END
GO
CREATE PROCEDURE get_twitter_rule_channels
@tag INT
AS
BEGIN
SELECT
channel_id
FROM
rule_channel_relation
WHERE
tag = @tag
END
GO
/* Get twitter auth */
CREATE PROCEDURE get_twitter_creds
AS
BEGIN
SELECT
TOP 1 *
FROM
twitter_auth
END
GO
/* Get discord auth */
CREATE PROCEDURE get_discord_creds
AS
BEGIN
SELECT
TOP 1 *
FROM
discord_auth
END
GO
CREATE PROCEDURE get_box_warmup
@lesson int
AS
BEGIN
SELECT
warmup
FROM
draw_a_box_warmups
WHERE
lesson <= @lesson
END
GO
CREATE PROCEDURE update_box_challenge_progress
@user_id VARCHAR(20),
@boxes_drawn INT
AS
BEGIN
IF (SELECT COUNT(*) FROM draw_a_box_box_challenge WHERE user_id = @user_id) = 0
BEGIN
IF @boxes_drawn < 0
BEGIN
INSERT INTO
draw_a_box_box_challenge (user_id, boxes_drawn)
VALUES
(@user_id, 0)
END
ELSE
BEGIN
INSERT INTO
draw_a_box_box_challenge (user_id, boxes_drawn)
VALUES
(@user_id, @boxes_drawn)
END
END
ELSE
BEGIN
UPDATE
draw_a_box_box_challenge
SET
boxes_drawn = (SELECT MAX(boxes_drawn) FROM (VALUES(boxes_drawn + @boxes_drawn), (0)) AS boxes(boxes_drawn))
WHERE
user_id = @user_id
END
SELECT
boxes_drawn
FROM
draw_a_box_box_challenge
WHERE
user_id = @user_id
END
GO
CREATE PROCEDURE get_box_challenge_progress
@user_id VARCHAR(20)
AS
BEGIN
IF (SELECT COUNT(*) FROM draw_a_box_box_challenge WHERE user_id = @user_id) = 0
BEGIN
SELECT 0 AS boxes_drawn
RETURN
END
ELSE
BEGIN
SELECT
boxes_drawn
FROM
draw_a_box_box_challenge
WHERE
user_id = @user_id
END
END
GO
CREATE PROCEDURE is_repost_channel
@channel_id VARCHAR(20)
AS
BEGIN
IF (SELECT COUNT(*) FROM discord_channels WHERE channel_id = @channel_id) = 0
BEGIN
SELECT CONVERT(BIT, 0) AS repost_check
RETURN
END
ELSE
BEGIN
SELECT
repost_check
FROM
discord_channels
WHERE
channel_id = @channel_id
END
END
GO
CREATE PROCEDURE mark_as_repost_channel
@channel_id VARCHAR(20),
@guild_id VARCHAR(20)
AS
BEGIN
IF (SELECT COUNT(*) FROM discord_channels WHERE channel_id = @channel_id) = 0
BEGIN
INSERT INTO discord_channels (
channel_id,
channel_type_id,
guild_id,
pixiv_expand,
repost_check
)
VALUES
(@channel_id, 1, @guild_id, 0, 1)
END
ELSE
BEGIN
UPDATE
discord_channels
SET
repost_check = 1
WHERE
channel_id = @channel_id
END
END
GO
/* If we stop checking for reposts it makes no sense to keep the old ones */
CREATE TRIGGER repost_cleanup ON discord_channels
AFTER UPDATE AS
BEGIN
IF UPDATE(repost_check)
BEGIN
DELETE FROM
repost_repository
WHERE
repost_repository.channel_id IN (
SELECT
channel_id
FROM
discord_channels
WHERE repost_check = 0
)
END
END
GO
/* Delete channels made useless by the update */
CREATE TRIGGER channel_cleanup ON discord_channels
AFTER UPDATE AS
BEGIN
EXEC remove_useless_channels
END
GO
/* Since we have a very limited number of rules (30), we want to make sure we have no useless rules laying around, aka any rules not bound to any channels */
CREATE TRIGGER rule_cleanup ON rule_channel_relation
AFTER DELETE AS
BEGIN
DELETE FROM
twitter_rules
WHERE
twitter_rules.tag IN (
SELECT
twitter_rules.tag
FROM
twitter_rules
LEFT JOIN rule_channel_relation
ON twitter_rules.tag = rule_channel_relation.tag
WHERE rule_channel_relation.channel_id IS NULL
)
END
GO
/* I have no idea if I'm supposed to have all the DELETE triggers in one or have multiple and the internet is really not helping so I'm defaulting to SRP */
CREATE TRIGGER channel_cleanup_relation ON rule_channel_relation
AFTER DELETE AS
BEGIN
EXEC remove_useless_channels
END
GO