-- ----------------------------
-- 表名：users
-- ----------------------------
CREATE TABLE users (
	"USERNAME" NVARCHAR2(50) NOT NULL PRIMARY KEY,
	"PASSWORD" NVARCHAR2(500) NOT NULL,
	"ENABLED" NVARCHAR2(15) NOT NULL
);
COMMENT ON TABLE users IS '';
COMMENT ON COLUMN users."USERNAME" IS '';
COMMENT ON COLUMN users."PASSWORD" IS '';
COMMENT ON COLUMN users."ENABLED" IS '';
DELETE FROM users;
INSERT INTO users ("PASSWORD","ENABLED","USERNAME") VALUES ('$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu','true','nacos');
-- ----------------------------
-- 表名：tenant_info
-- ----------------------------
CREATE TABLE tenant_info (
	"ID" NUMBER PRIMARY KEY,
	"KP" NVARCHAR2(128) NOT NULL,
	"TENANT_ID" NVARCHAR2(128) DEFAULT '',
	"TENANT_NAME" NVARCHAR2(128) DEFAULT '',
	"TENANT_DESC" NVARCHAR2(256),
	"CREATE_SOURCE" NVARCHAR2(32),
	"GMT_CREATE" NUMBER NOT NULL,
	"GMT_MODIFIED" NUMBER NOT NULL
);
CREATE SEQUENCE tenant_info_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER tenant_info_tg
BEFORE INSERT ON tenant_info
FOR EACH ROW
BEGIN
	SELECT tenant_info_sq.NEXTVAL INTO :new.id FROM dual;
END;
/
;
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 '修改时间';


-- ----------------------------
-- Records of TENANT_INFO
-- ----------------------------
INSERT INTO "TENANT_INFO" VALUES ('1', '1', '69c4eecb-05bd-4041-81fe-1473f95f578c', 'develop', '开发环境', 'nacos', '1683364156784', '1683364156784');
INSERT INTO "TENANT_INFO" VALUES ('2', '1', '1e017954-eb52-4d21-a843-0286d9013cf3', 'staging', '测试环境', 'nacos', '1683364163967', '1683364163967');
INSERT INTO "TENANT_INFO" VALUES ('3', '1', 'f50f4948-3ca2-48af-8bb1-1a47950b503d', 'pre', '预发环境', 'nacos', '1683364172231', '1683364172231');
INSERT INTO "TENANT_INFO" VALUES ('5', '1', '3baec428-9669-486c-b359-a76f7a1f1ac7', 'production', '生产环境', 'nacos', '1683364190662', '1683364190662');

-- ----------------------------
-- 表名：tenant_capacity
-- ----------------------------
CREATE TABLE tenant_capacity (
	"ID" NUMBER PRIMARY KEY,
	"TENANT_ID" NVARCHAR2(128) DEFAULT '',
	"QUOTA" NUMBER DEFAULT '0',
	"USAGE" NUMBER DEFAULT '0',
	"MAX_SIZE" NUMBER DEFAULT '0',
	"MAX_AGGR_COUNT" NUMBER DEFAULT '0',
	"MAX_AGGR_SIZE" NUMBER DEFAULT '0',
	"MAX_HISTORY_COUNT" NUMBER DEFAULT '0',
	"GMT_CREATE" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"GMT_MODIFIED" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
);
CREATE SEQUENCE tenant_capacity_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER tenant_capacity_tg
BEFORE INSERT ON tenant_capacity
FOR EACH ROW
BEGIN
	SELECT tenant_capacity_sq.NEXTVAL INTO :new.id FROM dual;
