-- Databus API 凭证绑定 & 访问日志补充字段

-- API 与客户凭证绑定表
CREATE TABLE IF NOT EXISTS `databus_api_definition_credential` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  `api_id` bigint NOT NULL COMMENT 'API 定义 ID',
  `credential_id` bigint NOT NULL COMMENT '凭证 ID',
  `app_id` varchar(128) DEFAULT NULL COMMENT '凭证应用标识冗余',
  `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',
  `creator` varchar(64) DEFAULT '' COMMENT '创建者',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updater` varchar(64) DEFAULT '' COMMENT '更新者',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_api_credential` (`api_id`, `credential_id`, `deleted`),
  KEY `idx_api_id` (`api_id`),
  KEY `idx_credential_id` (`credential_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Databus API 凭证绑定表';

-- 访问日志补充凭证信息
ALTER TABLE `databus_api_access_log`
  ADD COLUMN IF NOT EXISTS `credential_app_id` varchar(128) NULL COMMENT '调用凭证应用标识' AFTER `api_version`;
ALTER TABLE `databus_api_access_log`
  ADD COLUMN IF NOT EXISTS `credential_id` bigint NULL COMMENT '调用凭证 ID' AFTER `credential_app_id`;
