-- 达梦数据库（DM）兼容 nacos-db.sql 脚本
-- 由 MySQL 版本转换

CREATE TABLE config_info (
  id BIGINT NOT NULL IDENTITY,
  data_id VARCHAR(255) NOT NULL,
  group_id VARCHAR(255),
  content CLOB NOT NULL,
  md5 VARCHAR(32),
  gmt_create TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  gmt_modified TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  src_user CLOB,
  src_ip VARCHAR(20),
  app_name VARCHAR(128),
  tenant_id VARCHAR(128) DEFAULT '',
  c_desc VARCHAR(256),
  c_use VARCHAR(64),
  effect VARCHAR(64),
  type VARCHAR(64),
  c_schema CLOB,
  encrypted_data_key CLOB NOT NULL,
  CONSTRAINT pk_config_info PRIMARY KEY (id),
  CONSTRAINT uk_configinfo_datagrouptenant UNIQUE (data_id, group_id, tenant_id)
);

COMMENT ON TABLE config_info IS 'config_info';
COMMENT ON COLUMN config_info.id IS 'id';
COMMENT ON COLUMN config_info.data_id IS 'data_id';
COMMENT ON COLUMN config_info.content IS 'content';
COMMENT ON COLUMN config_info.md5 IS 'md5';
COMMENT ON COLUMN config_info.gmt_create IS '创建时间';
COMMENT ON COLUMN config_info.gmt_modified IS '修改时间';
COMMENT ON COLUMN config_info.src_user IS 'source user';
COMMENT ON COLUMN config_info.src_ip IS 'source ip';
COMMENT ON COLUMN config_info.tenant_id IS '租户字段';
COMMENT ON COLUMN config_info.encrypted_data_key IS '秘钥';

CREATE TABLE config_info_aggr (
  id BIGINT NOT NULL IDENTITY,
  data_id VARCHAR(255) NOT NULL,
  group_id VARCHAR(255) NOT NULL,
  datum_id VARCHAR(255) NOT NULL,
  content CLOB NOT NULL,
  gmt_modified TIMESTAMP NOT NULL,
  app_name VARCHAR(128),
  tenant_id VARCHAR(128) DEFAULT '',
  CONSTRAINT pk_config_info_aggr PRIMARY KEY (id),
  CONSTRAINT uk_configinfoaggr_datagrouptenantdatum UNIQUE (data_id, group_id, tenant_id, datum_id)
);

COMMENT ON TABLE config_info_aggr IS 'config_info_aggr';
COMMENT ON COLUMN config_info_aggr.id IS 'id';
COMMENT ON COLUMN config_info_aggr.data_id IS 'data_id';
COMMENT ON COLUMN config_info_aggr.group_id IS 'group_id';
COMMENT ON COLUMN config_info_aggr.datum_id IS 'datum_id';
COMMENT ON COLUMN config_info_aggr.content IS '内容';
COMMENT ON COLUMN config_info_aggr.gmt_modified IS '修改时间';
COMMENT ON COLUMN config_info_aggr.tenant_id IS '租户字段';

CREATE TABLE config_info_beta (
  id BIGINT NOT NULL IDENTITY,
  data_id VARCHAR(255) NOT NULL,
  group_id VARCHAR(128) NOT NULL,
  app_name VARCHAR(128),
  content CLOB NOT NULL,
  beta_ips VARCHAR(1024),
  md5 VARCHAR(32),
  gmt_create TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  gmt_modified TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  src_user CLOB,
  src_ip VARCHAR(20),
  tenant_id VARCHAR(128) DEFAULT '',
  encrypted_data_key CLOB NOT NULL,
  CONSTRAINT pk_config_info_beta PRIMARY KEY (id),
  CONSTRAINT uk_configinfobeta_datagrouptenant UNIQUE (data_id, group_id, tenant_id)
);

