Skip to content

Instantly share code, notes, and snippets.

@wakasann
Last active January 11, 2019 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakasann/58e10613321dfc497f6ffbdcb77e7cde to your computer and use it in GitHub Desktop.
Save wakasann/58e10613321dfc497f6ffbdcb77e7cde to your computer and use it in GitHub Desktop.
drop procedure if exists add_all_timestamp;
CREATE PROCEDURE add_all_timestamp()
BEGIN
DECLARE tableName varchar(30) default '';
DECLARE allAddr varchar(40) default '';
DECLARE tableSchemaName varchar(30) default 'test';
DECLARE done INT DEFAULT 0;
DECLARE curl CURSOR FOR select table_name from information_schema.tables where table_schema=tableSchemaName;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN curl;
REPEAT
FETCH curl INTO tableName;
IF not done THEN
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'deleted_at') THEN
set @sql=concat('alter table ',tableName,' ADD `deleted_at` TIMESTAMP NULL');
PREPARE stmt from @sql;
execute stmt;
END IF;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'created_at') THEN
set @sql2=concat('alter table ',tableName,' ADD `created_at` TIMESTAMP NULL');
PREPARE stmt2 from @sql2;
execute stmt2;
END IF;
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'updated_at') THEN
set @sql3=concat('alter table ',tableName,' ADD `updated_at` TIMESTAMP NULL');
PREPARE stmt3 from @sql3;
execute stmt3;
END IF;
END IF;
UNTIL done END REPEAT;
select allAddr;
CLOSE curl;
END;
call add_all_timestamp();
drop procedure if exists delete_timestamp;
CREATE PROCEDURE delete_timestamp()
BEGIN
DECLARE tableName varchar(30) default '';
DECLARE allAddr varchar(40) default '';
DECLARE tableSchemaName varchar(30) default 'test';
DECLARE done INT DEFAULT 0;
DECLARE curl CURSOR FOR select table_name from information_schema.tables where table_schema=tableSchemaName;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN curl;
REPEAT
FETCH curl INTO tableName;
IF not done THEN
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'deleted_at') THEN
set @sql=concat('alter table ',tableName,' DROP `deleted_at`');
PREPARE stmt from @sql;
execute stmt;
END IF;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'created_at') THEN
set @sql2=concat('alter table ',tableName,' DROP `created_at`');
PREPARE stmt2 from @sql2;
execute stmt2;
END IF;
IF EXISTS (SELECT * FROM information_schema.columns WHERE table_schema=tableSchemaName AND table_name = tableName AND column_name = 'updated_at') THEN
set @sql3=concat('alter table ',tableName,' DROP `updated_at`');
PREPARE stmt3 from @sql3;
execute stmt3;
END IF;
END IF;
UNTIL done END REPEAT;
select allAddr;
CLOSE curl;
END;
call delete_timestamp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment