/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

-- Create user nacos (Oracle schema is created through user creation)
-- CREATE USER nacos IDENTIFIED BY nacos;
-- GRANT CONNECT, RESOURCE TO nacos;

CREATE TABLE config_info (
    id NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,
    data_id VARCHAR2(255) NOT NULL,
    group_id VARCHAR2(128) NOT NULL,
    tenant_id VARCHAR2(128) DEFAULT '',
    app_name VARCHAR2(128),
    content CLOB,
    md5 VARCHAR2(32) DEFAULT NULL,
    gmt_create TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    gmt_modified TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    src_user VARCHAR2(128) DEFAULT NULL,
    src_ip VARCHAR2(50) DEFAULT NULL,
    c_desc VARCHAR2(256) DEFAULT NULL,
    c_use VARCHAR2(64) DEFAULT NULL,
    effect VARCHAR2(64) DEFAULT NULL,
    type VARCHAR2(64) DEFAULT NULL,
    c_schema CLOB DEFAULT NULL,
    encrypted_data_key VARCHAR2(1024) DEFAULT NULL,
    CONSTRAINT configinfo_id_key PRIMARY KEY (id),
    CONSTRAINT uk_configinfo_datagrouptenant UNIQUE (data_id, group_id, tenant_id)
);

CREATE INDEX configinfo_dataid_key_idx ON config_info(data_id);
CREATE INDEX configinfo_groupid_key_idx ON config_info(group_id);
CREATE INDEX configinfo_dataid_group_key_idx ON config_info(data_id, group_id);

CREATE TABLE his_config_info (
    id NUMBER(20) NOT NULL,
    nid NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,
    data_id VARCHAR2(255) NOT NULL,
    group_id VARCHAR2(128) NOT NULL,
    tenant_id VARCHAR2(128) DEFAULT '',
    app_name VARCHAR2(128),
    content CLOB,
    md5 VARCHAR2(32) DEFAULT NULL,
    gmt_create TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    gmt_modified TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    src_user VARCHAR2(128),
    src_ip VARCHAR2(50) DEFAULT NULL,
    publish_type VARCHAR2(50) DEFAULT 'formal',
    gray_name VARCHAR2(128) DEFAULT NULL,
    ext_info CLOB,
    op_type CHAR(10) DEFAULT NULL,
    encrypted_data_key VARCHAR2(1024) DEFAULT NULL,
    CONSTRAINT hisconfiginfo_nid_key PRIMARY KEY (nid)
);

CREATE INDEX hisconfiginfo_dataid_key_idx ON his_config_info(data_id);
CREATE INDEX hisconfiginfo_gmt_create_idx ON his_config_info(gmt_create);
CREATE INDEX hisconfiginfo_gmt_modified_idx ON his_config_info(gmt_modified);

CREATE TABLE config_info_gray (
    id NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,
    data_id VARCHAR2(255) NOT NULL,
    group_id VARCHAR2(128) NOT NULL,
    tenant_id VARCHAR2(128) DEFAULT '',
    gray_name VARCHAR2(128) NOT NULL,
    gray_rule CLOB,
    app_name VARCHAR2(128),
    src_ip VARCHAR2(128),
    src_user VARCHAR2(128) DEFAULT '',
    content CLOB,
    md5 VARCHAR2(32) DEFAULT NULL,
    encrypted_data_key VARCHAR2(256) DEFAULT NULL,
    gmt_create TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    gmt_modified TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    CONSTRAINT configinfogray_id_key PRIMARY KEY (id),
    CONSTRAINT uk_configinfogray_datagrouptenantgrayname UNIQUE (data_id, group_id, tenant_id, gray_name)
);

CREATE INDEX config_info_gray_dataid_gmt_modified ON config_info_gray(data_id, gmt_modified);
CREATE INDEX config_info_gray_gmt_modified ON config_info_gray(gmt_modified);

CREATE TABLE config_tags_relation (
    id NUMBER(19) NOT NULL,
    tag_name VARCHAR2(128) NOT NULL,
    tag_type VARCHAR2(64) DEFAULT NULL,
    data_id VARCHAR2(255) NOT NULL,
    group_id VARCHAR2(128) NOT NULL,
    tenant_id VARCHAR2(128) DEFAULT '',
    nid NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,
    CONSTRAINT config_tags_id_key PRIMARY KEY (nid),
    CONSTRAINT uk_configtagrelation_configidtag UNIQUE (id, tag_name, tag_type)
);

CREATE INDEX config_tags_tenant_id_idx ON config_tags_relation(tenant_id);

CREATE TABLE group_capacity (
    id NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,
    group_id VARCHAR2(128) DEFAULT '',
    quota NUMBER(10) DEFAULT 0,
    usage NUMBER(10) DEFAULT 0,
    max_size NUMBER(10) DEFAULT 0,
    max_aggr_count NUMBER(10) DEFAULT 0,
    max_aggr_size NUMBER(10) DEFAULT 0,
    max_history_count NUMBER(10) DEFAULT 0,
    gmt_create TIMESTAMP DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    gmt_modified TIMESTAMP DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    CONSTRAINT group_capacity_id_key PRIMARY KEY (id),
    CONSTRAINT uk_group_id UNIQUE (group_id)
);

