-- create database
-- drop database if exists bigmeta;
-- create database bigmeta
--    with owner postgres;

create table bigmeta_classification_record
(
    id              bigserial
        constraint bigmeta_classification_record_pk
            primary key,
    column_guid     varchar(255),
    cost            integer,
    records         jsonb,
    gmt_create      timestamp,
    gmt_update      timestamp,
    gmt_delete      timestamp,
    create_operator varchar(255),
    update_operator varchar(255),
    delete_operator varchar(255),
    is_deleted      boolean default false,
    tenant_id       bigint not null,
    data            jsonb,
    result          jsonb
);

alter table bigmeta_classification_record
    owner to postgres;

create unique index bigmeta_classification_record_id_uindex
    on bigmeta_classification_record (id);

create table bigmeta_data_classification_config
(
    id                          bigserial
        constraint bigmeta_data_classification_config_pk
            primary key,
    code                        varchar(255)      not null,
    pre_rule_fetch_data_percent double precision,
    pre_rule_matched_condition  jsonb,
    pre_rule_set                jsonb,
    classification_logic_type   varchar(8),
    classification_rule_set     jsonb,
    algorithm                   jsonb,
    algorithm_matched_condition jsonb,
    version                     bigint  default 1 not null,
    tenant_id                   bigint            not null,
    is_deleted                  boolean default false,
    gmt_create                  timestamp         not null,
    gmt_update                  timestamp         not null,
    gmt_delete                  timestamp,
    create_operator             varchar(255),
    update_operator             varchar(255),
    delete_operator             varchar(255),
    propagation_rule_set        jsonb
);

alter table bigmeta_data_classification_config
    owner to postgres;

create unique index bigmeta_data_classification_config_id_uindex
    on bigmeta_data_classification_config (id);

create unique index bigmeta_data_classification_config_tenant_id_code_uindex
    on bigmeta_data_classification_config (tenant_id, code);

create table bigmeta_relation_graph_check
(
    id               serial
        primary key,
    tenant_id        bigint,
    type_code        varchar(50),
    meta_type        json,
    check_start_time timestamp,
    operate_dml      varchar(20),
    affected_ids     text,
    status           smallint,
    failure_reason   text,
    create_operator  varchar(255),
    update_operator  varchar(255),
    delete_operator  varchar(255),
    gmt_create       timestamp,
    gmt_update       timestamp,
    gmt_delete       timestamp,
    is_deleted       boolean
);

comment
on table bigmeta_relation_graph_check is 'A table of record to check relation and graph data consistency';

comment
on column bigmeta_relation_graph_check.id is 'PRIMARY KEY';

comment
on column bigmeta_relation_graph_check.tenant_id is 'tenant id';

comment
on column bigmeta_relation_graph_check.type_code is 'type code, definition in type system';

comment
on column bigmeta_relation_graph_check.meta_type is 'meta type instance snapshot';

comment
on column bigmeta_relation_graph_check.check_start_time is 'start check time';

comment
on column bigmeta_relation_graph_check.operate_dml is 'operate dml:INSERT,UPDATE,DELETE';

comment
on column bigmeta_relation_graph_check.affected_ids is 'wait check ids';

comment
on column bigmeta_relation_graph_check.status is '0:INIT,1:CHECKING,2:SUCCESS,-1:FAILURE';

comment
on column bigmeta_relation_graph_check.create_operator is 'create operator';

comment
on column bigmeta_relation_graph_check.update_operator is 'update operator';

comment
on column bigmeta_relation_graph_check.delete_operator is 'delete operator';

comment
on column bigmeta_relation_graph_check.gmt_create is 'gmt create';

comment
on column bigmeta_relation_graph_check.gmt_update is 'gmt update';

comment
on column bigmeta_relation_graph_check.gmt_delete is 'gmt delete';

comment
on column bigmeta_relation_graph_check.is_deleted is 'is delete';

alter table bigmeta_relation_graph_check
    owner to postgres;

create index idx_bigmeta_relation_graph_check_status_check_start_time
    on bigmeta_relation_graph_check (status, check_start_time);

create table bigmeta_sample_data
(
    id              bigserial
        primary key,
    tenant_id       bigint,
    column_guid     varchar(255),
    task_type       varchar(100),
    source_type     varchar(100),
    task_name       varchar(100),
    instance_id     varchar(128),
    content_type    varchar(100),
    start_time      timestamp,
    end_time        timestamp,
    content         json,
    column_version  bigint,
    status          smallint,
    create_operator varchar(255),
    update_operator varchar(255),
    delete_operator varchar(255),
    gmt_create      timestamp,
    gmt_update      timestamp,
    gmt_delete      timestamp,
    is_deleted      boolean
);

comment
on table bigmeta_sample_data is 'A table of entity to save sample data';

comment
on column bigmeta_sample_data.id is 'PRIMARY KEY';

comment
on column bigmeta_sample_data.tenant_id is 'tenant id';

comment
on column bigmeta_sample_data.column_guid is 'column guid';

comment
on column bigmeta_sample_data.task_type is 'task type';

comment
on column bigmeta_sample_data.source_type is 'source type';

comment
on column bigmeta_sample_data.task_name is 'task name';

comment
on column bigmeta_sample_data.instance_id is 'instance id';

comment
on column bigmeta_sample_data.content_type is 'content type';

comment
on column bigmeta_sample_data.start_time is 'start time';

comment
on column bigmeta_sample_data.end_time is 'end time';

comment
on column bigmeta_sample_data.content is 'ingest single row content';

comment
on column bigmeta_sample_data.column_version is 'column version';

comment
on column bigmeta_sample_data.status is 'status 1:uncomplete,0:complete';

comment
on column bigmeta_sample_data.create_operator is 'create operator';

comment
on column bigmeta_sample_data.update_operator is 'update operator';

comment
on column bigmeta_sample_data.delete_operator is 'delete operator';

comment
on column bigmeta_sample_data.gmt_create is 'gmt create';