END;
/
;
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 '修改时间';
-- ----------------------------
-- 表名：roles
-- ----------------------------
CREATE TABLE roles (
	"USERNAME" NVARCHAR2(50) NOT NULL,
	"ROLE" NVARCHAR2(50) NOT NULL
);
COMMENT ON TABLE roles IS '';
COMMENT ON COLUMN roles."USERNAME" IS '';
COMMENT ON COLUMN roles."ROLE" IS '';
DELETE FROM roles;
INSERT INTO roles ("ROLE","USERNAME") VALUES ('ROLE_ADMIN','nacos');
-- ----------------------------
-- 表名：permissions
-- ----------------------------
CREATE TABLE permissions (
	"ROLE" NVARCHAR2(50) NOT NULL,
	"RESOURCE" NVARCHAR2(512) NOT NULL,
	"ACTION" NVARCHAR2(8) NOT NULL
);
COMMENT ON TABLE permissions IS '';
COMMENT ON COLUMN permissions."ROLE" IS '';
COMMENT ON COLUMN permissions."RESOURCE" IS '';
COMMENT ON COLUMN permissions."ACTION" IS '';
-- ----------------------------
-- 表名：his_config_info
-- ----------------------------
CREATE TABLE his_config_info (
	"ID" NUMBER NOT NULL,
	"NID" NUMBER PRIMARY KEY,
	"DATA_ID" NVARCHAR2(255) NOT NULL,
	"GROUP_ID" NVARCHAR2(128) NOT NULL,
	"APP_NAME" NVARCHAR2(128),
	"CONTENT" NCLOB NOT NULL,
	"MD5" NVARCHAR2(32),
	"GMT_CREATE" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"GMT_MODIFIED" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"SRC_USER" NCLOB,
	"SRC_IP" NVARCHAR2(20),
	"OP_TYPE" CHAR(10),
	"TENANT_ID" NVARCHAR2(128) DEFAULT '',
	"ENCRYPTED_DATA_KEY" NCLOB
);
CREATE SEQUENCE his_config_info_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER his_config_info_tg
BEFORE INSERT ON his_config_info
FOR EACH ROW
BEGIN
	SELECT his_config_info_sq.NEXTVAL INTO :new.nid FROM dual;
END;
/
;
COMMENT ON TABLE his_config_info IS '多租户改造';
COMMENT ON COLUMN his_config_info."ID" IS '';
COMMENT ON COLUMN his_config_info."NID" IS '';
COMMENT ON COLUMN his_config_info."DATA_ID" IS '';
COMMENT ON COLUMN his_config_info."GROUP_ID" IS '';
COMMENT ON COLUMN his_config_info."APP_NAME" IS 'app_name';
COMMENT ON COLUMN his_config_info."CONTENT" IS '';
COMMENT ON COLUMN his_config_info."MD5" IS '';
COMMENT ON COLUMN his_config_info."GMT_CREATE" IS '';
COMMENT ON COLUMN his_config_info."GMT_MODIFIED" IS '';
COMMENT ON COLUMN his_config_info."SRC_USER" IS '';
COMMENT ON COLUMN his_config_info."SRC_IP" IS '';
COMMENT ON COLUMN his_config_info."OP_TYPE" IS '';
COMMENT ON COLUMN his_config_info."TENANT_ID" IS '租户字段';
COMMENT ON COLUMN his_config_info."ENCRYPTED_DATA_KEY" IS '秘钥';
-- ----------------------------
-- 表名：group_capacity
-- ----------------------------
CREATE TABLE group_capacity (
	"ID" NUMBER PRIMARY KEY,
	"GROUP_ID" NVARCHAR2(128) DEFAULT '',
	"QUOTA" NUMBER DEFAULT '0',
	"USAGE" NUMBER DEFAULT '0',
	"MAX_SIZE" NUMBER DEFAULT '0',
	"MAX_AGGR_COUNT" NUMBER DEFAULT '0',
	"MAX_AGGR_SIZE" NUMBER DEFAULT '0',
	"MAX_HISTORY_COUNT" NUMBER DEFAULT '0',
	"GMT_CREATE" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"GMT_MODIFIED" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
);
CREATE SEQUENCE group_capacity_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER group_capacity_tg
BEFORE INSERT ON group_capacity
FOR EACH ROW
BEGIN
	SELECT group_capacity_sq.NEXTVAL INTO :new.id FROM dual;
