-- Nacos MySQL Schema for Config and Service Registration Persistence
-- Nacos版本: 2.2.x
-- 数据库名: nacos_config

CREATE DATABASE IF NOT EXISTS nacos_config CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE nacos_config;

-- ---------------------------------------------------------------
-- Config Info 配置信息表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS config_info (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT 'id',
    data_id VARCHAR(255) NOT NULL COMMENT 'data_id',
    group_id VARCHAR(128) NOT NULL COMMENT 'group_id',
    content LONGTEXT NOT NULL COMMENT 'content',
    md5 VARCHAR(32) DEFAULT NULL COMMENT 'md5',
    gmt_create DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    gmt_modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
    src_user TEXT COMMENT '源用户',
    src_ip VARCHAR(50) DEFAULT NULL COMMENT '源IP',
    app_name VARCHAR(128) DEFAULT NULL COMMENT '应用名',
    tenant_id VARCHAR(128) DEFAULT '' COMMENT '租户ID',
    c_desc VARCHAR(256) DEFAULT NULL COMMENT '描述',
    c_use VARCHAR(64) DEFAULT NULL COMMENT '使用方式',
    effect VARCHAR(64) DEFAULT NULL COMMENT '生效范围',
    type VARCHAR(64) DEFAULT NULL COMMENT '配置类型',
    c_schema TEXT COMMENT 'schema',
    encrypted_data_key TEXT DEFAULT NULL COMMENT 'encrypted_data_key',
    PRIMARY KEY (id),
    UNIQUE KEY uk_configinfo_datagrouptenant (data_id, group_id, tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='config_info';

-- ---------------------------------------------------------------
-- Config Tags Relation 配置标签关系表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS config_tags_relation (
    id BIGINT NOT NULL COMMENT 'id',
    tag_name VARCHAR(128) NOT NULL COMMENT 'tag_name',
    tag_type VARCHAR(64) DEFAULT NULL COMMENT 'tag_type',
    data_id VARCHAR(255) NOT NULL COMMENT 'data_id',
    group_id VARCHAR(128) NOT NULL COMMENT 'group_id',
    tenant_id VARCHAR(128) DEFAULT '' COMMENT 'tenant_id',
    nid BIGINT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (nid),
    UNIQUE KEY uk_configtagrelation_configidtag (id, tag_name, tag_type),
    KEY idx_tenant_id (tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='config_tag_relation';

-- ---------------------------------------------------------------
-- Group Capacity 表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS group_capacity (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    group_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'Group ID，空字符表示所有分组',
    quota INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '配额，0表示不限制',
    usage INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '使用量',
    max_size INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '单个配置大小上限，单位字节，0表示不限制',
    max_aggr_count INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '聚合配置子配置最大个数，0表示不限制',
    max_aggr_size INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '单个聚合配置的最大值，单位字节，0表示不限制',
    max_history_count INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '最大变更历史数',
    gmt_create DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    gmt_modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (id),
    UNIQUE KEY uk_group_id (group_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='group_capacity';

-- ---------------------------------------------------------------
-- Tenant Capacity 表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tenant_capacity (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
    tenant_id VARCHAR(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
    quota INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '配额，0表示不限制',
    usage INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '使用量',
    max_size INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '单个配置大小上限，单位字节，0表示不限制',
    max_aggr_count INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '聚合配置子配置最大个数，0表示不限制',
    max_aggr_size INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '单个聚合配置的最大值，单位字节，0表示不限制',
    max_history_count INT UNSIGNED NOT NULL DEFAULT '0' COMMENT '最大变更历史数',
    gmt_create DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    gmt_modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
    PRIMARY KEY (id),
    UNIQUE KEY uk_tenant_id (tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='tenant_capacity';

-- ---------------------------------------------------------------
-- Tenant Info 租户信息表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS tenant_info (
    id BIGINT NOT NULL AUTO_INCREMENT COMMENT 'id',
    kp VARCHAR(128) NOT NULL COMMENT 'kp',
    tenant_id VARCHAR(128) DEFAULT '' COMMENT 'tenant_id',
    tenant_name VARCHAR(128) DEFAULT '' COMMENT 'tenant_name',
    tenant_desc VARCHAR(256) DEFAULT NULL COMMENT 'tenant_desc',
    create_source VARCHAR(32) DEFAULT NULL COMMENT 'create_source',
    gmt_create BIGINT NOT NULL COMMENT '创建时间',
    gmt_modified BIGINT NOT NULL COMMENT '修改时间',
    PRIMARY KEY (id),
    UNIQUE KEY uk_tenant_info_kptenantid (kp, tenant_id),
    KEY idx_tenant_id (tenant_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='tenant_info';

-- ---------------------------------------------------------------
-- Users 用户表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS users (
    username VARCHAR(50) NOT NULL PRIMARY KEY COMMENT 'username',
    password VARCHAR(500) NOT NULL COMMENT 'password',
    enabled BOOLEAN NOT NULL COMMENT 'enabled'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='users';

-- ---------------------------------------------------------------
-- Roles 角色表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS roles (
    username VARCHAR(50) NOT NULL COMMENT 'username',
    role VARCHAR(50) NOT NULL COMMENT 'role',
    UNIQUE INDEX idx_user_role (username ASC, role ASC) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='roles';

-- ---------------------------------------------------------------
-- Permissions 权限表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS permissions (
    role VARCHAR(50) NOT NULL COMMENT 'role',
    resource VARCHAR(255) NOT NULL COMMENT 'resource',
    action VARCHAR(8) NOT NULL COMMENT 'action',
    UNIQUE INDEX uk_role_permission (role, resource, action) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='permissions';

-- ---------------------------------------------------------------
-- Initialize Default Users (Nacos Console)
-- Nacos 2.2.x 默认用户名/密码: nacos/nacos
-- 生产环境务必修改！
-- ---------------------------------------------------------------
INSERT INTO users (username, password, enabled) VALUES
('nacos', '{BCrypt}$2a$10$YSQRzLJWk1OLaNOX4vP39eu2tY1LQLeLPxKnF5Vfl2h2JHKtVVD.K', TRUE);

INSERT INTO roles (username, role) VALUES
('nacos', 'ROLE_ADMIN');

-- ---------------------------------------------------------------
-- Config History 配置变更历史表
-- ---------------------------------------------------------------
CREATE TABLE IF NOT EXISTS config_history (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    app_name VARCHAR(128) DEFAULT NULL COMMENT 'appName',
    bean_name VARCHAR(128) DEFAULT NULL COMMENT 'beanName',
    change_data LONGTEXT DEFAULT NULL COMMENT 'changeData',
    md5 VARCHAR(32) DEFAULT NULL,
    gmt_create DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    gmt_modified DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
    op_type CHAR(10) DEFAULT NULL COMMENT 'opType',
    data_id VARCHAR(255) DEFAULT NULL COMMENT 'data_id',
    group_id VARCHAR(128) DEFAULT NULL COMMENT 'group_id',
    tenant_id VARCHAR(128) DEFAULT '' COMMENT 'tenant_id',
    type VARCHAR(64) DEFAULT NULL COMMENT 'type',
    last_sync_time DATETIME DEFAULT NULL COMMENT 'last_sync_time',
    INDEX idx_gmt_create (gmt_create),
    INDEX idx_gmt_modified (gmt_modified),
    INDEX idx_data_id (data_id),
    INDEX idx_group_id (group_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='config_history';