comment
on column bigmeta_sample_data.gmt_update is 'gmt update';

comment
on column bigmeta_sample_data.gmt_delete is 'gmt delete';

comment
on column bigmeta_sample_data.is_deleted is 'is delete';

alter table bigmeta_sample_data
    owner to postgres;

create unique index idx_bigmeta_sample_data_tenant_id_column_guid_column_version
    on bigmeta_sample_data (tenant_id, column_guid, column_version);

create index idx_bigmeta_sample_data_status
    on bigmeta_sample_data (status);

create table bigmeta_arrange_task
(
    id               bigserial
        primary key,
    col_guid         varchar(1024),
    task_type        varchar(63),
    task_status      varchar(63),
    col_data_version varchar(63),
    result           text,
    classify_way     varchar(255),
    error_msg        text,
    gmt_create       timestamp,
    gmt_update       timestamp,
    gmt_delete       timestamp,
    create_operator  varchar(255),
    update_operator  varchar(255),
    delete_operator  varchar(255),
    is_deleted       boolean,
    tenant_id        integer
);

comment
on table bigmeta_arrange_task is 'arrange task';

comment
on column bigmeta_arrange_task.id is 'primary key';

comment
on column bigmeta_arrange_task.col_guid is 'col guid';

comment
on column bigmeta_arrange_task.task_type is 'task type';

comment
on column bigmeta_arrange_task.task_status is 'task status';

comment
on column bigmeta_arrange_task.col_data_version is 'col data version';

comment
on column bigmeta_arrange_task.result is 'task result';

comment
on column bigmeta_arrange_task.classify_way is 'classify way';

comment
on column bigmeta_arrange_task.error_msg is 'error_msg';

comment
on column bigmeta_arrange_task.gmt_create is 'create time';

comment
on column bigmeta_arrange_task.gmt_update is 'gmt update';

comment
on column bigmeta_arrange_task.gmt_delete is 'gmt delete';

comment
on column bigmeta_arrange_task.create_operator is 'operator create';

comment
on column bigmeta_arrange_task.update_operator is 'operator update';

comment
on column bigmeta_arrange_task.delete_operator is 'operator delete';

comment
on column bigmeta_arrange_task.is_deleted is 'is deleted';

comment
on column bigmeta_arrange_task.tenant_id is 'tenant id';

alter table bigmeta_arrange_task
    owner to postgres;

create unique index bigmeta_arrange_task_id_uindex
    on bigmeta_arrange_task (id);

create table bigmeta_dlq
(
    id            bigint,
    message_type  varchar(255),
    message_topic varchar(511),
    retry_times   integer,
    gmt_create    timestamp,
    gmt_update    timestamp,
    tenant_id     integer
);

comment
on column bigmeta_dlq.id is 'primary key';

comment
on column bigmeta_dlq.message_type is 'message type';

comment
on column bigmeta_dlq.message_topic is 'message topic';

comment
on column bigmeta_dlq.retry_times is 'retry times';

comment
on column bigmeta_dlq.gmt_create is 'gmt create';

comment
on column bigmeta_dlq.gmt_update is 'gmt_update';

comment
on column bigmeta_dlq.tenant_id is 'tenant id';

alter table bigmeta_dlq
    owner to postgres;

create table bigmeta_dlq_history
(
    id            bigserial,
    message_type  varchar(255),
    message_topic varchar(255),
    retry_times   integer,
    result        text,
    gmt_create    timestamp,
    tenant_id     integer
);

comment
on table bigmeta_dlq_history is 'bigmeta_dlq_history';

comment
on column bigmeta_dlq_history.id is 'primary key';

comment
on column bigmeta_dlq_history.message_type is 'message type';

comment
on column bigmeta_dlq_history.message_topic is 'message topic';

comment
on column bigmeta_dlq_history.retry_times is 'retry times';

comment
on column bigmeta_dlq_history.result is 'result';

comment
on column bigmeta_dlq_history.gmt_create is 'gmt create time';

comment
on column bigmeta_dlq_history.tenant_id is 'tenant id';

alter table bigmeta_dlq_history
    owner to postgres;

create table bigmeta_entity_tags
(
    id                  bigserial
        primary key,
    tenant_id           bigint,
    guid                varchar(255),
    type_code           varchar(255),
    classification_code varchar(255),
    tier_code           varchar(255),
    source_guid         varchar(255),
    source_tag_type     smallint,
    parent_guid         varchar(255),
    child_guid          varchar(255),
    tag_type            smallint,
    tag_time            timestamp,
    is_revised          boolean,
    revise_comment      varchar(255),
    meta_version        bigint,
    create_operator     varchar(255),
    update_operator     varchar(255),
    delete_operator     varchar(255),
    gmt_create          timestamp,
    gmt_update          timestamp,
    gmt_delete          timestamp,
    is_deleted          boolean
);

comment
on table bigmeta_entity_tags is 'A table of entity to save entity tags';

comment
on column bigmeta_entity_tags.id is 'PRIMARY KEY';

comment
on column bigmeta_entity_tags.tenant_id is 'tenant id';

comment
on column bigmeta_entity_tags.guid is 'column or table guid, decide by type code';

comment
on column bigmeta_entity_tags.type_code is 'type code';

comment
on column bigmeta_entity_tags.classification_code is 'classification code';

comment
on column bigmeta_entity_tags.tier_code is 'tier code';

comment
on column bigmeta_entity_tags.source_guid is 'start spread point guid';

comment
on column bigmeta_entity_tags.source_tag_type is 'start spread point type';

comment
on column bigmeta_entity_tags.parent_guid is 'parent spread point guid';

comment
on column bigmeta_entity_tags.child_guid is 'child spread point guid';

comment
on column bigmeta_entity_tags.tag_type is 'tag type, (0：manual，1: recognize，2: spread)';

comment
on column bigmeta_entity_tags.tag_time is 'do tag time';

comment
on column bigmeta_entity_tags.is_revised is 'is revised';

