forked from stadust/pointercrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
20221009140916_thumbnails.up.sql
100 lines (83 loc) · 3.38 KB
/
20221009140916_thumbnails.up.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
-- Your SQL goes here
ALTER TABLE demons ADD COLUMN thumbnail TEXT NOT NULL DEFAULT 'https://i.ytimg.com/vi/zebrafishes/mqdefault.jpg';
UPDATE demons
SET thumbnail = 'https://i.ytimg.com/vi/' || SUBSTRING(video FROM '%v=#"___________#"%' FOR '#') || '/mqdefault.jpg'
WHERE video IS NOT NULL
AND NOT EXISTS (SELECT 1 FROM players WHERE players.id=demons.verifier AND players.link_banned);
CREATE OR REPLACE FUNCTION set_initial_thumbnail() RETURNS trigger AS '
BEGIN
IF NEW.video IS NOT NULL AND NOT EXISTS(SELECT 1 FROM players WHERE players.id=NEW.verifier AND players.link_banned) THEN
NEW.thumbnail := ''https://i.ytimg.com/vi/'' || SUBSTRING(NEW.video FROM ''%v=#"___________#"%'' FOR ''#'') || ''/mqdefault.jpg'';
END IF;
RETURN NEW;
END;
' LANGUAGE plpgsql;
CREATE TRIGGER demons_insert_set_thumbnail BEFORE INSERT ON demons FOR
EACH ROW EXECUTE PROCEDURE set_initial_thumbnail();
-- Your SQL goes here
ALTER TABLE demon_modifications ADD COLUMN thumbnail TEXT NULL DEFAULT NULL;
CREATE OR REPLACE FUNCTION audit_demon_modification() RETURNS trigger AS $demon_modification_trigger$
DECLARE
name_change CITEXT;
position_change SMALLINT;
requirement_change SMALLINT;
video_change VARCHAR(200);
thumbnail_change TEXT;
verifier_change INT;
publisher_change INT;
BEGIN
IF (OLD.name <> NEW.name) THEN
name_change = OLD.name;
END IF;
IF (OLD.position <> NEW.position) THEN
position_change = OLD.position;
END IF;
IF (OLD.requirement <> NEW.requirement) THEN
requirement_change = OLD.requirement;
END IF;
IF (OLD.video <> NEW.video) THEN
video_change = OLD.video;
END IF;
IF (OLD.thumbnail <> NEW.thumbnail) THEN
thumbnail_change = OLD.thumbnail;
END IF;
IF (OLD.verifier <> NEW.verifier) THEN
verifier_change = OLD.verifier;
END IF;
IF (OLD.publisher <> NEW.publisher) THEN
publisher_change = OLD.publisher;
END IF;
INSERT INTO demon_modifications (userid, name, position, requirement, video, verifier, publisher, thumbnail, id)
(SELECT id, name_change, position_change, requirement_change, video_change, verifier_change, publisher_change, thumbnail_change, NEW.id
FROM active_user LIMIT 1);
RETURN NEW;
END;
$demon_modification_trigger$ LANGUAGE plpgsql;
DROP FUNCTION list_at(TIMESTAMP WITHOUT TIME ZONE);
CREATE FUNCTION list_at(TIMESTAMP WITHOUT TIME ZONE)
RETURNS TABLE (
name CITEXT,
position_ SMALLINT,
requirement SMALLINT,
video VARCHAR(200),
thumbnail TEXT,
verifier INTEGER,
publisher INTEGER,
id INTEGER,
level_id BIGINT,
current_position SMALLINT
)
AS $$
SELECT name, CASE WHEN t.position IS NULL THEN demons.position ELSE t.position END, requirement, video, thumbnail, verifier, publisher, demons.id, level_id, demons.position AS current_position
FROM demons
LEFT OUTER JOIN (
SELECT DISTINCT ON (id) id, position
FROM demon_modifications
WHERE time >= $1 AND position != -1
ORDER BY id, time
) t
ON demons.id = t.id
WHERE NOT EXISTS (SELECT 1 FROM demon_additions WHERE demon_additions.id = demons.id AND time >= $1)
$$
LANGUAGE SQL
STABLE;