-
Notifications
You must be signed in to change notification settings - Fork 20
/
database.sql
executable file
·973 lines (816 loc) · 24.7 KB
/
database.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Mar 07, 2019 at 01:09 AM
-- Server version: 10.0.17-MariaDB-1~wheezy-log
-- PHP Version: 7.0.33-1~dotdeb+8.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
--
-- Database: `anime_bracket`
--
CREATE DATABASE IF NOT EXISTS `anime_bracket` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `anime_bracket`;
DELIMITER $$
--
-- Procedures
--
DROP PROCEDURE IF EXISTS `proc_AddUserToBracket`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_AddUserToBracket` (`bracketId` INT, `userId` INT) BEGIN
INSERT INTO
`bracket_owners`
VALUES (
bracketId,
userId
);
END$$
DROP PROCEDURE IF EXISTS `proc_AdvanceEliminationRound`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_AdvanceEliminationRound` (`bracketId` INT) BEGIN
DECLARE currentGroup INT;
SELECT
MIN(`round_group`) INTO currentGroup
FROM
`round`
WHERE
`bracket_id` = bracketId AND
`round_final` != 1 AND
`round_deleted` = 0;
UPDATE
`round`
SET
`round_final` = 1,
`round_end_date` = UNIX_TIMESTAMP(NOW())
WHERE
`bracket_id` = bracketId AND
`round_tier` = 0 AND
`round_group` = currentGroup;
END$$
DROP PROCEDURE IF EXISTS `proc_CleanBrackets`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_CleanBrackets` () BEGIN
/* Brackets in nomination phase will be reverted to "not started" after 30 days */
UPDATE
`bracket`
SET
`bracket_state` = 0
WHERE
`bracket_state` = 1
AND `bracket_start` < (UNIX_TIMESTAMP() - 86400 * 30);
END$$
DROP PROCEDURE IF EXISTS `proc_GetBracketActiveGroupTier`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetBracketActiveGroupTier` (`bracketId` INT) BEGIN
SELECT
`round_tier`,
`round_group`
FROM
`round`
WHERE
`bracket_id` = bracketId AND
`round_deleted` = 0 AND
`round_final` = 0
ORDER BY
`round_tier` ASC,
`round_group` ASC;
END$$
DROP PROCEDURE IF EXISTS `proc_GetBracketFinalScore`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetBracketFinalScore` (`bracketId` INT) BEGIN
SELECT
COUNT(1) / (
SELECT
COUNT(1)
FROM
`round`
WHERE
`bracket_id` = bracketId
AND `round_tier` > 0
AND `round_deleted` = 0
) AS total
FROM
`votes`
WHERE
`bracket_id` = bracketId
AND `round_id` NOT IN (
SELECT
`round_id`
FROM
`round`
WHERE
`bracket_id` = bracketId
AND `round_tier` = 0
AND `round_deleted` = 0
);
END$$
DROP PROCEDURE IF EXISTS `proc_GetBracketRoundInfo`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetBracketRoundInfo` (`bracketId` INT) BEGIN
SELECT
SUM(
CASE WHEN `round_tier` = 1 THEN 1 ELSE 0 END
) AS total,
MAX(`round_tier`) AS max_tier,
MAX(`round_group`) AS max_group
FROM
`round`
WHERE
`bracket_id` = bracketId AND
`round_tier` > 0 AND
`round_deleted` = 0;
END$$
DROP PROCEDURE IF EXISTS `proc_GetBracketRounds`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetBracketRounds` (`bracketId` INT, `tierNum` INT, `groupNum` INT, `userId` INT) BEGIN
SELECT
*,
(
SELECT
`character_id`
FROM
`votes`
WHERE
`user_id` = userId AND
`round_id` = r.`round_id`
) AS user_vote
FROM
`round` r
WHERE
r.`round_deleted` = 0 AND
r.`bracket_id` = bracketId AND
(
tierNum IS NULL OR
r.`round_tier` = tierNum
) AND
(
groupNum IS NULL OR
r.`round_group` = groupNum
)
ORDER BY
r.`round_order`;
END$$
DROP PROCEDURE IF EXISTS `proc_GetBracketUsers`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetBracketUsers` (`bracketId` INT) BEGIN
SELECT
u.*
FROM
`bracket_owners` bo
INNER JOIN
`users` u ON u.`user_id` = bo.`user_id`
WHERE
bo.`bracket_id` = bracketId;
END$$
DROP PROCEDURE IF EXISTS `proc_GetBracketVotesForUser`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetBracketVotesForUser` (`bracketId` INT, `userId` INT) BEGIN
SELECT
`round_id`,
`character_id`
FROM
`votes`
WHERE
`user_id` = userId
AND `bracket_id` = bracketId;
END$$
DROP PROCEDURE IF EXISTS `proc_GetBracketVotingStats`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetBracketVotingStats` (`bracketId` INT) BEGIN
SELECT
COUNT(1) AS total,
COUNT(DISTINCT v.user_id) AS user_total,
r.`round_tier`,
r.`round_group`
FROM
`votes` v
INNER JOIN
`round` r
ON
r.`round_id` = v.`round_id`
WHERE
v.`bracket_id` = bracketId
GROUP BY
r.`round_tier`,
r.`round_group`;
END$$
DROP PROCEDURE IF EXISTS `proc_GetCharacterVotesForRound`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetCharacterVotesForRound` (`roundId` INT) BEGIN
SELECT
COUNT(1) AS total,
`character_id`
FROM
`votes`
WHERE
`round_id` = roundId
GROUP BY
`character_id`;
END$$
DROP PROCEDURE IF EXISTS `proc_GetEliminationGroupsCharacterVotes`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetEliminationGroupsCharacterVotes` (`bracketId` INT, `maxDate` INT) BEGIN
SELECT
COUNT(1) AS total,
r.`round_group`,
c.*
FROM
`round` r
INNER JOIN
`character` c ON c.`character_id` = r.`round_character1_id`
LEFT OUTER JOIN
`votes` v ON v.`character_id` = r.`round_character1_id`
WHERE
v.`bracket_id` = bracketId
AND v.`vote_date` <= maxDate
AND r.`round_tier` = 0
AND r.`round_deleted` = 0
GROUP BY
c.`character_id`;
END$$
DROP PROCEDURE IF EXISTS `proc_GetEliminationVotesForGroups`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetEliminationVotesForGroups` (`bracketId` INT, `maxDate` INT) BEGIN
SELECT
COUNT(1) AS total,
r.`round_group`
FROM
`votes` v
INNER JOIN
`round` r ON r.`round_id` = v.`round_id`
WHERE
v.`bracket_id` = bracketId
AND v.`vote_date` <= maxDate
AND r.`round_tier` = 0
AND r.`round_deleted` = 0
GROUP BY
r.`round_group`;
END$$
DROP PROCEDURE IF EXISTS `proc_GetUserBrackets`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetUserBrackets` (`source` INT, `userId` INT) BEGIN
SELECT
*
FROM
`bracket`
WHERE
`bracket_source` = source AND
`bracket_id` IN (
SELECT
`bracket_id`
FROM
`bracket_owners`
WHERE
`user_id` = userId
);
END$$
DROP PROCEDURE IF EXISTS `proc_RemoveUserFromBracket`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_RemoveUserFromBracket` (`bracketId` INT, `userId` INT) BEGIN
DELETE FROM
`bracket_owners`
WHERE
`bracket_id` = bracketId
AND `user_id` = userId;
END$$
DROP PROCEDURE IF EXISTS `proc_GetNextRounds`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetNextRounds` (`bracketId` INT) BEGIN
DECLARE currentTier INT;
DECLARE currentGroup INT;
DECLARE lastRoundInCurrentGroup INT;
SELECT
`round_tier`, `round_group` INTO currentTier, currentGroup
FROM
`round`
WHERE
`bracket_id` = bracketId
AND `round_final` = 0
AND `round_deleted` = 0
ORDER BY
`round_tier` ASC,
`round_group` ASC;
SELECT
MAX(round_id) INTO lastRoundInCurrentGroup
FROM
`round`
WHERE
`bracket_id` = bracketId
AND `round_tier` = currentTier
AND `round_group` = currentGroup
AND `round_final` = 0
AND `round_deleted` = 0;
SELECT
MIN(`round_tier`) AS nextTier,
(MIN(`round_tier`) * MAX(`round_group`) + MIN(`round_group`)) AS calculatedGroup,
`round_group` AS nextGroup
FROM
`round`
WHERE
`bracket_id` = bracketId
AND `round_final` = 0
AND `round_id` > lastRoundInCurrentGroup
AND `round_deleted` = 0
GROUP BY
`round_group`,
`round_tier`
ORDER BY
nextTier ASC,
calculatedGroup ASC
LIMIT 1;
END$$
DROP PROCEDURE IF EXISTS `proc_GetRoundWinner`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_GetRoundWinner` (`roundId` INT) BEGIN
DECLARE character1Id INT;
DECLARE character2Id INT;
DECLARE character1Votes INT;
DECLARE character2Votes INT;
DECLARE character1Seed INT;
DECLARE character2Seed INT;
SELECT
`round_character1_id`, `round_character2_id` INTO character1Id, character2Id
FROM
`round`
WHERE
`round_id` = roundId;
SELECT
COUNT(1) INTO character1Votes
FROM
`votes`
WHERE
`round_id` = roundId
AND `character_id` = character1Id;
SELECT
COUNT(1) INTO character2Votes
FROM
`votes`
WHERE
`round_id` = roundId
AND `character_id` = character2Id;
CASE
WHEN character1Votes > character2Votes THEN SELECT character1Id AS character_id;
WHEN character2Votes > character1Votes THEN SELECT character2Id AS character_id;
ELSE
BEGIN
SELECT
`character_seed` INTO character1Seed
FROM
`character`
WHERE
`character_id` = character1Id;
SELECT
`character_seed` INTO character2Seed
FROM
`character`
WHERE
`character_id` = character2Id;
IF character1Seed < character2Seed THEN
SELECT character1Id AS character_id;
ELSE
SELECT character2Id AS character_id;
END IF;
END;
END CASE;
END$$
DROP PROCEDURE IF EXISTS `proc_RollbackBracket`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_RollbackBracket` (`bracketId` INT, `roundTier` INT, `roundGroup` INT) BEGIN
/* Delete rounds in the next tier after (and including) the group to roll back to and all tiers after that */
UPDATE
`round`
SET
`round_deleted` = 1
WHERE
`bracket_id` = bracketId
AND (
(
`round_tier` = roundTier + 1
AND `round_group` >= roundGroup
)
OR `round_tier` > roundTier + 1
);
/* Set the new tier/group and all groups after as non-finalized and clear any calculated vote counts */
UPDATE
`round`
SET
`round_final` = 0,
`round_character1_votes` = NULL,
`round_character2_votes` = NULL,
`round_end_date` = NULL
WHERE
`bracket_id` = bracketId
AND `round_deleted` = 0
AND (
(
`round_tier` = roundTier
AND `round_group` >= roundGroup
)
OR `round_tier` > roundTier
);
/* Finally, delete all votes from the non-finalized rounds */
DELETE FROM
`votes`
WHERE
`round_id` IN (
SELECT
`round_id`
FROM
`round`
WHERE
`bracket_id` = bracketId
AND `round_final` = 0
AND `round_deleted` = 0
);
/* If rolling back to an eliminations round, kill any actual bracket setup */
IF roundTier = 0 THEN
BEGIN
UPDATE
`round`
SET
`round_deleted` = 1
WHERE
`bracket_id` = bracketId
AND `round_tier` > 0;
END;
END IF;
END$$
DROP PROCEDURE IF EXISTS `proc_UpdateBracketScores`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_UpdateBracketScores` () BEGIN
DECLARE bracketId INT;
DECLARE score DECIMAL;
DECLARE cursorDone BOOLEAN DEFAULT FALSE;
DECLARE query CURSOR FOR
SELECT
v.`bracket_id`,
(LOG10(COUNT(1)) + (AVG(v.`vote_date`) - (UNIX_TIMESTAMP() - 2592000)) / 45000) * b.bracket_state AS score
FROM
`votes` v
INNER JOIN
`bracket` b ON b.`bracket_id` = v.`bracket_id`
WHERE
v.`vote_date` >= (UNIX_TIMESTAMP() - 2592000)
AND b.`bracket_state` IN (2, 3)
GROUP BY
v.`bracket_id`
UNION
SELECT
n.`bracket_id`,
LOG10(COUNT(1)) + (b.bracket_start - (UNIX_TIMESTAMP() - 2592000)) / 45000 AS score
FROM
`nominee` n
INNER JOIN
`bracket` b ON b.`bracket_id` = n.`bracket_id`
WHERE
b.`bracket_start` >= (UNIX_TIMESTAMP() - 2592000)
AND b.`bracket_state` = 1
GROUP BY
n.`bracket_id`;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET cursorDone = TRUE;
OPEN query;
WHILE cursorDone = FALSE DO
FETCH query INTO bracketId, score;
UPDATE
`bracket`
SET
`bracket_score` = score
WHERE
`bracket_id` = bracketId;
END WHILE;
END$$
DELIMITER ;
-- --------------------------------------------------------
--
-- Table structure for table `bot_users`
--
DROP TABLE IF EXISTS `bot_users`;
CREATE TABLE `bot_users` (
`bot_id` int(10) NOT NULL,
`bot_name` varchar(50) NOT NULL,
`bot_password` varchar(50) NOT NULL,
`bot_hash` varchar(255) DEFAULT NULL,
`bot_cookie` varchar(255) DEFAULT NULL,
`bot_data` text,
`bot_callback` varchar(30) NOT NULL,
`bot_updated` int(10) NOT NULL,
`bot_created` int(10) NOT NULL,
`bot_enabled` tinyint(4) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table `bracket`
--
DROP TABLE IF EXISTS `bracket`;
CREATE TABLE `bracket` (
`bracket_id` int(11) NOT NULL,
`bracket_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`bracket_perma` varchar(150) COLLATE utf8_unicode_ci NOT NULL,
`bracket_start` int(11) NOT NULL,
`bracket_state` int(11) NOT NULL,
`bracket_pic` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`winner_character_id` int(11) DEFAULT NULL,
`bracket_rules` text COLLATE utf8_unicode_ci NOT NULL,
`bracket_source` int(11) NOT NULL,
`bracket_advance_hour` int(11) DEFAULT NULL,
`bracket_name_label` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
`bracket_source_label` varchar(150) COLLATE utf8_unicode_ci DEFAULT NULL,
`bracket_score` decimal(10,0) DEFAULT NULL,
`bracket_external_id` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`bracket_min_age` int(11) DEFAULT '2592000',
`bracket_hidden` tinyint(4) DEFAULT '1',
`bracket_blurb` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`bracket_captcha` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `bracket_owners`
--
DROP TABLE IF EXISTS `bracket_owners`;
CREATE TABLE `bracket_owners` (
`bracket_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `categories`
--
DROP TABLE IF EXISTS `categories`;
CREATE TABLE `categories` (
`category_id` int(11) NOT NULL,
`category_name` varchar(25) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `character`
--
DROP TABLE IF EXISTS `character`;
CREATE TABLE `character` (
`character_id` int(11) NOT NULL,
`bracket_id` int(11) NOT NULL,
`character_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`character_source` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`character_seed` int(11) DEFAULT NULL,
`character_meta` TEXT COLLATE utf8_unicode_ci NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `logins`
--
DROP TABLE IF EXISTS `logins`;
CREATE TABLE `logins` (
`user_id` int(11) NOT NULL,
`login_date` int(11) NOT NULL,
`login_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `mal_items`
--
DROP TABLE IF EXISTS `mal_items`;
CREATE TABLE `mal_items` (
`item_id` int(11) NOT NULL,
`item_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`item_pic` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`item_perma` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`item_type` varchar(10) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `mal_xref`
--
DROP TABLE IF EXISTS `mal_xref`;
CREATE TABLE `mal_xref` (
`mal_parent` int(11) NOT NULL,
`mal_child` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `nominee`
--
DROP TABLE IF EXISTS `nominee`;
CREATE TABLE `nominee` (
`nominee_id` int(11) NOT NULL,
`bracket_id` int(11) NOT NULL,
`nominee_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`nominee_source` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`nominee_created` int(11) NOT NULL,
`nominee_processed` bit(1) DEFAULT NULL,
`nominee_image` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `round`
--
DROP TABLE IF EXISTS `round`;
CREATE TABLE `round` (
`round_id` int(11) NOT NULL,
`bracket_id` int(11) NOT NULL,
`round_tier` int(11) NOT NULL,
`round_order` int(11) NOT NULL,
`round_group` int(11) NOT NULL,
`round_character1_id` int(11) NOT NULL,
`round_character2_id` int(11) NOT NULL,
`round_final` tinyint(1) NOT NULL,
`round_character1_votes` int(11) DEFAULT NULL,
`round_character2_votes` int(11) DEFAULT NULL,
`round_end_date` int(11) DEFAULT NULL,
`round_deleted` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `typeahead`
--
DROP TABLE IF EXISTS `typeahead`;
CREATE TABLE `typeahead` (
`typeahead_id` bigint(20) NOT NULL,
`typeahead_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`typeahead_category` int(11) NOT NULL,
`typeahead_image` varchar(255) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`user_id` int(11) NOT NULL,
`user_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`user_admin` tinyint(1) NOT NULL,
`user_ip` varchar(39) COLLATE utf8_unicode_ci NOT NULL,
`user_age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
-- --------------------------------------------------------
--
-- Table structure for table `votes`
--
DROP TABLE IF EXISTS `votes`;
CREATE TABLE `votes` (
`vote_date` int(11) NOT NULL,
`round_id` int(11) NOT NULL,
`character_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`bracket_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `bot_users`
--
ALTER TABLE `bot_users`
ADD PRIMARY KEY (`bot_id`),
ADD UNIQUE KEY `bot_name` (`bot_name`);
--
-- Indexes for table `bracket`
--
ALTER TABLE `bracket`
ADD PRIMARY KEY (`bracket_id`),
ADD UNIQUE KEY `U_bracket_perma` (`bracket_perma`),
ADD KEY `FK_winner_character_id` (`winner_character_id`);
--
-- Indexes for table `bracket_owners`
--
ALTER TABLE `bracket_owners`
ADD KEY `FK_bracket_owners_user_id` (`user_id`),
ADD KEY `FK_bracket_owners_bracket_id` (`bracket_id`);
--
-- Indexes for table `categories`
--
ALTER TABLE `categories`
ADD PRIMARY KEY (`category_id`);
--
-- Indexes for table `character`
--
ALTER TABLE `character`
ADD PRIMARY KEY (`character_id`),
ADD KEY `FK_character_bracket_id` (`bracket_id`);
--
-- Indexes for table `logins`
--
ALTER TABLE `logins`
ADD KEY `user_id` (`user_id`,`login_ip`);
--
-- Indexes for table `mal_items`
--
ALTER TABLE `mal_items`
ADD PRIMARY KEY (`item_id`),
ADD UNIQUE KEY `item_perma` (`item_perma`);
--
-- Indexes for table `mal_xref`
--
ALTER TABLE `mal_xref`
ADD KEY `FK_mal_xref_mal_parent` (`mal_parent`),
ADD KEY `FK_mal_xref_mal_child` (`mal_child`);
--
-- Indexes for table `nominee`
--
ALTER TABLE `nominee`
ADD PRIMARY KEY (`nominee_id`),
ADD KEY `FK_nominee_bracket_id` (`bracket_id`);
--
-- Indexes for table `round`
--
ALTER TABLE `round`
ADD PRIMARY KEY (`round_id`),
ADD KEY `FK_round_bracket_id` (`bracket_id`),
ADD KEY `FK_round_character1_id` (`round_character1_id`),
ADD KEY `FK_round_character2_id` (`round_character2_id`);
--
-- Indexes for table `typeahead`
--
ALTER TABLE `typeahead`
ADD PRIMARY KEY (`typeahead_id`);
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`);
--
-- Indexes for table `votes`
--
ALTER TABLE `votes`
ADD UNIQUE KEY `uc_user_id_round_id` (`user_id`,`round_id`),
ADD KEY `FK_vote_round_id` (`round_id`),
ADD KEY `FK_vote_character_id` (`character_id`),
ADD KEY `FK_votes_bracket_id` (`bracket_id`),
ADD KEY `FK_votes_user_id` (`user_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `bot_users`
--
ALTER TABLE `bot_users`
MODIFY `bot_id` int(10) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `bracket`
--
ALTER TABLE `bracket`
MODIFY `bracket_id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `categories`
--
ALTER TABLE `categories`
MODIFY `category_id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `character`
--
ALTER TABLE `character`
MODIFY `character_id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `nominee`
--
ALTER TABLE `nominee`
MODIFY `nominee_id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `round`
--
ALTER TABLE `round`
MODIFY `round_id` int(11) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `typeahead`
--
ALTER TABLE `typeahead`
MODIFY `typeahead_id` bigint(20) NOT NULL AUTO_INCREMENT;
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `bracket`
--
ALTER TABLE `bracket`
ADD CONSTRAINT `FK_winner_character_id` FOREIGN KEY (`winner_character_id`) REFERENCES `character` (`character_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `bracket_owners`
--
ALTER TABLE `bracket_owners`
ADD CONSTRAINT `FK_bracket_owners_bracket_id` FOREIGN KEY (`bracket_id`) REFERENCES `bracket` (`bracket_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_bracket_owners_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `character`
--
ALTER TABLE `character`
ADD CONSTRAINT `FK_character_bracket_id` FOREIGN KEY (`bracket_id`) REFERENCES `bracket` (`bracket_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `logins`
--
ALTER TABLE `logins`
ADD CONSTRAINT `FK_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`);
--
-- Constraints for table `mal_xref`
--
ALTER TABLE `mal_xref`
ADD CONSTRAINT `FK_mal_xref_mal_child` FOREIGN KEY (`mal_child`) REFERENCES `mal_items` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_mal_xref_mal_parent` FOREIGN KEY (`mal_parent`) REFERENCES `mal_items` (`item_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `nominee`
--
ALTER TABLE `nominee`
ADD CONSTRAINT `FK_nominee_bracket_id` FOREIGN KEY (`bracket_id`) REFERENCES `bracket` (`bracket_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `round`
--
ALTER TABLE `round`
ADD CONSTRAINT `FK_round_bracket_id` FOREIGN KEY (`bracket_id`) REFERENCES `bracket` (`bracket_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_round_character1_id` FOREIGN KEY (`round_character1_id`) REFERENCES `character` (`character_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_round_character2_id` FOREIGN KEY (`round_character2_id`) REFERENCES `character` (`character_id`) ON DELETE CASCADE ON UPDATE CASCADE;
--
-- Constraints for table `votes`
--
ALTER TABLE `votes`
ADD CONSTRAINT `FK_vote_character_id` FOREIGN KEY (`character_id`) REFERENCES `character` (`character_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_vote_round_id` FOREIGN KEY (`round_id`) REFERENCES `round` (`round_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_votes_bracket_id` FOREIGN KEY (`bracket_id`) REFERENCES `bracket` (`bracket_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `FK_votes_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;