END;
/
;
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 '修改时间';
-- ----------------------------
-- 表名：config_tags_relation
-- ----------------------------
CREATE TABLE config_tags_relation (
	"ID" NUMBER NOT NULL,
	"TAG_NAME" NVARCHAR2(128) NOT NULL,
	"TAG_TYPE" NVARCHAR2(64),
	"DATA_ID" NVARCHAR2(255) NOT NULL,
	"GROUP_ID" NVARCHAR2(128) NOT NULL,
	"TENANT_ID" NVARCHAR2(128) DEFAULT '',
	"NID" NUMBER PRIMARY KEY
);
CREATE SEQUENCE config_tags_relation_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER config_tags_relation_tg
BEFORE INSERT ON config_tags_relation
FOR EACH ROW
BEGIN
	SELECT config_tags_relation_sq.NEXTVAL INTO :new.nid FROM dual;
END;
/
;
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';
COMMENT ON COLUMN config_tags_relation."NID" IS '';
-- ----------------------------
-- 表名：config_info_tag
-- ----------------------------
CREATE TABLE config_info_tag (
	"ID" NUMBER PRIMARY KEY,
	"DATA_ID" NVARCHAR2(255) NOT NULL,
	"GROUP_ID" NVARCHAR2(128) NOT NULL,
	"TENANT_ID" NVARCHAR2(128) DEFAULT '',
	"TAG_ID" NVARCHAR2(128) NOT NULL,
	"APP_NAME" NVARCHAR2(128),
	"CONTENT" NCLOB NOT NULL,
	"MD5" NVARCHAR2(32),
	"GMT_CREATE" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"GMT_MODIFIED" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"SRC_USER" NCLOB,
	"SRC_IP" NVARCHAR2(20)
);
CREATE SEQUENCE config_info_tag_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER config_info_tag_tg
BEFORE INSERT ON config_info_tag
FOR EACH ROW
BEGIN
	SELECT config_info_tag_sq.NEXTVAL INTO :new.id FROM dual;
END;
/
;
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';
-- ----------------------------
-- 表名：config_info_beta
-- ----------------------------
CREATE TABLE config_info_beta (
	"ID" NUMBER PRIMARY KEY,
	"DATA_ID" NVARCHAR2(255) NOT NULL,
	"GROUP_ID" NVARCHAR2(128) NOT NULL,
	"APP_NAME" NVARCHAR2(128),
	"CONTENT" NCLOB NOT NULL,
	"BETA_IPS" NVARCHAR2(1024),
	"MD5" NVARCHAR2(32),
	"GMT_CREATE" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"GMT_MODIFIED" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"SRC_USER" NCLOB,
	"SRC_IP" NVARCHAR2(20),
	"TENANT_ID" NVARCHAR2(128) DEFAULT '',
	"ENCRYPTED_DATA_KEY" NCLOB
);
CREATE SEQUENCE config_info_beta_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER config_info_beta_tg
BEFORE INSERT ON config_info_beta
FOR EACH ROW
BEGIN
	SELECT config_info_beta_sq.NEXTVAL INTO :new.id FROM dual;
END;
/
;
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 '秘钥';
-- ----------------------------
-- 表名：config_info_aggr
-- ----------------------------
CREATE TABLE config_info_aggr (
	"ID" NUMBER PRIMARY KEY,
	"DATA_ID" NVARCHAR2(255) NOT NULL,
	"GROUP_ID" NVARCHAR2(128) NOT NULL,
	"DATUM_ID" NVARCHAR2(255) NOT NULL,
	"CONTENT" NCLOB NOT NULL,
	"GMT_MODIFIED" TIMESTAMP NOT NULL,
	"APP_NAME" NVARCHAR2(128),
	"TENANT_ID" NVARCHAR2(128) DEFAULT ''
);
CREATE SEQUENCE config_info_aggr_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER config_info_aggr_tg
BEFORE INSERT ON config_info_aggr
FOR EACH ROW
BEGIN
	SELECT config_info_aggr_sq.NEXTVAL INTO :new.id FROM dual;
