Skip to content

Instantly share code, notes, and snippets.

@dtcooper
Last active December 26, 2015 03:49
Show Gist options
  • Save dtcooper/7088747 to your computer and use it in GitHub Desktop.
Save dtcooper/7088747 to your computer and use it in GitHub Desktop.
Conditionally run an ALTER
/* 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