-- migrate:up
-- 000001_init_schema.up.sql
CREATE TABLE IF NOT EXISTS "ads"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "title"      varchar(255)   NOT NULL DEFAULT '',
    "type"       varchar(255)   NOT NULL DEFAULT '',
    "content"    text,
    "target_url" varchar(512)            DEFAULT '',
    "start_time" TIMESTAMP                                                               DEFAULT NULL,
    "end_time"   TIMESTAMP                                                               DEFAULT NULL,
    "status"     SMALLINT                                                             DEFAULT '0',
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "announcement"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "title"      varchar(255)   NOT NULL DEFAULT '',
    "content"    text,
    "show"       BOOLEAN                                                    NOT NULL DEFAULT false,
    "pinned"     BOOLEAN                                                    NOT NULL DEFAULT false,
    "popup"      BOOLEAN                                                    NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "application"
(
    "id"             BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"           varchar(255)   NOT NULL DEFAULT '',
    "icon"           text           NOT NULL,
    "description"    text,
    "subscribe_type" varchar(50)    NOT NULL DEFAULT '',
    "created_at"     TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"     TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "application_config"
(
    "id"                        BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "app_id"                    bigint NOT NULL                                               DEFAULT '0',
    "encryption_key"            text,
    "encryption_method"         varchar(255)   DEFAULT NULL,
    "domains"                   text,
    "startup_picture"           text,
    "startup_picture_skip_time" bigint NOT NULL                                               DEFAULT '0',
    "created_at"                TIMESTAMP(3)                                                   DEFAULT NULL,
    "updated_at"                TIMESTAMP(3)                                                   DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "application_version"
(
    "id"             BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "url"            varchar(255)   NOT NULL DEFAULT '',
    "version"        varchar(255)   NOT NULL DEFAULT '',
    "platform"       varchar(50)    NOT NULL DEFAULT '',
    "is_default"     BOOLEAN                                                    NOT NULL DEFAULT false,
    "description"    text,
    "application_id" bigint                                                                 DEFAULT NULL,
    "created_at"     TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"     TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "application_version_fk_application_application_versions" ON "application_version" ("application_id");
CREATE TABLE IF NOT EXISTS "auth_method"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "method"     varchar(255)   NOT NULL DEFAULT '',
    "config"     text           NOT NULL,
    "enabled"    BOOLEAN                                                    NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "uni_auth_method" UNIQUE ("method")
);
CREATE TABLE IF NOT EXISTS "coupon"
(
    "id"          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"        varchar(255)   NOT NULL DEFAULT '',
    "code"        varchar(255)   NOT NULL DEFAULT '',
    "count"       bigint                                                        NOT NULL DEFAULT '0',
    "type"        SMALLINT                                                    NOT NULL DEFAULT '1',
    "discount"    bigint                                                        NOT NULL DEFAULT '0',
    "start_time"  bigint                                                        NOT NULL DEFAULT '0',
    "expire_time" bigint                                                        NOT NULL DEFAULT '0',
    "user_limit"  bigint                                                        NOT NULL DEFAULT '0',
    "subscribe"   varchar(255)   NOT NULL DEFAULT '',
    "used_count"  bigint                                                        NOT NULL DEFAULT '0',
    "enable"      BOOLEAN                                                    NOT NULL DEFAULT true,
    "created_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "uni_coupon_code" UNIQUE ("code")
);
CREATE TABLE IF NOT EXISTS "document"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "title"      varchar(255)   NOT NULL DEFAULT '',
    "content"    text,
    "tags"       varchar(255)   NOT NULL DEFAULT '',
    "show"       BOOLEAN                                                    NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "message_log"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "type"       varchar(50)    NOT NULL DEFAULT 'email',
    "platform"   varchar(50)    NOT NULL DEFAULT 'smtp',
    "to"         text           NOT NULL,
    "subject"    varchar(255)   NOT NULL DEFAULT '',
    "content"    text,
    "status"     SMALLINT                                                    NOT NULL DEFAULT '0',
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "order"
(
    "id"              BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "parent_id"       bigint                                                                 DEFAULT NULL,
    "user_id"         bigint                                                        NOT NULL DEFAULT '0',
    "order_no"        varchar(255)   NOT NULL DEFAULT '',
    "type"            SMALLINT                                                    NOT NULL DEFAULT '1',
    "quantity"        bigint                                                        NOT NULL DEFAULT '1',
    "price"           bigint                                                        NOT NULL DEFAULT '0',
    "amount"          bigint                                                        NOT NULL DEFAULT '0',
    "gift_amount"     bigint                                                        NOT NULL DEFAULT '0',
    "discount"        bigint                                                        NOT NULL DEFAULT '0',
    "coupon"          varchar(255)            DEFAULT NULL,
    "coupon_discount" bigint                                                        NOT NULL DEFAULT '0',
    "commission"      bigint                                                        NOT NULL DEFAULT '0',
    "payment_id"      bigint                                                        NOT NULL DEFAULT '-1',
    "method"          varchar(255)   NOT NULL DEFAULT '',
    "fee_amount"      bigint                                                        NOT NULL DEFAULT '0',
    "trade_no"        varchar(255)            DEFAULT NULL,
    "status"          SMALLINT                                                    NOT NULL DEFAULT '1',
    "subscribe_id"    bigint                                                        NOT NULL DEFAULT '0',
    "subscribe_token" varchar(255)            DEFAULT NULL,
    "is_new"          BOOLEAN                                                    NOT NULL DEFAULT false,
    "created_at"      TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"      TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "uni_order_order_no" UNIQUE ("order_no")
);
CREATE TABLE IF NOT EXISTS "payment"
(
    "id"          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"        varchar(100)   NOT NULL DEFAULT '',
    "platform"    varchar(100)   NOT NULL,
    "description" text,
    "icon"        varchar(255)            DEFAULT '',
    "domain"      varchar(255)            DEFAULT '',
    "config"      text           NOT NULL,
    "fee_mode"    SMALLINT                                                    NOT NULL DEFAULT '0',
    "fee_percent" bigint                                                                 DEFAULT '0',
    "fee_amount"  bigint                                                                 DEFAULT '0',
    "enable"      BOOLEAN                                                    NOT NULL DEFAULT false,
    "token"       varchar(255)            DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "uni_payment_token" UNIQUE ("token")
);
CREATE TABLE IF NOT EXISTS "server"
(
    "id"               BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"             varchar(100)   NOT NULL DEFAULT '',
    "tags"             varchar(128)   NOT NULL DEFAULT '',
    "country"          varchar(128)   NOT NULL DEFAULT '',
    "city"             varchar(128)   NOT NULL DEFAULT '',
    "latitude"         varchar(128)   NOT NULL DEFAULT '',
    "longitude"        varchar(128)   NOT NULL DEFAULT '',
    "server_addr"      varchar(100)   NOT NULL DEFAULT '',
    "relay_mode"       varchar(20)    NOT NULL DEFAULT 'none',
    "relay_node"       text,
    "speed_limit"      bigint                                                        NOT NULL DEFAULT '0',
    "traffic_ratio"    decimal(4, 2)                                                 NOT NULL DEFAULT '0.00',
    "group_id"         bigint                                                                 DEFAULT NULL,
    "protocol"         varchar(20)    NOT NULL DEFAULT '',
    "config"           text,
    "enable"           SMALLINT                                                    NOT NULL DEFAULT '1',
    "sort"             bigint                                                        NOT NULL DEFAULT '0',
    "last_reported_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "created_at"       TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"       TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "server_idx_group_id" ON "server" ("group_id");
CREATE TABLE IF NOT EXISTS "server_group"
(
    "id"          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"        varchar(100)   NOT NULL DEFAULT '',
    "description" varchar(255)            DEFAULT '',
    "created_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
-- if "sms" not exist, create it
CREATE TABLE IF NOT EXISTS "sms"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "content"    text,
    "platform"   varchar(64)   DEFAULT NULL,
    "area_code"  varchar(64)   DEFAULT NULL,
    "telephone"  varchar(64)   DEFAULT NULL,
    "status"     SMALLINT                                                   DEFAULT '1',
    "created_at" timestamp NULL                                               DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "subscribe"
(
    "id"              BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"            varchar(255)   NOT NULL DEFAULT '',
    "description"     text,
    "unit_price"      bigint                                                        NOT NULL DEFAULT '0',
    "unit_time"       varchar(255)   NOT NULL DEFAULT '',
    "discount"        text,
    "replacement"     bigint                                                        NOT NULL DEFAULT '0',
    "inventory"       bigint                                                        NOT NULL DEFAULT '0',
    "traffic"         bigint                                                        NOT NULL DEFAULT '0',
    "speed_limit"     bigint                                                        NOT NULL DEFAULT '0',
    "device_limit"    bigint                                                        NOT NULL DEFAULT '0',
    "quota"           bigint                                                        NOT NULL DEFAULT '0',
    "group_id"        bigint                                                                 DEFAULT NULL,
    "server_group"    varchar(255)            DEFAULT NULL,
    "server"          varchar(255)            DEFAULT NULL,
    "show"            BOOLEAN                                                    NOT NULL DEFAULT false,
    "sell"            BOOLEAN                                                    NOT NULL DEFAULT false,
    "sort"            bigint                                                        NOT NULL DEFAULT '0',
    "deduction_ratio" bigint                                                                 DEFAULT '0',
    "allow_deduction" BOOLEAN                                                             DEFAULT true,
    "reset_cycle"     bigint                                                                 DEFAULT '0',
    "renewal_reset"   BOOLEAN                                                             DEFAULT false,
    "created_at"      TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"      TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "subscribe_group"
(
    "id"          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"        varchar(255)   NOT NULL DEFAULT '',
    "description" text,
    "created_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "subscribe_type"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"       varchar(50)    NOT NULL DEFAULT '',
    "mark"       varchar(255)   NOT NULL DEFAULT '',
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "system"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "category"   varchar(100)   NOT NULL DEFAULT '',
    "key"        varchar(100)   NOT NULL DEFAULT '',
    "value"      text           NOT NULL,
    "type"       varchar(50)    NOT NULL DEFAULT '',
    "desc"       text           NOT NULL,
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "uni_system_key" UNIQUE ("key")
);
CREATE INDEX IF NOT EXISTS "system_index_key" ON "system" ("key");
CREATE TABLE IF NOT EXISTS "ticket"
(
    "id"          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "title"       varchar(255)   NOT NULL DEFAULT '',
    "description" text,
    "user_id"     bigint                                                        NOT NULL DEFAULT '0',
    "status"      SMALLINT                                                    NOT NULL DEFAULT '1',
    "created_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "ticket_follow"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "ticket_id"  bigint                                                        NOT NULL DEFAULT '0',
    "from"       varchar(255)   NOT NULL DEFAULT '',
    "type"       SMALLINT                                                    NOT NULL DEFAULT '1',
    "content"    text,
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "traffic_log"
(
    "id"           BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "server_id"    bigint      NOT NULL,
    "user_id"      bigint      NOT NULL,
    "subscribe_id" bigint      NOT NULL,
    "download"     bigint               DEFAULT '0',
    "upload"       bigint               DEFAULT '0',
    "timestamp"    TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "traffic_log_idx_subscribe_id" ON "traffic_log" ("subscribe_id");
CREATE INDEX IF NOT EXISTS "traffic_log_idx_server_id" ON "traffic_log" ("server_id");
CREATE INDEX IF NOT EXISTS "traffic_log_idx_user_id" ON "traffic_log" ("user_id");
CREATE TABLE IF NOT EXISTS "user"
(
    "id"                      BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "password"                varchar(100)   NOT NULL,
    "avatar"                  text,
    "balance"                 bigint                                                                 DEFAULT '0',
    "telegram"                bigint                                                                 DEFAULT NULL,
    "refer_code"              varchar(20)             DEFAULT '',
    "referer_id"              bigint                                                                 DEFAULT NULL,
    "commission"              bigint                                                                 DEFAULT '0',
    "gift_amount"             bigint                                                                 DEFAULT '0',
    "enable"                  BOOLEAN                                                    NOT NULL DEFAULT true,
    "is_admin"                BOOLEAN                                                    NOT NULL DEFAULT false,
    "valid_email"             SMALLINT                                                    NOT NULL DEFAULT '0',
    "enable_email_notify"     SMALLINT                                                    NOT NULL DEFAULT '0',
    "enable_telegram_notify"  SMALLINT                                                    NOT NULL DEFAULT '0',
    "enable_balance_notify"   BOOLEAN                                                    NOT NULL DEFAULT false,
    "enable_login_notify"     BOOLEAN                                                    NOT NULL DEFAULT false,
    "enable_subscribe_notify" BOOLEAN                                                    NOT NULL DEFAULT false,
    "enable_trade_notify"     BOOLEAN                                                    NOT NULL DEFAULT false,
    "created_at"              TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"              TIMESTAMP(3)                                                            DEFAULT NULL,
    "deleted_at"              TIMESTAMP(3)                                                            DEFAULT NULL,
    "is_del"                  BIGINT                                                        DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "user_idx_referer" ON "user" ("referer_id");
CREATE TABLE IF NOT EXISTS "user_auth_methods"
(
    "id"              BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"         bigint                                                        NOT NULL,
    "auth_type"       varchar(255)   NOT NULL,
    "auth_identifier" varchar(255)   NOT NULL,
    "verified"        BOOLEAN                                                    NOT NULL DEFAULT false,
    "created_at"      TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"      TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "idx_auth_identifier" UNIQUE ("auth_identifier")
);
CREATE INDEX IF NOT EXISTS "user_auth_methods_idx_user_id" ON "user_auth_methods" ("user_id");
CREATE TABLE IF NOT EXISTS "user_balance_log"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"    bigint     NOT NULL,
    "amount"     bigint     NOT NULL,
    "type"       SMALLINT NOT NULL,
    "order_id"   bigint      DEFAULT NULL,
    "balance"    bigint     NOT NULL,
    "created_at" TIMESTAMP(3) DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "user_balance_log_idx_user_id" ON "user_balance_log" ("user_id");
CREATE TABLE IF NOT EXISTS "user_commission_log"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"    bigint NOT NULL,
    "order_no"   varchar(191)   DEFAULT NULL,
    "amount"     bigint NOT NULL,
    "created_at" TIMESTAMP(3)                                                   DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "user_commission_log_idx_user_id" ON "user_commission_log" ("user_id");
CREATE TABLE IF NOT EXISTS "user_device"
(
    "id"           BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"      bigint     NOT NULL,
    "subscribe_id" bigint                                                        DEFAULT NULL,
    "ip"           varchar(191)   DEFAULT NULL,
    "identifier"   varchar(191)   DEFAULT NULL,
    "user_agent"   varchar(64)    DEFAULT NULL,
    "online"       BOOLEAN NOT NULL                                           DEFAULT false,
    "enabled"      BOOLEAN NOT NULL                                           DEFAULT true,
    "created_at"   TIMESTAMP(3)                                                   DEFAULT NULL,
    "updated_at"   TIMESTAMP(3)                                                   DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "user_device_idx_user_id" ON "user_device" ("user_id");
CREATE TABLE IF NOT EXISTS "user_gift_amount_log"
(
    "id"                BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"           bigint     NOT NULL,
    "user_subscribe_id" bigint                                                        DEFAULT NULL,
    "order_no"          varchar(191)   DEFAULT NULL,
    "type"              SMALLINT NOT NULL,
    "amount"            bigint     NOT NULL,
    "balance"           bigint     NOT NULL,
    "remark"            varchar(255)   DEFAULT '',
    "created_at"        TIMESTAMP(3)                                                   DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "user_gift_amount_log_idx_user_id" ON "user_gift_amount_log" ("user_id");
CREATE TABLE IF NOT EXISTS "user_login_log"
(
    "id"         BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"    bigint                                                        NOT NULL,
    "login_ip"   varchar(255)   NOT NULL,
    "user_agent" text           NOT NULL,
    "success"    BOOLEAN                                                    NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "user_login_log_idx_user_id" ON "user_login_log" ("user_id");
CREATE TABLE IF NOT EXISTS "user_subscribe"
(
    "id"           BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"      bigint      NOT NULL,
    "order_id"     bigint      NOT NULL,
    "subscribe_id" bigint      NOT NULL,
    "start_time"   TIMESTAMP(3) NOT NULL                                          DEFAULT CURRENT_TIMESTAMP(3),
    "expire_time"  TIMESTAMP(3)                                                   DEFAULT NULL,
    "traffic"      bigint                                                        DEFAULT '0',
    "download"     bigint                                                        DEFAULT '0',
    "upload"       bigint                                                        DEFAULT '0',
    "token"        varchar(255)   DEFAULT '',
    "uuid"         varchar(255)   DEFAULT '',
    "status"       SMALLINT                                                    DEFAULT '0',
    "created_at"   TIMESTAMP(3)                                                   DEFAULT NULL,
    "updated_at"   TIMESTAMP(3)                                                   DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "uni_user_subscribe_token" UNIQUE ("token"),
    CONSTRAINT "uni_user_subscribe_uuid" UNIQUE ("uuid")
);
CREATE INDEX IF NOT EXISTS "user_subscribe_idx_user_id" ON "user_subscribe" ("user_id");
CREATE INDEX IF NOT EXISTS "user_subscribe_idx_order_id" ON "user_subscribe" ("order_id");
CREATE INDEX IF NOT EXISTS "user_subscribe_idx_subscribe_id" ON "user_subscribe" ("subscribe_id");
CREATE INDEX IF NOT EXISTS "user_subscribe_idx_token" ON "user_subscribe" ("token");
CREATE INDEX IF NOT EXISTS "user_subscribe_idx_uuid" ON "user_subscribe" ("uuid");
CREATE TABLE IF NOT EXISTS "user_subscribe_log"
(
    "id"                BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "user_id"           bigint                                                        NOT NULL,
    "user_subscribe_id" bigint                                                        NOT NULL,
    "token"             varchar(255)   NOT NULL,
    "ip"                varchar(255)   NOT NULL,
    "user_agent"        text           NOT NULL,
    "created_at"        TIMESTAMP(3) DEFAULT NULL,
    PRIMARY KEY ("id")
);
CREATE INDEX IF NOT EXISTS "user_subscribe_log_idx_user_id" ON "user_subscribe_log" ("user_id");
CREATE INDEX IF NOT EXISTS "user_subscribe_log_idx_user_subscribe_id" ON "user_subscribe_log" ("user_subscribe_id");
CREATE TABLE IF NOT EXISTS "server_rule_group"
(
    "id"          BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    "name"        varchar(100)   NOT NULL DEFAULT '',
    "icon"        text,
    "tags"        text,
    "description" varchar(255)            DEFAULT '',
    "enable"      BOOLEAN                                                    NOT NULL DEFAULT true,
    "created_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    "updated_at"  TIMESTAMP(3)                                                            DEFAULT NULL,
    PRIMARY KEY ("id"),
    CONSTRAINT "unique_name" UNIQUE ("name")
);

-- migrate:down
-- 000001_init_schema.down.sql
DROP TABLE IF EXISTS "user_subscribe_log";
DROP TABLE IF EXISTS "user_subscribe";
DROP TABLE IF EXISTS "user_login_log";
DROP TABLE IF EXISTS "user_gift_amount_log";
DROP TABLE IF EXISTS "user_device";
DROP TABLE IF EXISTS "user_commission_log";
DROP TABLE IF EXISTS "user_balance_log";
DROP TABLE IF EXISTS "user_auth_methods";
DROP TABLE IF EXISTS "user";
DROP TABLE IF EXISTS "traffic_log";
DROP TABLE IF EXISTS "ticket_follow";
DROP TABLE IF EXISTS "ticket";
DROP TABLE IF EXISTS "system";
DROP TABLE IF EXISTS "subscribe_type";
DROP TABLE IF EXISTS "subscribe_group";
DROP TABLE IF EXISTS "subscribe";
DROP TABLE IF EXISTS "sms";
DROP TABLE IF EXISTS "server_rule_group";
DROP TABLE IF EXISTS "server_group";
DROP TABLE IF EXISTS "server";
DROP TABLE IF EXISTS "payment";
DROP TABLE IF EXISTS "order";
DROP TABLE IF EXISTS "message_log";
DROP TABLE IF EXISTS "document";
DROP TABLE IF EXISTS "coupon";
DROP TABLE IF EXISTS "auth_method";
DROP TABLE IF EXISTS "application_version";
DROP TABLE IF EXISTS "application_config";
DROP TABLE IF EXISTS "application";
DROP TABLE IF EXISTS "announcement";
DROP TABLE IF EXISTS "ads";

