DELIMITER //
CREATE PROCEDURE microservices.AddColumnsIfNotExists()
BEGIN
DECLARE user_name_exists INT DEFAULT 0;
DECLARE account_exists INT DEFAULT 0;
<code>-- Check if the column 'user_name' exists
SELECT COUNT(*)
INTO user_name_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = 'user' AND COLUMN_NAME = 'user_name';
-- Check if the column 'account' exists
SELECT COUNT(*)
INTO account_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = 'user' AND COLUMN_NAME = 'Accounts';
-- If neither column exists, add both
IF user_name_exists = 0 AND account_exists = 0 THEN
ALTER TABLE microservices.user ADD COLUMN `user_name` JSON NOT NULL,
ADD COLUMN `Accounts` JSON NOT NULL;
-- If only 'user_name' does not exist, add it
ELSEIF user_name_exists = 0 AND account_exists > 0 THEN
ALTER TABLE microservices.user ADD COLUMN `user_name` JSON NOT NULL;
-- If only 'account' does not exist, add it
ELSEIF user_name_exists > 0 AND account_exists = 0 THEN
ALTER TABLE microservices.user ADD COLUMN `Accounts` JSON NOT NULL;
END IF;
</code>
<code>-- Check if the column 'user_name' exists
SELECT COUNT(*)
INTO user_name_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = 'user' AND COLUMN_NAME = 'user_name';
-- Check if the column 'account' exists
SELECT COUNT(*)
INTO account_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = 'user' AND COLUMN_NAME = 'Accounts';
-- If neither column exists, add both
IF user_name_exists = 0 AND account_exists = 0 THEN
ALTER TABLE microservices.user ADD COLUMN `user_name` JSON NOT NULL,
ADD COLUMN `Accounts` JSON NOT NULL;
-- If only 'user_name' does not exist, add it
ELSEIF user_name_exists = 0 AND account_exists > 0 THEN
ALTER TABLE microservices.user ADD COLUMN `user_name` JSON NOT NULL;
-- If only 'account' does not exist, add it
ELSEIF user_name_exists > 0 AND account_exists = 0 THEN
ALTER TABLE microservices.user ADD COLUMN `Accounts` JSON NOT NULL;
END IF;
</code>
-- Check if the column 'user_name' exists
SELECT COUNT(*)
INTO user_name_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = 'user' AND COLUMN_NAME = 'user_name';
-- Check if the column 'account' exists
SELECT COUNT(*)
INTO account_exists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = SCHEMA() AND TABLE_NAME = 'user' AND COLUMN_NAME = 'Accounts';
-- If neither column exists, add both
IF user_name_exists = 0 AND account_exists = 0 THEN
ALTER TABLE microservices.user ADD COLUMN `user_name` JSON NOT NULL,
ADD COLUMN `Accounts` JSON NOT NULL;
-- If only 'user_name' does not exist, add it
ELSEIF user_name_exists = 0 AND account_exists > 0 THEN
ALTER TABLE microservices.user ADD COLUMN `user_name` JSON NOT NULL;
-- If only 'account' does not exist, add it
ELSEIF user_name_exists > 0 AND account_exists = 0 THEN
ALTER TABLE microservices.user ADD COLUMN `Accounts` JSON NOT NULL;
END IF;
END //
DELIMITER ;
CALL microservices
.AddColumnsIfNotExists
();
DROP PROCEDURE IF EXISTS microservices.AddColumnsIfNotExists;
SELECT * FROM microservices.user;
ALTER table microservices.user drop column Accounts
, drop column user_name
;
ALTER table microservices.user drop column user_name
;
is it correct way of writing a query for liquibase ?