comment
on column bigmeta_entity_tags.revise_comment is 'revise comment';

comment
on column bigmeta_entity_tags.meta_version is 'table or column meta version';

comment
on column bigmeta_entity_tags.create_operator is 'create operator';

comment
on column bigmeta_entity_tags.update_operator is 'update operator';

comment
on column bigmeta_entity_tags.delete_operator is 'delete operator';

comment
on column bigmeta_entity_tags.gmt_create is 'gmt create';

comment
on column bigmeta_entity_tags.gmt_update is 'gmt update';

comment
on column bigmeta_entity_tags.gmt_delete is 'gmt delete';

comment
on column bigmeta_entity_tags.is_deleted is 'is delete';

alter table bigmeta_entity_tags
    owner to postgres;

create index idx_bigmeta_entity_tags_tenant_id_guid
    on bigmeta_entity_tags (tenant_id, guid);

create table bigmeta_data_tier
(
    id              bigint not null
        constraint bigmeta_data_level_pk
            primary key,
    tenant_id       bigint,
    group_name      varchar,
    level           integer,
    type            varchar,
    param           json,
    description     text,
    display_name    varchar,
    create_operator varchar(255),
    update_operator varchar(255),
    delete_operator varchar(255),
    gmt_create      timestamp,
    gmt_update      timestamp,
    gmt_delete      timestamp,
    is_deleted      boolean
);

comment
on column bigmeta_data_tier.id is '主键id';

comment
on column bigmeta_data_tier.tenant_id is '租户id';

comment
on column bigmeta_data_tier.group_name is '分组名';

comment
on column bigmeta_data_tier.level is '分级级别';

comment
on column bigmeta_data_tier.type is '类型，组/级';

comment
on column bigmeta_data_tier.param is '额外参数';

comment
on column bigmeta_data_tier.description is '描述';

comment
on column bigmeta_data_tier.display_name is '显示名，如C1';

alter table bigmeta_data_tier
    owner to postgres;

create table bigmeta_pipeline_execution
(
    id               bigserial
        constraint bigmeta_pipeline_execution_pk
            primary key,
    pipeline_name    varchar(512) not null,
    pipeline_type    varchar(32)  not null,
    start_time       timestamp,
    end_time         timestamp,
    dag_run_id       varchar(250) not null,
    external_trigger boolean,
    state            varchar(32)  not null,
    remarks          varchar(512) NULL,
    gmt_create       timestamp,
    gmt_update       timestamp,
    gmt_delete       timestamp,
    create_operator  varchar(255),
    update_operator  varchar(255),
    delete_operator  varchar(255),
    is_deleted       boolean default false,
    tenant_id        bigint       not null
);

comment
on table bigmeta_pipeline_execution is 'pipeline执行实例表';

comment
on column bigmeta_pipeline_execution.id is '主键id';

comment
on column bigmeta_pipeline_execution.pipeline_name is 'pipeline名称';

comment
on column bigmeta_pipeline_execution.pipeline_type is 'pipeline类型';

comment
on column bigmeta_pipeline_execution.start_time is '开始时间';

comment
on column bigmeta_pipeline_execution.end_time is '结束时间';

comment
on column bigmeta_pipeline_execution.dag_run_id is 'pipeline实例对应dagRunId';

comment
on column bigmeta_pipeline_execution.external_trigger is '是否外部触发，true：是，false：否';

comment
on column bigmeta_pipeline_execution.state is '执行实例状态，running、success、failed';

comment
on column bigmeta_pipeline_execution.remarks IS '备注';

comment
on column bigmeta_pipeline_execution.gmt_create is '创建时间';

comment
on column bigmeta_pipeline_execution.gmt_update is '更新时间';

comment
on column bigmeta_pipeline_execution.tenant_id is '租户';

alter table bigmeta_pipeline_execution
    owner to postgres;

create table bigmeta_edge_default
(
    id                   bigserial
        primary key,
    tenant_id            bigint,
    type_code            varchar(255),
    src_guid             varchar(255),
    dst_guid             varchar(255),
    rank                 integer,
    query_key_encryption varchar(32),
    json                 jsonb,
    src_sys_update_time  timestamp,
    collect_start_time   timestamp,
    create_operator      varchar(255),
    update_operator      varchar(255),
    delete_operator      varchar(255),
    gmt_create           timestamp,
    gmt_update           timestamp,
    gmt_delete           timestamp,
    is_deleted           boolean
);

comment
on table bigmeta_edge_default is 'A table of entity to save the data of other type code';

comment
on column bigmeta_edge_default.id is 'PRIMARY KEY';

comment
on column bigmeta_edge_default.tenant_id is 'tenant id';

comment
on column bigmeta_edge_default.type_code is 'type code';

comment
on column bigmeta_edge_default.src_guid is 'src guid';

comment
on column bigmeta_edge_default.dst_guid is 'dst guid';

comment
on column bigmeta_edge_default.rank is 'rank';

comment
on column bigmeta_edge_default.query_key_encryption is 'md5(src_guid_dst_guid_rank)';

comment
on column bigmeta_edge_default.json is 'json, to save the property of type system definition';

comment
on column bigmeta_edge_default.src_sys_update_time is 'source system execute time';

comment
on column bigmeta_edge_default.collect_start_time is 'collect start time';

comment
on column bigmeta_edge_default.create_operator is 'create operator';

comment
on column bigmeta_edge_default.update_operator is 'update operator';

comment
on column bigmeta_edge_default.delete_operator is 'delete operator';

comment
on column bigmeta_edge_default.gmt_create is 'gmt create';

comment
on column bigmeta_edge_default.gmt_update is 'gmt update';

comment
on column bigmeta_edge_default.gmt_delete is 'gmt delete';

comment
on column bigmeta_edge_default.is_deleted is 'is deleted';

alter table bigmeta_edge_default
    owner to postgres;

ALTER TABLE bigmeta_edge_default SET (autovacuum_vacuum_scale_factor = 0, autovacuum_vacuum_threshold = 1000);

