SET NAMES utf8mb4;

DROP PROCEDURE IF EXISTS `sp_add_column_if_missing`;

DELIMITER $$
CREATE PROCEDURE `sp_add_column_if_missing`(
    IN in_table_name VARCHAR(128),
    IN in_column_name VARCHAR(128),
    IN in_definition TEXT
)
BEGIN
    IF NOT 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, '` ADD COLUMN ', in_definition);
        PREPARE stmt FROM @ddl;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END $$
DELIMITER ;

CALL `sp_add_column_if_missing`('tb_workflow', 'publish_status',
                                '`publish_status` varchar(32) NOT NULL DEFAULT ''DRAFT'' COMMENT ''发布状态''');
CALL `sp_add_column_if_missing`('tb_workflow', 'current_approval_instance_id',
                                '`current_approval_instance_id` bigint DEFAULT NULL COMMENT ''当前审批实例ID''');
CALL `sp_add_column_if_missing`('tb_workflow', 'published_snapshot_json',
                                '`published_snapshot_json` json DEFAULT NULL COMMENT ''已发布快照''');
CALL `sp_add_column_if_missing`('tb_workflow', 'published_at',
                                '`published_at` datetime DEFAULT NULL COMMENT ''发布时间''');
CALL `sp_add_column_if_missing`('tb_workflow', 'published_by',
                                '`published_by` bigint DEFAULT NULL COMMENT ''发布人''');

CALL `sp_add_column_if_missing`('tb_document_collection', 'publish_status',
                                '`publish_status` varchar(32) NOT NULL DEFAULT ''DRAFT'' COMMENT ''发布状态''');
CALL `sp_add_column_if_missing`('tb_document_collection', 'current_approval_instance_id',
                                '`current_approval_instance_id` bigint DEFAULT NULL COMMENT ''当前审批实例ID''');
CALL `sp_add_column_if_missing`('tb_document_collection', 'published_snapshot_json',
                                '`published_snapshot_json` json DEFAULT NULL COMMENT ''已发布快照''');
CALL `sp_add_column_if_missing`('tb_document_collection', 'published_at',
                                '`published_at` datetime DEFAULT NULL COMMENT ''发布时间''');
CALL `sp_add_column_if_missing`('tb_document_collection', 'published_by',
                                '`published_by` bigint DEFAULT NULL COMMENT ''发布人''');

CALL `sp_add_column_if_missing`('tb_bot', 'publish_status',
                                '`publish_status` varchar(32) NOT NULL DEFAULT ''DRAFT'' COMMENT ''发布状态''');
CALL `sp_add_column_if_missing`('tb_bot', 'current_approval_instance_id',
                                '`current_approval_instance_id` bigint DEFAULT NULL COMMENT ''当前审批实例ID''');
CALL `sp_add_column_if_missing`('tb_bot', 'published_snapshot_json',
                                '`published_snapshot_json` json DEFAULT NULL COMMENT ''已发布快照''');
CALL `sp_add_column_if_missing`('tb_bot', 'published_at',
                                '`published_at` datetime DEFAULT NULL COMMENT ''发布时间''');
CALL `sp_add_column_if_missing`('tb_bot', 'published_by',
                                '`published_by` bigint DEFAULT NULL COMMENT ''发布人''');

DROP PROCEDURE IF EXISTS `sp_add_column_if_missing`;