END;
/
;
COMMENT ON TABLE config_info_aggr IS '增加租户字段';
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."APP_NAME" IS '';
COMMENT ON COLUMN config_info_aggr."TENANT_ID" IS '租户字段';
-- ----------------------------
-- 表名：config_info
-- ----------------------------
CREATE TABLE config_info (
	"ID" NUMBER PRIMARY KEY,
	"DATA_ID" NVARCHAR2(255) NOT NULL,
	"GROUP_ID" NVARCHAR2(128),
	"CONTENT" NCLOB NOT NULL,
	"MD5" NVARCHAR2(32),
	"GMT_CREATE" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"GMT_MODIFIED" TIMESTAMP DEFAULT to_date('2010-05-05 00:00:00', 'yyyy-mm-dd hh24:mi:ss'),
	"SRC_USER" NCLOB,
	"SRC_IP" NVARCHAR2(20),
	"APP_NAME" NVARCHAR2(128),
	"TENANT_ID" NVARCHAR2(128) DEFAULT '',
	"C_DESC" NVARCHAR2(256),
	"C_USE" NVARCHAR2(64),
	"EFFECT" NVARCHAR2(64),
	"TYPE" NVARCHAR2(64),
	"C_SCHEMA" NCLOB,
	"ENCRYPTED_DATA_KEY" NCLOB
);
CREATE SEQUENCE config_info_sq
INCREMENT by 1
START WITH 1
NOMAXVALUE
NOCYCLE
NOCACHE;
CREATE OR REPLACE TRIGGER config_info_tg
BEFORE INSERT ON config_info
FOR EACH ROW
BEGIN
	SELECT config_info_sq.NEXTVAL INTO :new.id FROM dual;
END;
/
;
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."GROUP_ID" IS '';
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."APP_NAME" IS '';
COMMENT ON COLUMN config_info."TENANT_ID" IS '租户字段';
COMMENT ON COLUMN config_info."C_DESC" IS '';
COMMENT ON COLUMN config_info."C_USE" IS '';
COMMENT ON COLUMN config_info."EFFECT" IS '';
COMMENT ON COLUMN config_info."TYPE" IS '';
COMMENT ON COLUMN config_info."C_SCHEMA" IS '';
COMMENT ON COLUMN config_info."ENCRYPTED_DATA_KEY" IS '秘钥';


-- ----------------------------
-- Records of CONFIG_INFO
-- ----------------------------

-- 唯一索引
CREATE UNIQUE INDEX uk_configinfo_datagrouptenant ON config_info(data_id,group_id,tenant_id);
CREATE UNIQUE INDEX uk_configinfoaggr_datagroup ON config_info_aggr(data_id,group_id,tenant_id,datum_id);
CREATE UNIQUE INDEX uk_configinfobeta_datagroup ON config_info_beta(data_id,group_id,tenant_id);
CREATE UNIQUE INDEX uk_configinfotag_datagroup ON config_info_tag(data_id,group_id,tenant_id,tag_id);
CREATE UNIQUE INDEX uk_configtagrelation ON config_tags_relation(id,tag_name,tag_type);
CREATE UNIQUE INDEX uk_group_id ON group_capacity(group_id);
CREATE UNIQUE INDEX uk_role_permission ON permissions(role,"RESOURCE",action);
CREATE UNIQUE INDEX uk_username_role ON roles(username,role);
CREATE UNIQUE INDEX uk_tenant_id ON tenant_capacity(tenant_id);
CREATE UNIQUE INDEX uk_tenant_info_kptenantid ON tenant_info(kp,tenant_id);