CREATE TABLE tenant_capacity (
    id NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,
    tenant_id VARCHAR2(128) DEFAULT '',
    quota NUMBER(10) DEFAULT 0,
    usage NUMBER(10) DEFAULT 0,
    max_size NUMBER(10) DEFAULT 0,
    max_aggr_count NUMBER(10) DEFAULT 0,
    max_aggr_size NUMBER(10) DEFAULT 0,
    max_history_count NUMBER(10) DEFAULT 0,
    gmt_create TIMESTAMP DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    gmt_modified TIMESTAMP DEFAULT TO_TIMESTAMP('2010-05-05 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
    CONSTRAINT tenant_capacity_id_key PRIMARY KEY (id),
    CONSTRAINT uk_tenant_id UNIQUE (tenant_id)
);

CREATE TABLE tenant_info (
    id NUMBER(20) GENERATED BY DEFAULT AS IDENTITY,
    kp VARCHAR2(128) NOT NULL,
    tenant_id VARCHAR2(128) DEFAULT '',
    tenant_name VARCHAR2(128) DEFAULT '',
    tenant_desc VARCHAR2(256) DEFAULT NULL,
    create_source VARCHAR2(32) DEFAULT NULL,
    gmt_create NUMBER(20) NOT NULL,
    gmt_modified NUMBER(20) NOT NULL,
    CONSTRAINT tenant_info_id_key PRIMARY KEY (id),
    CONSTRAINT uk_tenant_info_kptenantid UNIQUE (kp, tenant_id)
);

CREATE INDEX tenant_info_tenant_id_idx ON tenant_info(tenant_id);

CREATE TABLE users (
    username VARCHAR2(50) NOT NULL PRIMARY KEY,
    password VARCHAR2(500) NOT NULL,
    enabled NUMBER(1) NOT NULL DEFAULT 1
);

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

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


/******************************************/
/*   ipv6 support   */
/******************************************/

-- Note: In Oracle, ALTER TABLE ADD for existing columns should be handled carefully
-- The following statements are kept for compatibility but may need adjustment if columns already exist

-- ALTER TABLE his_config_info ADD src_ip VARCHAR2(50) DEFAULT NULL;

-- ALTER TABLE config_info ADD src_ip VARCHAR2(50) DEFAULT NULL;


/******************************************/
/*   support gray history since 2.5.0   */
/******************************************/

-- Note: These columns are already included in the CREATE TABLE statement above
-- The following ALTER statements are kept for reference when upgrading from older versions

-- ALTER TABLE his_config_info ADD publish_type VARCHAR2(50) DEFAULT 'formal';
-- ALTER TABLE his_config_info ADD ext_info CLOB DEFAULT NULL;
-- ALTER TABLE his_config_info ADD gray_name VARCHAR2(128) DEFAULT NULL;


/******************************************/
/*   表名称 = ai_resource                 */
/******************************************/
CREATE TABLE ai_resource (
    id NUMBER(20) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    gmt_create TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
    gmt_modified TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
    name VARCHAR2(256) NOT NULL,
    type VARCHAR2(32) NOT NULL,
    c_desc VARCHAR2(1024) DEFAULT NULL,
    status VARCHAR2(32) DEFAULT NULL,
    namespace_id VARCHAR2(128) DEFAULT '' NOT NULL,
    biz_tags VARCHAR2(1024) DEFAULT NULL,
    ext CLOB DEFAULT NULL,
    c_from VARCHAR2(256) DEFAULT 'local' NOT NULL,
    version_info CLOB DEFAULT NULL,
    meta_version NUMBER(20) DEFAULT 1 NOT NULL,
    scope VARCHAR2(16) DEFAULT 'PRIVATE' NOT NULL,
    owner VARCHAR2(128) DEFAULT '' NOT NULL,
    download_count NUMBER(20) DEFAULT 0 NOT NULL,
    CONSTRAINT uk_ai_resource_ns_name_type UNIQUE (namespace_id, name, type, c_from)
);

CREATE INDEX idx_ai_resource_name ON ai_resource(name);
CREATE INDEX idx_ai_resource_type ON ai_resource(type);
CREATE INDEX idx_ai_resource_gmt_modified ON ai_resource(gmt_modified);

/******************************************/
/*   表名称 = ai_resource_version         */
/******************************************/
CREATE TABLE ai_resource_version (
    id NUMBER(20) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    gmt_create TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
    gmt_modified TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
    type VARCHAR2(32) NOT NULL,
    author VARCHAR2(128) DEFAULT NULL,
    name VARCHAR2(256) NOT NULL,
    c_desc VARCHAR2(1024) DEFAULT NULL,
    status VARCHAR2(32) NOT NULL,
    version VARCHAR2(64) NOT NULL,
    namespace_id VARCHAR2(128) DEFAULT '' NOT NULL,
    storage CLOB DEFAULT NULL,
    publish_pipeline_info CLOB DEFAULT NULL,
    download_count NUMBER(20) DEFAULT 0 NOT NULL,
    CONSTRAINT uk_ai_res_ver_ns_nm_tp_ver UNIQUE (namespace_id, name, type, version)
);

CREATE INDEX idx_ai_res_ver_name ON ai_resource_version(name);
CREATE INDEX idx_ai_res_ver_status ON ai_resource_version(status);
CREATE INDEX idx_ai_res_ver_gmt_mod ON ai_resource_version(gmt_modified);
