Last active
December 26, 2015 03:49
-
-
Save dtcooper/7088747 to your computer and use it in GitHub Desktop.
Conditionally run an ALTER
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Emulate an IF EXISTS for ALTER TABLE ... DROP COLUMN */ | |
SELECT COUNT(*) INTO @exists FROM `information_schema`.`COLUMNS` | |
WHERE `TABLE_SCHEMA` = DATABASE() | |
AND `COLUMN_NAME` = 'link_type' | |
AND `TABLE_NAME` = 'Social_Identity'; | |
SET @query = IF( | |
@exists = 1, | |
'ALTER TABLE `Social_Identity` DROP COLUMN `link_type`', | |
'SELECT "Column does not exist"'); | |
PREPARE stmt FROM @query; | |
EXECUTE stmt; | |
/* Perform the ALTER */ | |
ALTER TABLE `Social_Identity` | |
ADD COLUMN `link_type` tinyint UNSIGNED NOT NULL DEFAULT 1 | |
COMMENT "Link type of connected user (default 1 = weak link)"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment