/*
 * Databus API portal schema for DM8
 * Generated on 2025-10-10
 */

-- ----------------------------
-- Table structure for databus_api_definition
-- ----------------------------
CREATE TABLE databus_api_definition (
    id BIGINT NOT NULL PRIMARY KEY,
    tenant_id BIGINT NOT NULL,
    api_code VARCHAR(128) NOT NULL,
    uri_pattern VARCHAR(256) NOT NULL,
    http_method VARCHAR(16) NOT NULL,
    version VARCHAR(32) NOT NULL,
    status SMALLINT DEFAULT 0 NOT NULL,
    description VARCHAR(512),
    auth_policy_id BIGINT,
    rate_limit_id BIGINT,
    response_template CLOB,
    cache_strategy VARCHAR(128),
    updated_at DATETIME,
    grey_released BIT DEFAULT '0' NOT NULL,
    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_code_ver ON databus_api_definition (tenant_id, api_code, version);
CREATE INDEX idx_databus_api_definition_status ON databus_api_definition (tenant_id, status);
CREATE INDEX idx_databus_api_definition_policy ON databus_api_definition (tenant_id, auth_policy_id, rate_limit_id);

COMMENT ON TABLE databus_api_definition IS '统一外部 API 门户 - API 定义表';
COMMENT ON COLUMN databus_api_definition.id IS '主键 ID';
COMMENT ON COLUMN databus_api_definition.tenant_id IS '租户编号';
COMMENT ON COLUMN databus_api_definition.api_code IS 'API 编码';
COMMENT ON COLUMN databus_api_definition.uri_pattern IS '匹配路径模板';
COMMENT ON COLUMN databus_api_definition.http_method IS 'HTTP 方法';
COMMENT ON COLUMN databus_api_definition.version IS '版本号';
COMMENT ON COLUMN databus_api_definition.status IS '发布状态';
COMMENT ON COLUMN databus_api_definition.description IS '描述信息';
COMMENT ON COLUMN databus_api_definition.auth_policy_id IS '认证策略 ID';
COMMENT ON COLUMN databus_api_definition.rate_limit_id IS '限流策略 ID';
COMMENT ON COLUMN databus_api_definition.response_template IS '响应模板 JSON';
COMMENT ON COLUMN databus_api_definition.cache_strategy IS '缓存策略配置';
COMMENT ON COLUMN databus_api_definition.updated_at IS '业务更新时间';
COMMENT ON COLUMN databus_api_definition.grey_released IS '灰度发布标记';
COMMENT ON COLUMN databus_api_definition.creator IS '创建者';
COMMENT ON COLUMN databus_api_definition.create_time IS '创建时间';
COMMENT ON COLUMN databus_api_definition.updater IS '更新者';
COMMENT ON COLUMN databus_api_definition.update_time IS '更新时间';
COMMENT ON COLUMN databus_api_definition.deleted IS '逻辑删除标记';

-- ----------------------------
-- Table structure for databus_api_flow_publish
-- ----------------------------
CREATE TABLE databus_api_flow_publish (
    id BIGINT NOT NULL PRIMARY KEY,
    tenant_id BIGINT NOT NULL,
    api_id BIGINT NOT NULL,
    release_tag VARCHAR(64),
    snapshot CLOB,
    status VARCHAR(32),
    active BIT DEFAULT '0' NOT NULL,
    description VARCHAR(512),
    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 INDEX idx_databus_api_flow_publish_api ON databus_api_flow_publish (tenant_id, api_id, active);

COMMENT ON TABLE databus_api_flow_publish IS '统一外部 API 门户 - 发布记录表';
COMMENT ON COLUMN databus_api_flow_publish.id IS '主键 ID';
COMMENT ON COLUMN databus_api_flow_publish.tenant_id IS '租户编号';
COMMENT ON COLUMN databus_api_flow_publish.api_id IS '关联的 API ID';
COMMENT ON COLUMN databus_api_flow_publish.release_tag IS '发布批次标识';
COMMENT ON COLUMN databus_api_flow_publish.snapshot IS '配置快照 JSON';
COMMENT ON COLUMN databus_api_flow_publish.status IS '发布状态';
COMMENT ON COLUMN databus_api_flow_publish.active IS '是否当前生效';
COMMENT ON COLUMN databus_api_flow_publish.description IS '备注信息';
COMMENT ON COLUMN databus_api_flow_publish.creator IS '创建者';
COMMENT ON COLUMN databus_api_flow_publish.create_time IS '创建时间';
COMMENT ON COLUMN databus_api_flow_publish.updater IS '更新者';
COMMENT ON COLUMN databus_api_flow_publish.update_time IS '更新时间';
COMMENT ON COLUMN databus_api_flow_publish.deleted IS '逻辑删除标记';

-- ----------------------------
-- Table structure for databus_policy_auth
-- ----------------------------
CREATE TABLE databus_policy_auth (
    id BIGINT NOT NULL PRIMARY KEY,
    tenant_id BIGINT NOT NULL,
    name VARCHAR(128) NOT NULL,
    type VARCHAR(64) NOT NULL,
    config CLOB,
    description VARCHAR(512),
    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_policy_auth_name ON databus_policy_auth (tenant_id, name);

COMMENT ON TABLE databus_policy_auth IS '统一外部 API 门户 - 认证策略表';
COMMENT ON COLUMN databus_policy_auth.id IS '主键 ID';
COMMENT ON COLUMN databus_policy_auth.tenant_id IS '租户编号';
COMMENT ON COLUMN databus_policy_auth.name IS '策略名称';
COMMENT ON COLUMN databus_policy_auth.type IS '策略类型';
COMMENT ON COLUMN databus_policy_auth.config IS '策略配置 JSON';
COMMENT ON COLUMN databus_policy_auth.description IS '描述信息';
COMMENT ON COLUMN databus_policy_auth.creator IS '创建者';
COMMENT ON COLUMN databus_policy_auth.create_time IS '创建时间';
COMMENT ON COLUMN databus_policy_auth.updater IS '更新者';
COMMENT ON COLUMN databus_policy_auth.update_time IS '更新时间';
COMMENT ON COLUMN databus_policy_auth.deleted IS '逻辑删除标记';

-- ----------------------------
-- Table structure for databus_policy_rate_limit
-- ----------------------------
CREATE TABLE databus_policy_rate_limit (
    id BIGINT NOT NULL PRIMARY KEY,
    tenant_id BIGINT NOT NULL,
    name VARCHAR(128) NOT NULL,
    type VARCHAR(64) NOT NULL,
    config CLOB,
    description VARCHAR(512),
    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_policy_rate_limit_name ON databus_policy_rate_limit (tenant_id, name);

COMMENT ON TABLE databus_policy_rate_limit IS '统一外部 API 门户 - 限流策略表';
COMMENT ON COLUMN databus_policy_rate_limit.id IS '主键 ID';
COMMENT ON COLUMN databus_policy_rate_limit.tenant_id IS '租户编号';
COMMENT ON COLUMN databus_policy_rate_limit.name IS '策略名称';
COMMENT ON COLUMN databus_policy_rate_limit.type IS '策略类型';
COMMENT ON COLUMN databus_policy_rate_limit.config IS '策略配置 JSON';
COMMENT ON COLUMN databus_policy_rate_limit.description IS '描述信息';
COMMENT ON COLUMN databus_policy_rate_limit.creator IS '创建者';
COMMENT ON COLUMN databus_policy_rate_limit.create_time IS '创建时间';
COMMENT ON COLUMN databus_policy_rate_limit.updater IS '更新者';
COMMENT ON COLUMN databus_policy_rate_limit.update_time IS '更新时间';
COMMENT ON COLUMN databus_policy_rate_limit.deleted IS '逻辑删除标记';

-- ----------------------------
-- Table structure for databus_api_step
-- ----------------------------
CREATE TABLE databus_api_step (
    id BIGINT NOT NULL PRIMARY KEY,
    tenant_id BIGINT NOT NULL,
    api_id BIGINT NOT NULL,
    step_order INT DEFAULT 0 NOT NULL,
    parallel_group VARCHAR(64),
    type VARCHAR(32) NOT NULL,
    target_endpoint VARCHAR(512),
    request_mapping_expr CLOB,
    response_mapping_expr CLOB,
    transform_id BIGINT,
    timeout BIGINT,
    retry_strategy CLOB,
    fallback_strategy CLOB,
    condition_expr CLOB,
    stop_on_error BIT DEFAULT '0' NOT NULL,
    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 INDEX idx_databus_api_step_api_order ON databus_api_step (tenant_id, api_id, parallel_group, step_order);

COMMENT ON TABLE databus_api_step IS '统一外部 API 门户 - 编排步骤表';
COMMENT ON COLUMN databus_api_step.id IS '主键 ID';
COMMENT ON COLUMN databus_api_step.tenant_id IS '租户编号';
COMMENT ON COLUMN databus_api_step.api_id IS '关联的 API ID';
COMMENT ON COLUMN databus_api_step.step_order IS '执行顺序';
COMMENT ON COLUMN databus_api_step.parallel_group IS '并行分组标识';
COMMENT ON COLUMN databus_api_step.type IS '步骤类型';
COMMENT ON COLUMN databus_api_step.target_endpoint IS '目标端点';
COMMENT ON COLUMN databus_api_step.request_mapping_expr IS '请求映射表达式';
COMMENT ON COLUMN databus_api_step.response_mapping_expr IS '响应映射表达式';
COMMENT ON COLUMN databus_api_step.transform_id IS '默认变换 ID';
COMMENT ON COLUMN databus_api_step.timeout IS '超时时间(毫秒)';
COMMENT ON COLUMN databus_api_step.retry_strategy IS '重试策略 JSON';
COMMENT ON COLUMN databus_api_step.fallback_strategy IS '降级策略 JSON';
COMMENT ON COLUMN databus_api_step.condition_expr IS '执行条件表达式';
COMMENT ON COLUMN databus_api_step.stop_on_error IS '出错是否终止';
COMMENT ON COLUMN databus_api_step.creator IS '创建者';
COMMENT ON COLUMN databus_api_step.create_time IS '创建时间';
COMMENT ON COLUMN databus_api_step.updater IS '更新者';
COMMENT ON COLUMN databus_api_step.update_time IS '更新时间';
COMMENT ON COLUMN databus_api_step.deleted IS '逻辑删除标记';

-- ----------------------------
-- Table structure for databus_api_transform
-- ----------------------------
CREATE TABLE databus_api_transform (
    id BIGINT NOT NULL PRIMARY KEY,
    tenant_id BIGINT NOT NULL,
    api_id BIGINT,
    step_id BIGINT,
    phase VARCHAR(32) NOT NULL,
    expression_type VARCHAR(32) NOT NULL,
    expression CLOB NOT NULL,
    description VARCHAR(512),
    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 INDEX idx_databus_api_transform_api ON databus_api_transform (tenant_id, api_id);
CREATE INDEX idx_databus_api_transform_step ON databus_api_transform (tenant_id, step_id);

COMMENT ON TABLE databus_api_transform IS '统一外部 API 门户 - 变换配置表';
COMMENT ON COLUMN databus_api_transform.id IS '主键 ID';
COMMENT ON COLUMN databus_api_transform.tenant_id IS '租户编号';
COMMENT ON COLUMN databus_api_transform.api_id IS '关联的 API ID';
COMMENT ON COLUMN databus_api_transform.step_id IS '关联的步骤 ID';
COMMENT ON COLUMN databus_api_transform.phase IS '执行阶段';
COMMENT ON COLUMN databus_api_transform.expression_type IS '表达式类型';
COMMENT ON COLUMN databus_api_transform.expression IS '表达式内容';
COMMENT ON COLUMN databus_api_transform.description IS '描述信息';
COMMENT ON COLUMN databus_api_transform.creator IS '创建者';
COMMENT ON COLUMN databus_api_transform.create_time IS '创建时间';
COMMENT ON COLUMN databus_api_transform.updater IS '更新者';
COMMENT ON COLUMN databus_api_transform.update_time IS '更新时间';
COMMENT ON COLUMN databus_api_transform.deleted IS '逻辑删除标记';