COMMENT ON TABLE config_info_beta IS 'config_info_beta';
COMMENT ON COLUMN config_info_beta.id IS 'id';
COMMENT ON COLUMN config_info_beta.data_id IS 'data_id';
COMMENT ON COLUMN config_info_beta.group_id IS 'group_id';
COMMENT ON COLUMN config_info_beta.app_name IS 'app_name';
COMMENT ON COLUMN config_info_beta.content IS 'content';
COMMENT ON COLUMN config_info_beta.beta_ips IS 'betaIps';
COMMENT ON COLUMN config_info_beta.md5 IS 'md5';
COMMENT ON COLUMN config_info_beta.gmt_create IS '创建时间';
COMMENT ON COLUMN config_info_beta.gmt_modified IS '修改时间';
COMMENT ON COLUMN config_info_beta.src_user IS 'source user';
COMMENT ON COLUMN config_info_beta.src_ip IS 'source ip';
COMMENT ON COLUMN config_info_beta.tenant_id IS '租户字段';
COMMENT ON COLUMN config_info_beta.encrypted_data_key IS '秘钥';

CREATE TABLE config_info_tag (
  id BIGINT NOT NULL IDENTITY,
  data_id VARCHAR(255) NOT NULL,
  group_id VARCHAR(128) NOT NULL,
  tenant_id VARCHAR(128) DEFAULT '',
  tag_id VARCHAR(128) NOT NULL,
  app_name VARCHAR(128),
  content CLOB NOT NULL,
  md5 VARCHAR(32),
  gmt_create TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  gmt_modified TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  src_user CLOB,
  src_ip VARCHAR(20),
  CONSTRAINT pk_config_info_tag PRIMARY KEY (id),
  CONSTRAINT uk_configinfotag_datagrouptenanttag UNIQUE (data_id, group_id, tenant_id, tag_id)
);

COMMENT ON TABLE config_info_tag IS 'config_info_tag';
COMMENT ON COLUMN config_info_tag.id IS 'id';
COMMENT ON COLUMN config_info_tag.data_id IS 'data_id';
COMMENT ON COLUMN config_info_tag.group_id IS 'group_id';
COMMENT ON COLUMN config_info_tag.tenant_id IS 'tenant_id';
COMMENT ON COLUMN config_info_tag.tag_id IS 'tag_id';
COMMENT ON COLUMN config_info_tag.app_name IS 'app_name';
COMMENT ON COLUMN config_info_tag.content IS 'content';
COMMENT ON COLUMN config_info_tag.md5 IS 'md5';
COMMENT ON COLUMN config_info_tag.gmt_create IS '创建时间';
COMMENT ON COLUMN config_info_tag.gmt_modified IS '修改时间';
COMMENT ON COLUMN config_info_tag.src_user IS 'source user';
COMMENT ON COLUMN config_info_tag.src_ip IS 'source ip';

CREATE TABLE config_tags_relation (
  id BIGINT NOT NULL,
  tag_name VARCHAR(128) NOT NULL,
  tag_type VARCHAR(64),
  data_id VARCHAR(255) NOT NULL,
  group_id VARCHAR(128) NOT NULL,
  tenant_id VARCHAR(128) DEFAULT '',
  nid BIGINT NOT NULL IDENTITY,
  CONSTRAINT pk_config_tags_relation PRIMARY KEY (nid),
  CONSTRAINT uk_configtagrelation_configidtag UNIQUE (id, tag_name, tag_type)
);

COMMENT ON TABLE config_tags_relation IS 'config_tag_relation';
COMMENT ON COLUMN config_tags_relation.id IS 'id';
COMMENT ON COLUMN config_tags_relation.tag_name IS 'tag_name';
COMMENT ON COLUMN config_tags_relation.tag_type IS 'tag_type';
COMMENT ON COLUMN config_tags_relation.data_id IS 'data_id';
COMMENT ON COLUMN config_tags_relation.group_id IS 'group_id';
COMMENT ON COLUMN config_tags_relation.tenant_id IS 'tenant_id';

CREATE INDEX idx_tenant_id_config_tags_relation ON config_tags_relation(tenant_id);