CREATE INDEX idx_bigmeta_edge_default_query_key_encrypti ON bigmeta_edge_default USING hash (query_key_encryption);
CREATE INDEX idx_bigmeta_edge_default_src_guid ON bigmeta_edge_default USING hash (src_guid);
CREATE INDEX idx_bigmeta_edge_default_dst_guid ON bigmeta_edge_default USING hash (dst_guid);

create table bigmeta_classification_metrics
(
    id              bigserial
        constraint bigmeta_classification_metrics_pk
            primary key,
    meta_name       varchar(255),
    meta_type       varchar(255),
    meta_version    bigint,
    meta_desc       varchar(255),
    values          jsonb,
    gmt_create      timestamp,
    gmt_update      timestamp,
    gmt_delete      timestamp,
    create_operator varchar(255),
    update_operator varchar(255),
    delete_operator varchar(255),
    is_deleted      boolean,
    predict_result  jsonb,
    cost            bigint,
    accuracy        double precision,
    recall          double precision
);

alter table bigmeta_classification_metrics
    owner to postgres;

create unique index bigmeta_classification_metrics_id_uindex
    on bigmeta_classification_metrics (id);

create table bigmeta_datasource
(
    id                   bigserial
        primary key,
    tenant_id            bigint,
    guid                 varchar(255),
    pipeline_name        varchar(255),
    name                 varchar(63),
    type                 varchar(31),
    description          text,
    owner                varchar(255),
    connection_args      json,
    integration_config   json,
    integration_schedule json,
    create_operator      varchar(255),
    update_operator      varchar(255),
    delete_operator      varchar(255),
    gmt_create           timestamp,
    gmt_update           timestamp,
    gmt_delete           timestamp,
    is_deleted           boolean
);

alter table bigmeta_datasource
    owner to postgres;

create index idx_guid
    on bigmeta_datasource (tenant_id, guid);

create index idx_pipeline
    on bigmeta_datasource (tenant_id, pipeline_name);

create table bigmeta_entity_database_schema
(
    id                  bigserial
        primary key,
    tenant_id           bigint,
    guid                varchar(255),
    type_code           varchar(255),
    belong_guid         varchar(255),
    name                varchar(255),
    display_name        varchar(255),
    description         varchar(255),
    database_guid       varchar(255),
    datasource_guid     varchar(255),
    datasource_type     varchar(255),
    src_sys_update_time bigint,
    collect_start_time  bigint,
    json                json,
    version             bigint,
    create_operator     varchar(255),
    update_operator     varchar(255),
    delete_operator     varchar(255),
    gmt_create          timestamp,
    gmt_update          timestamp,
    gmt_delete          timestamp,
    is_deleted          boolean
);

comment
on table bigmeta_entity_database_schema is 'A table of entity to save database schema detail';

comment
on column bigmeta_entity_database_schema.id is 'PRIMARY KEY';

comment
on column bigmeta_entity_database_schema.tenant_id is 'tenant id';

comment
on column bigmeta_entity_database_schema.guid is 'guid';

comment
on column bigmeta_entity_database_schema.type_code is 'type code';

comment
on column bigmeta_entity_database_schema.belong_guid is 'common field, is null';

comment
on column bigmeta_entity_database_schema.name is 'name';

comment
on column bigmeta_entity_database_schema.display_name is 'display name';

comment
on column bigmeta_entity_database_schema.description is 'db description text';

comment
on column bigmeta_entity_database_schema.database_guid is 'database guid';

comment
on column bigmeta_entity_database_schema.datasource_guid is 'datasource guid';

comment
on column bigmeta_entity_database_schema.datasource_type is 'datasource type, eg. mysql,hive';

comment
on column bigmeta_entity_database_schema.src_sys_update_time is 'source system execute time';

comment
on column bigmeta_entity_database_schema.collect_start_time is 'collect start time';

comment
on column bigmeta_entity_database_schema.json is 'extend json, to save the property of type system definition';

comment
on column bigmeta_entity_database_schema.version is 'version';

comment
on column bigmeta_entity_database_schema.create_operator is 'create operator';

comment
on column bigmeta_entity_database_schema.update_operator is 'update operator';

comment
on column bigmeta_entity_database_schema.delete_operator is 'delete operator';

comment
on column bigmeta_entity_database_schema.gmt_create is 'gmt create';

comment
on column bigmeta_entity_database_schema.gmt_update is 'gmt update';

comment
on column bigmeta_entity_database_schema.gmt_delete is 'gmt delete';

comment
on column bigmeta_entity_database_schema.is_deleted is 'is deleted';

alter table bigmeta_entity_database_schema
    owner to postgres;

CREATE INDEX idx_bigmeta_entity_database_schema_guid ON bigmeta_entity_database_schema USING hash (guid);

create index idx_bigmeta_entity_database_schema_tenant_id_belong_guid
    on bigmeta_entity_database_schema (tenant_id, belong_guid);

create table bigmeta_entity_database
(
    id                  bigserial
        primary key,
    tenant_id           bigint,
    guid                varchar(255),
    type_code           varchar(255),
    belong_guid         varchar(255),
    name                varchar(255),
    display_name        varchar(255),
    description         varchar(255),
    datasource_type     varchar(255),
    datasource_guid     varchar(255),
    src_sys_update_time bigint,
    collect_start_time  bigint,
    json                json,
    version             bigint,
    create_operator     varchar(255),
    update_operator     varchar(255),
    delete_operator     varchar(255),
    gmt_create          timestamp,
    gmt_update          timestamp,
    gmt_delete          timestamp,
    is_deleted          boolean
);

comment
on table bigmeta_entity_database is 'A table of entity to save database detail';

comment
on column bigmeta_entity_database.id is 'PRIMARY KEY';

comment
on column bigmeta_entity_database.tenant_id is 'tenant id';

comment
on column bigmeta_entity_database.guid is 'guid';

comment
on column bigmeta_entity_database.type_code is 'type code';

