SET NAMES utf8mb4;

DROP PROCEDURE IF EXISTS `sp_drop_index_if_exists`;
DROP PROCEDURE IF EXISTS `sp_drop_column_if_exists`;
DROP PROCEDURE IF EXISTS `sp_rename_column_if_exists`;
DROP PROCEDURE IF EXISTS `sp_add_index_if_missing`;

DELIMITER $$
CREATE PROCEDURE `sp_drop_index_if_exists`(
    IN in_table_name VARCHAR(128),
    IN in_index_name VARCHAR(128)
)
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = in_table_name
          AND INDEX_NAME = in_index_name
    ) THEN
        SET @ddl = CONCAT('ALTER TABLE `', in_table_name, '` DROP INDEX `', in_index_name, '`');
        PREPARE stmt FROM @ddl;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END $$

CREATE PROCEDURE `sp_drop_column_if_exists`(
    IN in_table_name VARCHAR(128),
    IN in_column_name VARCHAR(128)
)
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = in_table_name
          AND COLUMN_NAME = in_column_name
    ) THEN
        SET @ddl = CONCAT('ALTER TABLE `', in_table_name, '` DROP COLUMN `', in_column_name, '`');
        PREPARE stmt FROM @ddl;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END $$

CREATE PROCEDURE `sp_rename_column_if_exists`(
    IN in_table_name VARCHAR(128),
    IN in_old_column VARCHAR(128),
    IN in_new_column VARCHAR(128),
    IN in_definition TEXT
)
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = in_table_name
          AND COLUMN_NAME = in_old_column
    ) AND NOT EXISTS (
        SELECT 1
        FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = in_table_name
          AND COLUMN_NAME = in_new_column
    ) THEN
        SET @ddl = CONCAT(
            'ALTER TABLE `', in_table_name, '` RENAME COLUMN `', in_old_column, '` TO `', in_new_column, '`'
        );
        PREPARE stmt FROM @ddl;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;

        SET @ddl = CONCAT(
            'ALTER TABLE `', in_table_name, '` MODIFY COLUMN `', in_new_column, '` ', in_definition
        );
        PREPARE stmt FROM @ddl;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END $$

CREATE PROCEDURE `sp_add_index_if_missing`(
    IN in_table_name VARCHAR(128),
    IN in_index_name VARCHAR(128),
    IN in_definition TEXT
)
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = in_table_name
          AND INDEX_NAME = in_index_name
    ) THEN
        SET @ddl = CONCAT('ALTER TABLE `', in_table_name, '` ADD ', in_definition);
        PREPARE stmt FROM @ddl;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END $$
DELIMITER ;

CALL `sp_drop_index_if_exists`('tb_knowledge_share', 'uni_knowledge_share_code');
CALL `sp_rename_column_if_exists`(
    'tb_knowledge_share',
    'share_token_hash',
    'share_key_hash',
    'varchar(128) NOT NULL COMMENT ''分享访问密钥哈希'''
);
CALL `sp_drop_column_if_exists`('tb_knowledge_share', 'share_code');
CALL `sp_add_index_if_missing`(
    'tb_knowledge_share',
    'uni_knowledge_share_key_hash',
    'UNIQUE INDEX `uni_knowledge_share_key_hash`(`share_key_hash`) USING BTREE'
);

DROP PROCEDURE IF EXISTS `sp_drop_index_if_exists`;
DROP PROCEDURE IF EXISTS `sp_drop_column_if_exists`;
DROP PROCEDURE IF EXISTS `sp_rename_column_if_exists`;
DROP PROCEDURE IF EXISTS `sp_add_index_if_missing`;