CREATE TABLE group_capacity (
  id BIGINT NOT NULL IDENTITY,
  group_id VARCHAR(128) NOT NULL DEFAULT '',
  quota INT NOT NULL DEFAULT 0,
  usage INT NOT NULL DEFAULT 0,
  max_size INT NOT NULL DEFAULT 0,
  max_aggr_count INT NOT NULL DEFAULT 0,
  max_aggr_size INT NOT NULL DEFAULT 0,
  max_history_count INT NOT NULL DEFAULT 0,
  gmt_create TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  gmt_modified TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  CONSTRAINT pk_group_capacity PRIMARY KEY (id),
  CONSTRAINT uk_group_id UNIQUE (group_id)
);

COMMENT ON TABLE group_capacity IS '集群、各Group容量信息表';
COMMENT ON COLUMN group_capacity.id IS '主键ID';
COMMENT ON COLUMN group_capacity.group_id IS 'Group ID，空字符表示整个集群';
COMMENT ON COLUMN group_capacity.quota IS '配额，0表示使用默认值';
COMMENT ON COLUMN group_capacity.usage IS '使用量';
COMMENT ON COLUMN group_capacity.max_size IS '单个配置大小上限，单位为字节，0表示使用默认值';
COMMENT ON COLUMN group_capacity.max_aggr_count IS '聚合子配置最大个数，，0表示使用默认值';
COMMENT ON COLUMN group_capacity.max_aggr_size IS '单个聚合数据的子配置大小上限，单位为字节，0表示使用默认值';
COMMENT ON COLUMN group_capacity.max_history_count IS '最大变更历史数量';
COMMENT ON COLUMN group_capacity.gmt_create IS '创建时间';
COMMENT ON COLUMN group_capacity.gmt_modified IS '修改时间';

CREATE TABLE his_config_info (
  id BIGINT NOT NULL,
  nid BIGINT NOT NULL IDENTITY,
  data_id VARCHAR(255) NOT NULL,
  group_id VARCHAR(128) NOT NULL,
  app_name VARCHAR(128),
  content CLOB NOT NULL,
  md5 VARCHAR(32),
  gmt_create TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  gmt_modified TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  src_user CLOB,
  src_ip VARCHAR(20),
  op_type CHAR(10),
  tenant_id VARCHAR(128) DEFAULT '',
  encrypted_data_key CLOB NOT NULL,
  CONSTRAINT pk_his_config_info PRIMARY KEY (nid)
);

CREATE INDEX idx_gmt_create_his_config_info ON his_config_info(gmt_create);
CREATE INDEX idx_gmt_modified_his_config_info ON his_config_info(gmt_modified);
CREATE INDEX idx_did_his_config_info ON his_config_info(data_id);

COMMENT ON TABLE his_config_info IS '多租户改造';
COMMENT ON COLUMN his_config_info.id IS 'id';
COMMENT ON COLUMN his_config_info.nid IS 'nid';
COMMENT ON COLUMN his_config_info.data_id IS 'data_id';
COMMENT ON COLUMN his_config_info.group_id IS 'group_id';
COMMENT ON COLUMN his_config_info.app_name IS 'app_name';
COMMENT ON COLUMN his_config_info.content IS 'content';
COMMENT ON COLUMN his_config_info.md5 IS 'md5';
COMMENT ON COLUMN his_config_info.gmt_create IS 'gmt_create';
COMMENT ON COLUMN his_config_info.gmt_modified IS 'gmt_modified';
COMMENT ON COLUMN his_config_info.src_user IS 'src_user';
COMMENT ON COLUMN his_config_info.src_ip IS 'src_ip';
COMMENT ON COLUMN his_config_info.op_type IS 'op_type';
COMMENT ON COLUMN his_config_info.tenant_id IS '租户字段';
COMMENT ON COLUMN his_config_info.encrypted_data_key IS '秘钥';