comment
on column bigmeta_entity_database.belong_guid is 'common field, is null';

comment
on column bigmeta_entity_database.name is 'database name';

comment
on column bigmeta_entity_database.display_name is 'display name';

comment
on column bigmeta_entity_database.description is 'db description text';

comment
on column bigmeta_entity_database.datasource_type is 'datasource type, eg. mysql,hive';

comment
on column bigmeta_entity_database.datasource_guid is 'the database belongs to which datasource service';

comment
on column bigmeta_entity_database.src_sys_update_time is 'source system execute time';

comment
on column bigmeta_entity_database.collect_start_time is 'collect start time';

comment
on column bigmeta_entity_database.json is 'extend json, to save the property of type system definition';

comment
on column bigmeta_entity_database.version is 'version';

comment
on column bigmeta_entity_database.create_operator is 'create operator';

comment
on column bigmeta_entity_database.update_operator is 'update operator';

comment
on column bigmeta_entity_database.delete_operator is 'delete operator';

comment
on column bigmeta_entity_database.gmt_create is 'gmt create';

comment
on column bigmeta_entity_database.gmt_update is 'gmt update';

comment
on column bigmeta_entity_database.gmt_delete is 'gmt delete';

comment
on column bigmeta_entity_database.is_deleted is 'is deleted';

alter table bigmeta_entity_database
    owner to postgres;

CREATE INDEX idx_bigmeta_entity_database_guid ON bigmeta_entity_database USING hash (guid);

create table bigmeta_entity_table
(
    id                   bigserial
        primary key,
    tenant_id            bigint,
    guid                 varchar(255),
    type_code            varchar(255),
    belong_guid          varchar(255),
    name                 varchar(255),
    display_name         varchar(255),
    description          varchar(255),
    database_guid        varchar(255),
    database_schema_guid varchar(255),
    columns              json,
    table_type           varchar(100),
    table_partition      varchar(255),
    sql                  text,
    tag_labels           text,
    owner                varchar(100),
    usage_summary        varchar(255),
    joins                varchar(512),
    json                 json,
    src_sys_update_time  bigint,
    collect_start_time   bigint,
    version              bigint,
    create_operator      varchar(255),
    update_operator      varchar(255),
    delete_operator      varchar(255),
    gmt_create           timestamp,
    gmt_update           timestamp,
    gmt_delete           timestamp,
    is_deleted           boolean
);

comment
on table bigmeta_entity_table is 'A table of entity to save table detail';

comment
on column bigmeta_entity_table.id is 'PRIMARY KEY';

comment
on column bigmeta_entity_table.tenant_id is 'tenant id';

comment
on column bigmeta_entity_table.guid is 'table guid';

comment
on column bigmeta_entity_table.type_code is 'type code';

comment
on column bigmeta_entity_table.belong_guid is 'common field, is null';

comment
on column bigmeta_entity_table.name is 'table name';

comment
on column bigmeta_entity_table.display_name is 'display name';

comment
on column bigmeta_entity_table.description is 'table description text';

comment
on column bigmeta_entity_table.database_guid is 'database guid';

comment
on column bigmeta_entity_table.database_schema_guid is 'database schema guid';

comment
on column bigmeta_entity_table.columns is 'column list';

comment
on column bigmeta_entity_table.table_type is 'table type';

comment
on column bigmeta_entity_table.table_partition is 'This schema defines the partition column of a table and format the partition is created.';

comment
on column bigmeta_entity_table.sql is 'DDL or SQL query statement.';

comment
on column bigmeta_entity_table.tag_labels is 'Tags for this table.';

comment
on column bigmeta_entity_table.owner is 'owner of this table';

comment
on column bigmeta_entity_table.usage_summary is 'usage details';

comment
on column bigmeta_entity_table.joins is 'how this table is joined with other tables and columns';

comment
on column bigmeta_entity_table.json is 'extend json, to save the property of type system definition';

comment
on column bigmeta_entity_table.src_sys_update_time is 'source system execute time';

comment
on column bigmeta_entity_table.collect_start_time is 'collect start time';

comment
on column bigmeta_entity_table.version is 'version';

comment
on column bigmeta_entity_table.create_operator is 'create operator';

comment
on column bigmeta_entity_table.update_operator is 'update operator';

comment
on column bigmeta_entity_table.delete_operator is 'delete operator';

comment
on column bigmeta_entity_table.gmt_create is 'gmt create';

comment
on column bigmeta_entity_table.gmt_update is 'gmt update';

comment
on column bigmeta_entity_table.gmt_delete is 'gmt delete';

comment
on column bigmeta_entity_table.is_deleted is 'is deleted';

alter table bigmeta_entity_table
    owner to postgres;

CREATE INDEX idx_bigmeta_entity_table_guid ON bigmeta_entity_table USING hash (guid);

CREATE INDEX idx_bigmeta_entity_table_tenant_id_belong_guid
    ON bigmeta_entity_table (tenant_id, belong_guid);

CREATE INDEX idx_bigmeta_entity_table_tenant_id_name
    ON bigmeta_entity_table (tenant_id, name);

create table bigmeta_entity_column
(
    id                  bigserial
        primary key,
    tenant_id           bigint,
    guid                varchar(255),
    type_code           varchar(255),
    belong_guid         varchar(255),
    name                varchar(255),
    display_name        varchar(255),
    description         varchar(255),
    table_guid          varchar(255),
    caliber_function    text,
    caliber_expression  text,
    caliber_sql         text,
    data_type           varchar(100),
    data_length         integer,
    precision           integer,
    position            integer,
    scale               integer,
    tag_labels          text,
    is_partition        boolean,
    json                json,
    src_sys_update_time bigint,
    collect_start_time  bigint,
    version             bigint,
    create_operator     varchar(255),
    update_operator     varchar(255),
    delete_operator     varchar(255),
    gmt_create          timestamp,
    gmt_update          timestamp,
    gmt_delete          timestamp,
    is_deleted          boolean
);

