SET SESSION sql_mode = 'TRADITIONAL';
START TRANSACTION;

alter table extraFile
    modify origin enum ('qiniu', 'wechat', 'ctyun', 'aliyun', 'tencent', 'local', 'unknown', 's3') not null;

create table oauthApplication
(
    clientSecret      varchar(512)                 not null,
    systemId          char(36)                     not null,
    name              varchar(64)                  not null,
    description       text                         null,
    redirectUris      json                         null,
    logo              varchar(512)                 null,
    isConfidential    tinyint(1)                   not null,
    scopes            json                         null,
    ableState         enum ('enabled', 'disabled') null,
    id                char(36)                     not null
        primary key,
    `$$seq$$`         bigint auto_increment,
    `$$createAt$$`    bigint                       not null,
    `$$updateAt$$`    bigint                       not null,
    `$$deleteAt$$`    bigint                       null,
    `$$triggerData$$` json                         null,
    `$$triggerUuid$$` char(36)                     null,
    constraint `$$seq$$`
        unique (`$$seq$$`)
);

create index oauthApplication_create_at_auto_create
    on oauthApplication (`$$createAt$$`, `$$deleteAt$$`);

create index oauthApplication_fk_systemId_auto_create
    on oauthApplication (systemId, `$$deleteAt$$`);

create index oauthApplication_trigger_uuid
    on oauthApplication (`$$triggerUuid$$`, `$$deleteAt$$`);

create index oauthApplication_update_at_auto_create
    on oauthApplication (`$$updateAt$$`, `$$deleteAt$$`);

create table oauthAuthorizationCode
(
    code                varchar(128) not null,
    applicationId       char(36)     not null,
    oauthAppId          char(36)     not null,
    userId              char(36)     not null,
    redirectUri         varchar(512) not null,
    scope               json         not null,
    codeChallenge       varchar(128) null,
    codeChallengeMethod varchar(10)  null,
    expiresAt           bigint       not null,
    usedAt              bigint       null,
    id                  char(36)     not null
        primary key,
    `$$seq$$`           bigint auto_increment,
    `$$createAt$$`      bigint       not null,
    `$$updateAt$$`      bigint       not null,
    `$$deleteAt$$`      bigint       null,
    `$$triggerData$$`   json         null,
    `$$triggerUuid$$`   char(36)     null,
    constraint `$$seq$$`
        unique (`$$seq$$`)
);

create index oauthAuthorizationCode_create_at_auto_create
    on oauthAuthorizationCode (`$$createAt$$`, `$$deleteAt$$`);

create index oauthAuthorizationCode_fk_applicationId_auto_create
    on oauthAuthorizationCode (applicationId, `$$deleteAt$$`);

create index oauthAuthorizationCode_fk_oauthAppId_auto_create
    on oauthAuthorizationCode (oauthAppId, `$$deleteAt$$`);

create index oauthAuthorizationCode_fk_userId_auto_create
    on oauthAuthorizationCode (userId, `$$deleteAt$$`);

create index oauthAuthorizationCode_trigger_uuid
    on oauthAuthorizationCode (`$$triggerUuid$$`, `$$deleteAt$$`);

create index oauthAuthorizationCode_update_at_auto_create
    on oauthAuthorizationCode (`$$updateAt$$`, `$$deleteAt$$`);

create table oauthProvider
(
    systemId              char(36)                                                                                                                                                         not null,
    name                  varchar(64)                                                                                                                                                      not null,
    type                  enum ('oak', 'gitea', 'github', 'google', 'facebook', 'twitter', 'linkedin', 'custom', 'gitlab', 'microsoft', 'apple', 'tencent', 'weixin', 'weibo', 'dingtalk') not null,
    logo                  varchar(512)                                                                                                                                                     null,
    authorizationEndpoint varchar(512)                                                                                                                                                     not null,
    tokenEndpoint         varchar(512)                                                                                                                                                     not null,
    userInfoEndpoint      varchar(512)                                                                                                                                                     null,
    revokeEndpoint        varchar(512)                                                                                                                                                     null,
    refreshEndpoint       varchar(512)                                                                                                                                                     null,
    clientId              varchar(512)                                                                                                                                                     not null,
    clientSecret          varchar(512)                                                                                                                                                     not null,
    redirectUri           varchar(512)                                                                                                                                                     not null,
    scopes                json                                                                                                                                                             null,
    autoRegister          tinyint(1)                                                                                                                                                       not null,
    ableState             enum ('enabled', 'disabled')                                                                                                                                     null,
    id                    char(36)                                                                                                                                                         not null
        primary key,
    `$$seq$$`             bigint auto_increment,
    `$$createAt$$`        bigint                                                                                                                                                           not null,
    `$$updateAt$$`        bigint                                                                                                                                                           not null,
    `$$deleteAt$$`        bigint                                                                                                                                                           null,
    `$$triggerData$$`     json                                                                                                                                                             null,
    `$$triggerUuid$$`     char(36)                                                                                                                                                         null,
    constraint `$$seq$$`
        unique (`$$seq$$`)
);

create index oauthProvider_create_at_auto_create
    on oauthProvider (`$$createAt$$`, `$$deleteAt$$`);

create index oauthProvider_fk_systemId_auto_create
    on oauthProvider (systemId, `$$deleteAt$$`);

create index oauthProvider_trigger_uuid
    on oauthProvider (`$$triggerUuid$$`, `$$deleteAt$$`);

create index oauthProvider_update_at_auto_create
    on oauthProvider (`$$updateAt$$`, `$$deleteAt$$`);