CREATE TABLE "GLOBAL_TABLE"
(
    "XID"                       VARCHAR2(128) NOT NULL,
    "TRANSACTION_ID"            NUMBER(19),
    "STATUS"                    NUMBER(3)     NOT NULL,
    "APPLICATION_ID"            VARCHAR2(32),
    "TRANSACTION_SERVICE_GROUP" VARCHAR2(32),
    "TRANSACTION_NAME"          VARCHAR2(128),
    "TIMEOUT"                   NUMBER(10),
    "BEGIN_TIME"                NUMBER(19),
    "APPLICATION_DATA"          VARCHAR2(2000),
    "GMT_CREATE"                TIMESTAMP(0),
    "GMT_MODIFIED"              TIMESTAMP(0),
    PRIMARY KEY ("XID")
);

CREATE INDEX idx_status_gmt_modified ON "GLOBAL_TABLE" ("STATUS", "GMT_MODIFIED");
CREATE INDEX idx_transaction_id ON "GLOBAL_TABLE" ("TRANSACTION_ID");

-- the table to store BranchSession data
CREATE TABLE "BRANCH_TABLE"
(
    "BRANCH_ID"         NUMBER(19)    NOT NULL,
    "XID"               VARCHAR2(128) NOT NULL,
    "TRANSACTION_ID"    NUMBER(19),
    "RESOURCE_GROUP_ID" VARCHAR2(32),
    "RESOURCE_ID"       VARCHAR2(256),
    "BRANCH_TYPE"       VARCHAR2(8),
    "STATUS"            NUMBER(3),
    "CLIENT_ID"         VARCHAR2(64),
    "APPLICATION_DATA"  VARCHAR2(2000),
    "GMT_CREATE"        TIMESTAMP(6),
    "GMT_MODIFIED"      TIMESTAMP(6),
    PRIMARY KEY ("BRANCH_ID")
);

CREATE INDEX idx_xid ON "BRANCH_TABLE" ("XID");

-- the table to store lock data
CREATE TABLE "LOCK_TABLE"
(
    "ROW_KEY"        VARCHAR2(128) NOT NULL,
    "XID"            VARCHAR2(128),
    "TRANSACTION_ID" NUMBER(19),
    "BRANCH_ID"      NUMBER(19)    NOT NULL,
    "RESOURCE_ID"    VARCHAR2(256),
    "TABLE_NAME"     VARCHAR2(32),
    "PK"             VARCHAR2(128),
    "STATUS"         NUMBER(3)   DEFAULT 0 NOT NULL,
    "GMT_CREATE"     TIMESTAMP(0),
    "GMT_MODIFIED"   TIMESTAMP(0),
    PRIMARY KEY ("ROW_KEY")
);

comment on column "LOCK_TABLE"."STATUS" is '0:locked ,1:rollbacking';
CREATE INDEX idx_branch_id ON "LOCK_TABLE" ("BRANCH_ID");
CREATE INDEX idx_lock_table_xid ON "LOCK_TABLE" (XID);
CREATE INDEX idx_status ON "LOCK_TABLE" (STATUS);

CREATE TABLE "DISTRIBUTED_LOCK" (
                                  "LOCK_KEY"     VARCHAR2(20)  NOT NULL,
                                  "LOCK_VALUE"        VARCHAR2(20)  NOT NULL,
                                  "EXPIRE"       DECIMAL(18)   NOT NULL,
                                  PRIMARY KEY ("LOCK_KEY")
);

INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES ('AsyncCommitting', ' ', 0);
INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);

CREATE TABLE "VGROUP_TABLE"
(
    "VGROUP"    VARCHAR2(255) PRIMARY KEY,
    "NAMESPACE" VARCHAR2(255),
    "CLUSTER"   VARCHAR2(255)
);