CREATE TABLE tenant_capacity (
  id BIGINT NOT NULL IDENTITY,
  tenant_id VARCHAR(128) NOT NULL DEFAULT '',
  quota INT NOT NULL DEFAULT 0,
  usage INT NOT NULL DEFAULT 0,
  max_size INT NOT NULL DEFAULT 0,
  max_aggr_count INT NOT NULL DEFAULT 0,
  max_aggr_size INT NOT NULL DEFAULT 0,
  max_history_count INT NOT NULL DEFAULT 0,
  gmt_create TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  gmt_modified TIMESTAMP NOT NULL DEFAULT TIMESTAMP '2010-05-05 00:00:00',
  CONSTRAINT pk_tenant_capacity PRIMARY KEY (id),
  CONSTRAINT uk_tenant_id UNIQUE (tenant_id)
);

COMMENT ON TABLE tenant_capacity IS '租户容量信息表';
COMMENT ON COLUMN tenant_capacity.id IS '主键ID';
COMMENT ON COLUMN tenant_capacity.tenant_id IS 'Tenant ID';
COMMENT ON COLUMN tenant_capacity.quota IS '配额，0表示使用默认值';
COMMENT ON COLUMN tenant_capacity.usage IS '使用量';
COMMENT ON COLUMN tenant_capacity.max_size IS '单个配置大小上限，单位为字节，0表示使用默认值';
COMMENT ON COLUMN tenant_capacity.max_aggr_count IS '聚合子配置最大个数';
COMMENT ON COLUMN tenant_capacity.max_aggr_size IS '单个聚合数据的子配置大小上限，单位为字节，0表示使用默认值';
COMMENT ON COLUMN tenant_capacity.max_history_count IS '最大变更历史数量';
COMMENT ON COLUMN tenant_capacity.gmt_create IS '创建时间';
COMMENT ON COLUMN tenant_capacity.gmt_modified IS '修改时间';

CREATE TABLE tenant_info (
  id BIGINT NOT NULL IDENTITY,
  kp VARCHAR(128) NOT NULL,
  tenant_id VARCHAR(128) DEFAULT '',
  tenant_name VARCHAR(128) DEFAULT '',
  tenant_desc VARCHAR(256),
  create_source VARCHAR(32),
  gmt_create BIGINT NOT NULL,
  gmt_modified BIGINT NOT NULL,
  CONSTRAINT pk_tenant_info PRIMARY KEY (id),
  CONSTRAINT uk_tenant_info_kptenantid UNIQUE (kp, tenant_id)
);

CREATE INDEX idx_tenant_id_tenant_info ON tenant_info(tenant_id);

COMMENT ON TABLE tenant_info IS 'tenant_info';
COMMENT ON COLUMN tenant_info.id IS 'id';
COMMENT ON COLUMN tenant_info.kp IS 'kp';
COMMENT ON COLUMN tenant_info.tenant_id IS 'tenant_id';
COMMENT ON COLUMN tenant_info.tenant_name IS 'tenant_name';
COMMENT ON COLUMN tenant_info.tenant_desc IS 'tenant_desc';
COMMENT ON COLUMN tenant_info.create_source IS 'create_source';
COMMENT ON COLUMN tenant_info.gmt_create IS '创建时间';
COMMENT ON COLUMN tenant_info.gmt_modified IS '修改时间';

CREATE TABLE users (
  username VARCHAR(50) NOT NULL PRIMARY KEY,
  password VARCHAR(500) NOT NULL,
  enabled BIT NOT NULL
);

CREATE TABLE roles (
  username VARCHAR(50) NOT NULL,
  role VARCHAR(50) NOT NULL,
  CONSTRAINT uk_username_role UNIQUE (username, role)
);

CREATE TABLE permissions (
  role VARCHAR(50) NOT NULL,
  resource VARCHAR(512) NOT NULL,
  action VARCHAR(8) NOT NULL,
  CONSTRAINT uk_role_permission UNIQUE (role, resource, action)
);

INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', 1);

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