create table oauthState
(
    state             varchar(32)            not null,
    type              enum ('login', 'bind') not null,
    providerId        char(36)               not null,
    userId            char(36)               null,
    usedAt            bigint                 null,
    id                char(36)               not null
        primary key,
    `$$seq$$`         bigint auto_increment,
    `$$createAt$$`    bigint                 not null,
    `$$updateAt$$`    bigint                 not null,
    `$$deleteAt$$`    bigint                 null,
    `$$triggerData$$` json                   null,
    `$$triggerUuid$$` char(36)               null,
    constraint `$$seq$$`
        unique (`$$seq$$`)
);

create index oauthState_create_at_auto_create
    on oauthState (`$$createAt$$`, `$$deleteAt$$`);

create index oauthState_fk_providerId_auto_create
    on oauthState (providerId, `$$deleteAt$$`);

create index oauthState_fk_userId_auto_create
    on oauthState (userId, `$$deleteAt$$`);

create index oauthState_trigger_uuid
    on oauthState (`$$triggerUuid$$`, `$$deleteAt$$`);

create index oauthState_update_at_auto_create
    on oauthState (`$$updateAt$$`, `$$deleteAt$$`);

create table oauthToken
(
    accessToken       varchar(1024) not null,
    refreshToken      varchar(1024) not null,
    userId            char(36)      not null,
    codeId            char(36)      not null,
    accessExpiresAt   bigint        not null,
    refreshExpiresAt  bigint        not null,
    revokedAt         bigint        null,
    lastUsedAt        bigint        null,
    id                char(36)      not null
        primary key,
    `$$seq$$`         bigint auto_increment,
    `$$createAt$$`    bigint        not null,
    `$$updateAt$$`    bigint        not null,
    `$$deleteAt$$`    bigint        null,
    `$$triggerData$$` json          null,
    `$$triggerUuid$$` char(36)      null,
    constraint `$$seq$$`
        unique (`$$seq$$`)
);

create index oauthToken_create_at_auto_create
    on oauthToken (`$$createAt$$`, `$$deleteAt$$`);

create index oauthToken_fk_codeId_auto_create
    on oauthToken (codeId, `$$deleteAt$$`);

create index oauthToken_fk_userId_auto_create
    on oauthToken (userId, `$$deleteAt$$`);

create index oauthToken_trigger_uuid
    on oauthToken (`$$triggerUuid$$`, `$$deleteAt$$`);

create index oauthToken_update_at_auto_create
    on oauthToken (`$$updateAt$$`, `$$deleteAt$$`);

create table oauthUser
(
    userId            char(36)      null,
    applicationId     char(36)      not null,
    providerConfigId  char(36)      not null,
    providerUserId    varchar(256)  not null,
    rawUserInfo       json          not null,
    accessToken       varchar(1024) not null,
    refreshToken      varchar(1024) null,
    accessExpiresAt   bigint        not null,
    refreshExpiresAt  bigint        null,
    stateId           char(36)      not null,
    id                char(36)      not null
        primary key,
    `$$seq$$`         bigint auto_increment,
    `$$createAt$$`    bigint        not null,
    `$$updateAt$$`    bigint        not null,
    `$$deleteAt$$`    bigint        null,
    `$$triggerData$$` json          null,
    `$$triggerUuid$$` char(36)      null,
    constraint `$$seq$$`
        unique (`$$seq$$`)
);

create index oauthUser_create_at_auto_create
    on oauthUser (`$$createAt$$`, `$$deleteAt$$`);

create index oauthUser_fk_applicationId_auto_create
    on oauthUser (applicationId, `$$deleteAt$$`);

create index oauthUser_fk_providerConfigId_auto_create
    on oauthUser (providerConfigId, `$$deleteAt$$`);

create index oauthUser_fk_stateId_auto_create
    on oauthUser (stateId, `$$deleteAt$$`);

create index oauthUser_fk_userId_auto_create
    on oauthUser (userId, `$$deleteAt$$`);

create index oauthUser_trigger_uuid
    on oauthUser (`$$triggerUuid$$`, `$$deleteAt$$`);

create index oauthUser_update_at_auto_create
    on oauthUser (`$$updateAt$$`, `$$deleteAt$$`);

create table oauthUserAuthorization
(
    userId            char(36)                                        not null,
    applicationId     char(36)                                        not null,
    authorizedAt      bigint                                          not null,
    codeId            char(36)                                        null,
    tokenId           char(36)                                        null,
    usageState        enum ('unused', 'granted', 'denied', 'revoked') null,
    id                char(36)                                        not null
        primary key,
    `$$seq$$`         bigint auto_increment,
    `$$createAt$$`    bigint                                          not null,
    `$$updateAt$$`    bigint                                          not null,
    `$$deleteAt$$`    bigint                                          null,
    `$$triggerData$$` json                                            null,
    `$$triggerUuid$$` char(36)                                        null,
    constraint `$$seq$$`
        unique (`$$seq$$`)
);

create index oauthUserAuthorization_create_at_auto_create
    on oauthUserAuthorization (`$$createAt$$`, `$$deleteAt$$`);

create index oauthUserAuthorization_fk_applicationId_auto_create
    on oauthUserAuthorization (applicationId, `$$deleteAt$$`);

create index oauthUserAuthorization_fk_codeId_auto_create
    on oauthUserAuthorization (codeId, `$$deleteAt$$`);

create index oauthUserAuthorization_fk_tokenId_auto_create
    on oauthUserAuthorization (tokenId, `$$deleteAt$$`);

create index oauthUserAuthorization_fk_userId_auto_create
    on oauthUserAuthorization (userId, `$$deleteAt$$`);

create index oauthUserAuthorization_trigger_uuid
    on oauthUserAuthorization (`$$triggerUuid$$`, `$$deleteAt$$`);

create index oauthUserAuthorization_update_at_auto_create
    on oauthUserAuthorization (`$$updateAt$$`, `$$deleteAt$$`);


COMMIT;