Skip to content

Instantly share code, notes, and snippets.

@thaniaclair
Created July 17, 2013 15:34
Show Gist options
  • Save thaniaclair/6021700 to your computer and use it in GitHub Desktop.
Save thaniaclair/6021700 to your computer and use it in GitHub Desktop.
AddColumnUnlessExists
drop procedure if exists AddColumnUnlessExists;
delimiter '//'
create procedure AddColumnUnlessExists(
IN dbName tinytext,
IN tableName tinytext,
IN ddl text)
begin
IF NOT EXISTS (SELECT * FROM information_schema.COLUMNS
WHERE column_name=fieldName
and table_name=tableName
and table_schema=dbName)
THEN
prepare stmt from ddl;
execute stmt;
END IF;
end;
//
delimiter ';'
call AddColumnUnlessExists('goeuro', 'seo_data', 'ALTER TABLE goeuro.seo_data ADD json_compressed_data BLOB NULL DEFAULT NULL AFTER json_data');
call AddColumnUnlessExists('goeuro', 'seo_data', 'ALTER TABLE goeuro.seo_data CHANGE json_data json_data TEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment