You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
2.1 KiB
48 lines
2.1 KiB
USE dating_app;
|
|
|
|
-- Comment replies: nullable parent comment, self-referencing.
|
|
SET @column_exists = (
|
|
SELECT COUNT(*) FROM information_schema.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'post_comments' AND COLUMN_NAME = 'parent_comment_id'
|
|
);
|
|
SET @statement = IF(@column_exists = 0,
|
|
'ALTER TABLE post_comments ADD COLUMN parent_comment_id INT NULL AFTER user_id', 'SELECT 1');
|
|
PREPARE migration_statement FROM @statement;
|
|
EXECUTE migration_statement;
|
|
DEALLOCATE PREPARE migration_statement;
|
|
|
|
SET @index_exists = (
|
|
SELECT COUNT(*) FROM information_schema.STATISTICS
|
|
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'post_comments'
|
|
AND INDEX_NAME = 'ix_post_comments_parent'
|
|
);
|
|
SET @statement = IF(@index_exists = 0,
|
|
'ALTER TABLE post_comments ADD INDEX ix_post_comments_parent (parent_comment_id)', 'SELECT 1');
|
|
PREPARE migration_statement FROM @statement;
|
|
EXECUTE migration_statement;
|
|
DEALLOCATE PREPARE migration_statement;
|
|
|
|
SET @fk_exists = (
|
|
SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
|
|
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'post_comments'
|
|
AND CONSTRAINT_NAME = 'fk_post_comments_parent'
|
|
);
|
|
SET @statement = IF(@fk_exists = 0,
|
|
'ALTER TABLE post_comments ADD CONSTRAINT fk_post_comments_parent FOREIGN KEY (parent_comment_id) REFERENCES post_comments(id) ON DELETE CASCADE',
|
|
'SELECT 1');
|
|
PREPARE migration_statement FROM @statement;
|
|
EXECUTE migration_statement;
|
|
DEALLOCATE PREPARE migration_statement;
|
|
|
|
-- Comment likes.
|
|
CREATE TABLE IF NOT EXISTS comment_likes (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
comment_id INT NOT NULL,
|
|
user_id INT NOT NULL,
|
|
created_at DATETIME NOT NULL,
|
|
CONSTRAINT uq_comment_like UNIQUE (comment_id, user_id),
|
|
CONSTRAINT fk_comment_likes_comment FOREIGN KEY (comment_id) REFERENCES post_comments(id) ON DELETE CASCADE,
|
|
CONSTRAINT fk_comment_likes_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
|
INDEX ix_comment_likes_comment (comment_id),
|
|
INDEX ix_comment_likes_user (user_id)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
|
|