/*
 * Databus API 凭证绑定与访问日志补充字段（DM8）
 * Generated on 2025-12-09
 */

-- ----------------------------
-- Table structure for databus_api_definition_credential
-- ----------------------------
CREATE TABLE databus_api_definition_credential (
    id BIGINT NOT NULL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    api_id BIGINT NOT NULL,
    credential_id BIGINT NOT NULL,
    app_id VARCHAR(128),
    creator VARCHAR(64) DEFAULT '' NOT NULL,
    create_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
    updater VARCHAR(64) DEFAULT '' NOT NULL,
    update_time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
    deleted BIT DEFAULT '0' NOT NULL
);

-- 去掉错误的唯一索引逻辑
-- CREATE UNIQUE INDEX uk_databus_api_definition_credential ON databus_api_definition_credential (api_id, credential_id, deleted);
CREATE INDEX idx_databus_api_definition_credential_api ON databus_api_definition_credential (api_id);
CREATE INDEX idx_databus_api_definition_credential_cred ON databus_api_definition_credential (credential_id);

COMMENT ON TABLE databus_api_definition_credential IS 'Databus API 凭证绑定表';
COMMENT ON COLUMN databus_api_definition_credential.id IS '主键 ID';
COMMENT ON COLUMN databus_api_definition_credential.tenant_id IS '租户编号';
COMMENT ON COLUMN databus_api_definition_credential.api_id IS 'API 定义 ID';
COMMENT ON COLUMN databus_api_definition_credential.credential_id IS '凭证 ID';
COMMENT ON COLUMN databus_api_definition_credential.app_id IS '凭证应用标识冗余';
COMMENT ON COLUMN databus_api_definition_credential.creator IS '创建者';
COMMENT ON COLUMN databus_api_definition_credential.create_time IS '创建时间';
COMMENT ON COLUMN databus_api_definition_credential.updater IS '更新者';
COMMENT ON COLUMN databus_api_definition_credential.update_time IS '更新时间';
COMMENT ON COLUMN databus_api_definition_credential.deleted IS '逻辑删除标记';

-- ----------------------------
-- Alter databus_api_access_log add credential columns
-- ----------------------------
ALTER TABLE databus_api_access_log ADD credential_app_id VARCHAR(128);
COMMENT ON COLUMN databus_api_access_log.credential_app_id IS '调用凭证应用标识';

ALTER TABLE databus_api_access_log ADD credential_id BIGINT;
COMMENT ON COLUMN databus_api_access_log.credential_id IS '调用凭证 ID';