comment
on table bigmeta_entity_column is 'A table of entity to save column detail';

comment
on column bigmeta_entity_column.id is 'PRIMARY KEY';

comment
on column bigmeta_entity_column.tenant_id is 'tenant id';

comment
on column bigmeta_entity_column.guid is 'column guid';

comment
on column bigmeta_entity_column.type_code is 'type code';

comment
on column bigmeta_entity_column.belong_guid is 'common field, represent table guid';

comment
on column bigmeta_entity_column.name is 'column name';

comment
on column bigmeta_entity_column.display_name is 'display name';

comment
on column bigmeta_entity_column.description is 'column description text';

comment
on column bigmeta_entity_column.table_guid is 'table guid';

comment
on column bigmeta_entity_column.caliber_function is 'column process logic expression sql function';

comment
on column bigmeta_entity_column.caliber_expression is 'column process logic expressions';

comment
on column bigmeta_entity_column.caliber_sql is 'column process logic expression full sql';

comment
on column bigmeta_entity_column.data_type is 'data type';

comment
on column bigmeta_entity_column.data_length is 'data length';

comment
on column bigmeta_entity_column.precision is 'The precision of a numeric dataType';

comment
on column bigmeta_entity_column.position is 'The position of column in table';

comment
on column bigmeta_entity_column.scale is 'The scale of a numeric dataType, to the right of the decimal point';

comment
on column bigmeta_entity_column.tag_labels is 'tags on this column';

comment
on column bigmeta_entity_column.is_partition is 'is partition column';

comment
on column bigmeta_entity_column.json is 'extend json, to save the property of type system definition';

comment
on column bigmeta_entity_column.src_sys_update_time is 'source system execute time';

comment
on column bigmeta_entity_column.collect_start_time is 'collect start time';

comment
on column bigmeta_entity_column.version is 'version';

comment
on column bigmeta_entity_column.create_operator is 'create operator';

comment
on column bigmeta_entity_column.update_operator is 'update operator';

comment
on column bigmeta_entity_column.delete_operator is 'delete operator';

comment
on column bigmeta_entity_column.gmt_create is 'gmt create';

comment
on column bigmeta_entity_column.gmt_update is 'gmt update';

comment
on column bigmeta_entity_column.gmt_delete is 'gmt delete';

comment
on column bigmeta_entity_column.is_deleted is 'is deleted';

alter table bigmeta_entity_column
    owner to postgres;

ALTER TABLE bigmeta_entity_column SET (autovacuum_vacuum_scale_factor = 0, autovacuum_vacuum_threshold = 1000);

CREATE INDEX idx_bigmeta_entity_column_guid ON bigmeta_entity_column USING hash (guid);

create index idx_bigmeta_entity_column_tenant_id_belong_guid
    on bigmeta_entity_column (tenant_id, belong_guid);

create index idx_bigmeta_entity_column_gmt_update
    on bigmeta_entity_column (gmt_update);

create index idx_bigmeta_entity_column_tenant_id_name
    on bigmeta_entity_column (tenant_id, name);

create table bigmeta_entity_default
(
    id                  bigserial
        primary key,
    tenant_id           bigint,
    type_code           varchar(255),
    belong_guid         varchar(255),
    guid                varchar(255),
    json                jsonb,
    src_sys_update_time bigint,
    collect_start_time  bigint,
    version             bigint,
    create_operator     varchar(255),
    update_operator     varchar(255),
    delete_operator     varchar(255),
    gmt_create          timestamp,
    gmt_update          timestamp,
    gmt_delete          timestamp,
    is_deleted          boolean
);

comment
on table bigmeta_entity_default is 'A table of entity to save the data of other type code';

comment
on column bigmeta_entity_default.id is 'PRIMARY KEY';

comment
on column bigmeta_entity_default.tenant_id is 'tenant id';

comment
on column bigmeta_entity_default.type_code is 'type code';

comment
on column bigmeta_entity_default.belong_guid is 'common field, may be null, refer to bigmeta_entity_column';

comment
on column bigmeta_entity_default.guid is 'guid';

comment
on column bigmeta_entity_default.json is 'json, to save the property of type system definition';

comment
on column bigmeta_entity_default.src_sys_update_time is 'source system execute time';

comment
on column bigmeta_entity_default.collect_start_time is 'collect start time';

comment
on column bigmeta_entity_default.version is 'version';

comment
on column bigmeta_entity_default.create_operator is 'create operator';

comment
on column bigmeta_entity_default.update_operator is 'update operator';

comment
on column bigmeta_entity_default.delete_operator is 'delete operator';

comment
on column bigmeta_entity_default.gmt_create is 'gmt create';

comment
on column bigmeta_entity_default.gmt_update is 'gmt update';

comment
on column bigmeta_entity_default.gmt_delete is 'gmt delete';

comment
on column bigmeta_entity_default.is_deleted is 'is deleted';

alter table bigmeta_entity_default
    owner to postgres;

ALTER TABLE bigmeta_entity_default SET (autovacuum_vacuum_scale_factor = 0, autovacuum_vacuum_threshold = 1000);

CREATE INDEX idx_bigmeta_entity_default_guid ON bigmeta_entity_default USING hash (guid);

create table bigmeta_propagation_record
(
    id                  bigserial
        primary key,
    start_guid          varchar(255),
    start_tag_type      smallint,
    meta_version        bigint,
    task_id             varchar(255),
    classification_code varchar(255),
    tire_code           varchar(255),
    result              text,
    start_time          timestamp(6),
    end_time            timestamp(6),
    status              boolean default true,
    error_msg           text,
    tenant_id           bigint,
    create_operator     varchar(255),
    update_operator     varchar(255),
    delete_operator     varchar(255),
    gmt_create          timestamp(6),
    gmt_update          timestamp(6),
    gmt_delete          timestamp(6),
    is_deleted          boolean default false
);

alter table bigmeta_propagation_record
    owner to postgres;

