forked from ErikMcClure/sweetiebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy_migrate.sql
217 lines (176 loc) · 7.46 KB
/
legacy_migrate.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
DELIMITER //
CREATE TABLE IF NOT EXISTS `items` (
`ID` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`Content` VARCHAR(500) NOT NULL,
PRIMARY KEY (`ID`),
INDEX `CONTENT_INDEX` (`Content`(191))
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1
//
CREATE TABLE IF NOT EXISTS `tags` (
`ID` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(50) NOT NULL,
`Guild` BIGINT(20) UNSIGNED NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE INDEX `UNIQUE_NAME` (`Name`, `Guild`)
)
COLLATE='utf8mb4_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=1
//
CREATE TABLE IF NOT EXISTS `polloptions` (
`Poll` bigint(20) unsigned NOT NULL,
`Index` bigint(20) unsigned NOT NULL,
`Option` varchar(128) NOT NULL,
PRIMARY KEY (`Poll`,`Index`),
UNIQUE KEY `OPTION_INDEX` (`Option`,`Poll`),
KEY `POLL_INDEX` (`Poll`),
CONSTRAINT `FK_options_polls` FOREIGN KEY (`Poll`) REFERENCES `polls` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
//
CREATE TABLE IF NOT EXISTS `polls` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Guild` bigint(20) unsigned NOT NULL,
`Name` varchar(50) NOT NULL,
`Description` varchar(2048) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `Index 2` (`Name`,`Guild`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
//
CREATE TABLE IF NOT EXISTS `tags` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(50) NOT NULL,
`Guild` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `UNIQUE_NAME` (`Name`,`Guild`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
//
ALTER TABLE `members`
DROP COLUMN IF EXISTS `LastNickChange`//
IF NOT EXISTS (SELECT * FROM `information_schema`.`COLUMNS`
WHERE `TABLE_NAME` = "members" AND `COLUMN_NAME` = "FirstMessage")
THEN
ALTER TABLE `members` ADD `FirstMessage` datetime DEFAULT NULL;
END IF//
ALTER TABLE `users`
DROP COLUMN IF EXISTS `Email`,
DROP COLUMN IF EXISTS `Timezone`,
DROP COLUMN IF EXISTS `Verified`//
IF NOT EXISTS (SELECT * FROM `information_schema`.`COLUMNS`
WHERE `TABLE_NAME` = "users" AND `COLUMN_NAME` = "Discriminator")
THEN
ALTER TABLE `users` ADD `Discriminator` int(10) unsigned NOT NULL DEFAULT '0';
END IF//
DROP PROCEDURE IF EXISTS `SawUser`//
DROP TRIGGER IF EXISTS `members_before_update`//
DROP PROCEDURE IF EXISTS `AddChat`//
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddChat`(IN `_id` BIGINT, IN `_author` BIGINT, IN `_message` VARCHAR(2000), IN `_channel` BIGINT, IN `_everyone` BIT, IN `_guild` BIGINT)
MODIFIES SQL DATA
BEGIN
INSERT INTO users (ID, Username, Avatar, LastSeen, LastNameChange)
VALUES (_author, '', '', UTC_TIMESTAMP(), UTC_TIMESTAMP())
ON DUPLICATE KEY UPDATE LastSeen=UTC_TIMESTAMP();
INSERT INTO chatlog (ID, Author, Message, Timestamp, Channel, Everyone, Guild)
VALUES (_id, _author, _message, UTC_TIMESTAMP(), _channel, _everyone, _guild)
ON DUPLICATE KEY UPDATE /* This prevents a race condition from causing a serious error */
Message = _message COLLATE 'utf8mb4_general_ci', Timestamp = UTC_TIMESTAMP(), Everyone=_everyone;
END//
DROP FUNCTION IF EXISTS `AddItem`//
CREATE DEFINER=`root`@`localhost` FUNCTION `AddItem`(`_content` VARCHAR(500)) RETURNS bigint(20)
MODIFIES SQL DATA
BEGIN
SET @id = (SELECT ID FROM items WHERE Content = _content);
IF @id IS NULL THEN
INSERT INTO items (Content) VALUES (_content);
RETURN LAST_INSERT_ID();
END IF;
RETURN @id;
END//
DROP FUNCTION IF EXISTS `AddMarkov`//
CREATE DEFINER=`root`@`localhost` FUNCTION `AddMarkov`(`_prev` BIGINT, `_prev2` BIGINT, `_speaker` VARCHAR(64), `_phrase` VARCHAR(64)) RETURNS bigint(20)
MODIFIES SQL DATA
DETERMINISTIC
BEGIN
INSERT INTO markov_transcripts_speaker (Speaker)
VALUES (_speaker)
ON DUPLICATE KEY UPDATE ID = ID;
SET @speakerid = (SELECT ID FROM markov_transcripts_speaker WHERE Speaker = _speaker);
INSERT INTO markov_transcripts (SpeakerID, Phrase)
VALUES (@speakerid, _phrase)
ON DUPLICATE KEY UPDATE Phrase = _phrase;
/*LAST_UPDATE_ID() doesn't work here because of the duplicate key possibility */
SET @ret = (SELECT ID FROM markov_transcripts WHERE SpeakerID = @speakerid AND Phrase = _phrase);
INSERT INTO markov_transcripts_map (Prev, Prev2, Next)
VALUES (_prev, _prev2, @ret)
ON DUPLICATE KEY UPDATE Count = Count + 1;
RETURN @ret;
END//
DROP PROCEDURE IF EXISTS `AddMember`//
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddMember`(IN `_id` BIGINT, IN `_guild` BIGINT, IN `_firstseen` DATETIME, IN `_nickname` VARCHAR(128))
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT ''
INSERT INTO members (ID, Guild, FirstSeen, Nickname)
VALUES (_id, _guild, _firstseen, _nickname)
ON DUPLICATE KEY UPDATE
FirstSeen=GetMinDate(_firstseen,FirstSeen), Nickname=_nickname//
DROP PROCEDURE IF EXISTS `AddUser`//
CREATE DEFINER=`root`@`localhost` PROCEDURE `AddUser`(IN `_id` BIGINT, IN `_username` VARCHAR(512), IN `_discriminator` INT, IN `_avatar` VARCHAR(512), IN `_isonline` BIT)
LANGUAGE SQL
NOT DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY DEFINER
COMMENT ''
INSERT INTO users (ID, Username, Discriminator, Avatar, LastSeen, LastNameChange)
VALUES (_id, _username, _discriminator, _avatar, UTC_TIMESTAMP(), UTC_TIMESTAMP())
ON DUPLICATE KEY UPDATE
Username=_username, Discriminator=_discriminator, Avatar=_avatar, LastSeen=IF(_isonline > 0, UTC_TIMESTAMP(), LastSeen)//
DROP TRIGGER IF EXISTS `chatlog_before_delete`//
CREATE TRIGGER `chatlog_before_delete` BEFORE DELETE ON `chatlog` FOR EACH ROW DELETE FROM editlog WHERE ID = OLD.ID//
DROP TRIGGER IF EXISTS `chatlog_before_update`//
CREATE TRIGGER `chatlog_before_update` BEFORE UPDATE ON `chatlog` FOR EACH ROW INSERT INTO editlog (ID, Author, Message, Timestamp, Channel, Everyone, Guild)
VALUES (OLD.ID, OLD.Author, OLD.Message, OLD.Timestamp, OLD.Channel, OLD.Everyone, OLD.Guild)
ON DUPLICATE KEY UPDATE ID = OLD.ID//
CREATE TABLE IF NOT EXISTS `itemtags` (
`Item` bigint(20) unsigned NOT NULL,
`Tag` bigint(20) unsigned NOT NULL,
PRIMARY KEY (`Item`,`Tag`),
KEY `FK_itemtags_tags` (`Tag`),
CONSTRAINT `FK_itemtags_items` FOREIGN KEY (`Item`) REFERENCES `items` (`ID`),
CONSTRAINT `FK_itemtags_tags` FOREIGN KEY (`Tag`) REFERENCES `tags` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4//
DROP TRIGGER IF EXISTS `itemtags_after_delete`//
CREATE TRIGGER `itemtags_after_delete` AFTER DELETE ON `itemtags` FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM itemtags WHERE Item = OLD.Item) = 0 THEN
DELETE FROM items WHERE ID = OLD.Item;
END IF;
END//
DROP TRIGGER IF EXISTS `polloptions_before_delete`//
CREATE TRIGGER `polloptions_before_delete` BEFORE DELETE ON `polloptions` FOR EACH ROW DELETE FROM votes WHERE Poll = OLD.Poll AND `Option` = OLD.`Index`//
DROP TRIGGER IF EXISTS `polls_before_delete`//
CREATE TRIGGER `polls_before_delete` BEFORE DELETE ON `polls` FOR EACH ROW DELETE FROM polloptions WHERE Poll = OLD.ID//
DROP TRIGGER IF EXISTS `tags_before_delete`//
CREATE TRIGGER `tags_before_delete` BEFORE DELETE ON `tags` FOR EACH ROW DELETE FROM itemtags WHERE Tag = OLD.ID//
DROP TRIGGER IF EXISTS `users_before_update`//
CREATE TRIGGER `users_before_update` BEFORE UPDATE ON `users` FOR EACH ROW BEGIN
IF NEW.Username = '' THEN
SET NEW.Username = OLD.Username;
END IF;
IF NEW.Discriminator = 0 THEN
SET NEW.Discriminator = OLD.Discriminator;
END IF;
IF NEW.Avatar = '' THEN
SET NEW.Avatar = OLD.Avatar;
END IF;
IF NEW.Username != OLD.Username THEN
SET NEW.LastNameChange = UTC_TIMESTAMP();
SET @diff = UNIX_TIMESTAMP(NEW.LastNameChange) - UNIX_TIMESTAMP(OLD.LastNameChange);
INSERT INTO aliases (User, Alias, Duration)
VALUES (OLD.ID, OLD.Username, @diff)
ON DUPLICATE KEY UPDATE Duration = Duration + @diff;
END IF;
END//