create table bigmeta_lake_metaversion
(
    id              bigserial
        primary key,
    tenant_id       bigint,
    guid            varchar(255),
    content         jsonb,
    content_digest  varchar(255),
    diff            jsonb,
    version         bigint,
    biz_time        timestamp,
    status          integer,
    create_operator varchar(255),
    gmt_create      timestamp,
    update_operator varchar(255),
    gmt_update      timestamp,
    is_deleted      boolean default false,
    delete_operator varchar(255),
    gmt_delete      timestamp
);

comment
on table bigmeta_lake_metaversion is '元数据版本';

comment
on column bigmeta_lake_metaversion.id is 'id';

comment
on column bigmeta_lake_metaversion.tenant_id is '租户号';

comment
on column bigmeta_lake_metaversion.guid is '实体guid';

comment
on column bigmeta_lake_metaversion.content is '版本内容';

comment
on column bigmeta_lake_metaversion.content_digest is '版本内容签名';

comment
on column bigmeta_lake_metaversion.diff is '与上一版本差异';

comment
on column bigmeta_lake_metaversion.version is '版本号';

comment
on column bigmeta_lake_metaversion.biz_time is '业务时间(生成新版本时判断)';

comment
on column bigmeta_lake_metaversion.status is '状态';

comment
on column bigmeta_lake_metaversion.create_operator is '创建人';

comment
on column bigmeta_lake_metaversion.gmt_create is '创建时间';

comment
on column bigmeta_lake_metaversion.update_operator is '更新人';

comment
on column bigmeta_lake_metaversion.gmt_update is '更新时间';

comment
on column bigmeta_lake_metaversion.is_deleted is '是否删除';

comment
on column bigmeta_lake_metaversion.delete_operator is '删除人';

comment
on column bigmeta_lake_metaversion.gmt_delete is '删除时间';

alter table bigmeta_lake_metaversion
    owner to postgres;

create index idx_meta_version
    on bigmeta_lake_metaversion (tenant_id, guid);

create table bigmeta_lake_metatype
(
    id               bigserial
        primary key,
    tenant_id        bigint,
    er_type          integer,
    type_code        varchar(255),
    parent_type_code varchar(255),
    attributes       jsonb,
    version          bigint,
    comments         varchar(255),
    create_operator  varchar(255),
    gmt_create       timestamp,
    update_operator  varchar(255),
    gmt_update       timestamp,
    is_deleted       boolean default false,
    delete_operator  varchar(255),
    gmt_delete       timestamp
);

comment
on table bigmeta_lake_metatype is '元数据类型定义MetaType';

comment
on column bigmeta_lake_metatype.id is 'id';

comment
on column bigmeta_lake_metatype.tenant_id is '租户号';

comment
on column bigmeta_lake_metatype.er_type is '实体关系类型';

comment
on column bigmeta_lake_metatype.type_code is '类型编码';

comment
on column bigmeta_lake_metatype.parent_type_code is '父类型编码(继承属性)';

comment
on column bigmeta_lake_metatype.attributes is '属性列表';

comment
on column bigmeta_lake_metatype.version is '版本号';

comment
on column bigmeta_lake_metatype.comments is '类型描述';

comment
on column bigmeta_lake_metatype.create_operator is '创建人';

comment
on column bigmeta_lake_metatype.gmt_create is '创建时间';

comment
on column bigmeta_lake_metatype.update_operator is '更新人';

comment
on column bigmeta_lake_metatype.gmt_update is '更新时间';

comment
on column bigmeta_lake_metatype.is_deleted is '是否删除';

comment
on column bigmeta_lake_metatype.delete_operator is '删除人';

comment
on column bigmeta_lake_metatype.gmt_delete is '删除时间';

alter table bigmeta_lake_metatype
    owner to postgres;

create index idx_metatype
    on bigmeta_lake_metatype (tenant_id, type_code);

create index idx_metatype_attrs
    on bigmeta_lake_metatype using gin (attributes);

create table bigmeta_ingestion_pipeline
(
    id                 bigserial
        constraint bigmeta_ingestion_pipeline_pk
            primary key,
    datasource_type    varchar(32) not null,
    datasource_name    varchar(64) not null,
    pipeline_name      varchar(512),
    ingestion_pipeline jsonb,
    workflow_config    jsonb,
    gmt_create         timestamp,
    gmt_update         timestamp,
    gmt_delete         timestamp,
    create_operator    varchar(255),
    update_operator    varchar(255),
    delete_operator    varchar(255),
    is_deleted         boolean default false,
    tenant_id          bigint      not null
);

comment
on table bigmeta_ingestion_pipeline is 'ingestion_pipeline对应json表';

comment
on column bigmeta_ingestion_pipeline.id is '主键id';

comment
on column bigmeta_ingestion_pipeline.datasource_type is '数据源类型，database、dashboard、service、message';

comment
on column bigmeta_ingestion_pipeline.datasource_name is '数据源名称';

comment
on column bigmeta_ingestion_pipeline.pipeline_name is 'pipeline名称';

comment
on column bigmeta_ingestion_pipeline.ingestion_pipeline is 'ingestion_pipeline对应json';

comment
on column bigmeta_ingestion_pipeline.workflow_config is 'workflow_config对应json';

comment
on column bigmeta_ingestion_pipeline.gmt_create is '创建时间';

comment
on column bigmeta_ingestion_pipeline.gmt_update is '修改时间';

comment
on column bigmeta_ingestion_pipeline.create_operator is '创建人';

comment
on column bigmeta_ingestion_pipeline.update_operator is '修改人';

comment
on column bigmeta_ingestion_pipeline.tenant_id is '租户';

alter table bigmeta_ingestion_pipeline
    owner to postgres;

create view column_classify_record_view
            (columnguid, rid, rcost, code, "preRuleCost", "algorithmCost", "algorithmType", "preRuleMatched",
             "algorithmMatched", "preRuleTotalCount", "algorithmTotalCount", "preRuleMatchedCount",
             "algorithmMatchedCount", "classificationVersion")
as
SELECT aaa.columnguid,
       aaa.rid,
       aaa.rcost,
       aaa.code,
       aaa."preRuleCost",
       aaa."algorithmCost",
       aaa."algorithmType",
       aaa."preRuleMatched",
       aaa."algorithmMatched",
       aaa."preRuleTotalCount",
       aaa."algorithmTotalCount",
       aaa."preRuleMatchedCount",
       aaa."algorithmMatchedCount",
       aaa."classificationVersion"
FROM (SELECT r.column_guid AS columnguid,
             r.id          AS rid,
             r.cost        AS rcost,
             x.code,
             x."preRuleCost",
             x."algorithmCost",
             x."algorithmType",
             x."preRuleMatched",
             x."algorithmMatched",
             x."preRuleTotalCount",
             x."algorithmTotalCount",
             x."preRuleMatchedCount",
             x."algorithmMatchedCount",
             x."classificationVersion"
      FROM bigmeta_classification_record r,
           LATERAL jsonb_to_recordset(r.records) x(code text, "preRuleCost" text, "algorithmCost" bigint,
                                                   "algorithmType" text, "preRuleMatched" boolean,
                                                   "algorithmMatched" boolean, "preRuleTotalCount" bigint,
                                                   "algorithmTotalCount" bigint, "preRuleMatchedCount" bigint,
                                                   "algorithmMatchedCount" bigint, "classificationVersion" bigint)) aaa
ORDER BY aaa.rid DESC;

alter table column_classify_record_view
    owner to postgres;

create view bigmeta_per_classification_in_one_classify_run
            (id, column_guid, cost, records, gmt_create, gmt_update, gmt_delete, create_operator, update_operator,
             delete_operator, is_deleted, tenant_id, data, result, code, accuracy, precision, recall, "preRuleCost",
             "algorithmCost", "algorithmType", "preRuleMatched", "algorithmMatched", "preRuleTotalCount",
             "algorithmTotalCount", "preRuleMatchedCount", "algorithmMatchedCount", "classificationVersion", "fnList",
             "fpList")
as
SELECT r.id,
       r.column_guid,
       r.cost,
       r.records,
       r.gmt_create,
       r.gmt_update,
       r.gmt_delete,
       r.create_operator,
       r.update_operator,
       r.delete_operator,
       r.is_deleted,
       r.tenant_id,
       r.data,
       r.result,
       x.code,
       x.accuracy,
       x."precision",
       x.recall,
       x."preRuleCost",
       x."algorithmCost",
       x."algorithmType",
       x."preRuleMatched",
       x."algorithmMatched",
       x."preRuleTotalCount",
       x."algorithmTotalCount",
       x."preRuleMatchedCount",
       x."algorithmMatchedCount",
       x."classificationVersion",
       x."fnList",
       x."fpList"
FROM bigmeta_classification_record r,
     LATERAL jsonb_to_recordset(r.records) x(code text, accuracy double precision, "precision" double precision,
                                             recall double precision, "preRuleCost" text, "algorithmCost" bigint,
                                             "algorithmType" text, "preRuleMatched" boolean, "algorithmMatched" boolean,
                                             "preRuleTotalCount" bigint, "algorithmTotalCount" bigint,
                                             "preRuleMatchedCount" bigint, "algorithmMatchedCount" bigint,
                                             "classificationVersion" bigint, "fnList" text, "fpList" text);

alter table bigmeta_per_classification_in_one_classify_run
    owner to postgres;



CREATE SEQUENCE user_id_seq INCREMENT 1 MINVALUE 1 MAXVALUE 999999999999 START 1 CACHE 10;
CREATE TABLE "bigmeta_user"
(
    "id"          int8                                        NOT NULL DEFAULT nextval('user_id_seq'::regclass),
    "create_time" timestamp(6),
    "update_time" timestamp(6),
    "enabled"     bool,
    "full_name"   varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
    "password"    varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
    "user_name"   varchar(255) COLLATE "pg_catalog"."default" NOT NULL,
    "roles"       varchar(100) COLLATE "pg_catalog"."default",
    PRIMARY KEY ("id")
)
;

ALTER TABLE "bigmeta_user"
    OWNER TO "postgres";

-- ----------------------------
-- Table structure for bigmeta_ingestion_msg_record
-- ----------------------------
CREATE SEQUENCE ingestion_msg_id_seq START 1;
DROP TABLE IF EXISTS "public"."bigmeta_ingestion_msg_record";
CREATE TABLE "public"."bigmeta_ingestion_msg_record"
(
    "id"         int8 NOT NULL DEFAULT nextval('ingestion_msg_id_seq'::regclass),
    "uuid"       varchar(64) COLLATE "pg_catalog"."default",
    "msg_type"   varchar(16) COLLATE "pg_catalog"."default",
    "msg"        jsonb,
    "status"     int2,
    "tenant_id"  int8,
    "is_deleted" bool,
    "gmt_create" timestamp(6),
    "gmt_update" timestamp(6),
    "task_name"  varchar(512) COLLATE "pg_catalog"."default",
    "retry_cnt"  int4          DEFAULT 0
)
;
ALTER TABLE "public"."bigmeta_ingestion_msg_record" OWNER TO "postgres";

-- ----------------------------
-- Indexes structure for table bigmeta_ingestion_msg_record
-- ----------------------------
create index idx_status on bigmeta_ingestion_msg_record (status);
create index idx_tenantId on bigmeta_ingestion_msg_record (tenant_id, task_name, status, retry_cnt);
create index idx_uuid on bigmeta_ingestion_msg_record (uuid, id);

create index idx_msg
    on bigmeta_ingestion_msg_record using gin (msg);

-- ----------------------------
-- Primary Key structure for table bigmeta_ingestion_msg_record
-- ----------------------------
ALTER TABLE "public"."bigmeta_ingestion_msg_record"
    ADD CONSTRAINT "bigmeta_ingestion_msg_record_pkey" PRIMARY KEY ("id");
