create type log_level_enum as enum ('error', 'warning', 'info', 'success', 'debug');

create type note_visibility_enum as enum ('public', 'home', 'followers', 'specified', 'hidden');

create type poll_notevisibility_enum as enum ('public', 'home', 'followers', 'specified');

create type page_visibility_enum as enum ('public', 'followers', 'specified');

create type antenna_src_enum as enum ('home', 'all', 'users', 'list', 'group', 'instances');

create type relay_status_enum as enum ('requesting', 'accepted', 'rejected');

create type muted_note_reason_enum as enum ('word', 'manual', 'spam', 'other');

create type user_profile_mutingnotificationtypes_enum as enum ('follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'pollEnded', 'receiveFollowRequest', 'followRequestAccepted', 'groupInvited', 'app');

create type user_profile_ffvisibility_enum as enum ('public', 'followers', 'private');

create type notification_type_enum as enum ('follow', 'mention', 'reply', 'renote', 'quote', 'reaction', 'pollVote', 'pollEnded', 'receiveFollowRequest', 'followRequestAccepted', 'groupInvited', 'app');

create type meta_sensitivemediadetection_enum as enum ('none', 'all', 'local', 'remote');

create type meta_sensitivemediadetectionsensitivity_enum as enum ('medium', 'low', 'high', 'veryLow', 'veryHigh');

create table if not exists drive_folder
(
    id          varchar(32)              not null
        constraint "PK_7a0c089191f5ebdc214e0af808a"
            primary key,
    "createdAt" timestamp with time zone not null,
    name        varchar(128)             not null,
    "userId"    varchar(32),
    "parentId"  varchar(32)
        constraint "FK_00ceffb0cdc238b3233294f08f2"
            references drive_folder
            on delete set null
);

comment on column drive_folder."createdAt" is 'The created date of the DriveFolder.';

comment on column drive_folder.name is 'The name of the DriveFolder.';

comment on column drive_folder."userId" is 'The owner ID.';

comment on column drive_folder."parentId" is 'The parent folder ID. If null, it means the DriveFolder is located in root.';

create index if not exists "IDX_02878d441ceae15ce060b73daf"
    on drive_folder ("createdAt");

create index if not exists "IDX_f4fc06e49c0171c85f1c48060d"
    on drive_folder ("userId");

create index if not exists "IDX_00ceffb0cdc238b3233294f08f"
    on drive_folder ("parentId");

create table if not exists drive_file
(
    id                   varchar(32)                 not null
        constraint "PK_43ddaaaf18c9e68029b7cbb032e"
            primary key,
    "createdAt"          timestamp with time zone    not null,
    "userId"             varchar(32),
    "userHost"           varchar(128),
    md5                  varchar(32)                 not null,
    name                 varchar(256)                not null,
    type                 varchar(128)                not null,
    size                 integer                     not null,
    comment              varchar(8192),
    properties           jsonb   default '{}'::jsonb not null,
    "storedInternal"     boolean                     not null,
    url                  varchar(512)                not null,
    "thumbnailUrl"       varchar(512),
    "webpublicUrl"       varchar(512),
    "accessKey"          varchar(256),
    "thumbnailAccessKey" varchar(256),
    "webpublicAccessKey" varchar(256),
    uri                  varchar(512),
    src                  varchar(512),
    "folderId"           varchar(32)
        constraint "FK_bb90d1956dafc4068c28aa7560a"
            references drive_folder
            on delete set null,
    "isSensitive"        boolean default false       not null,
    "isLink"             boolean default false       not null,
    blurhash             varchar(128),
    "webpublicType"      varchar(128),
    "requestHeaders"     jsonb   default '{}'::jsonb,
    "requestIp"          varchar(128),
    "maybeSensitive"     boolean default false       not null,
    "maybePorn"          boolean default false       not null
);

comment on column drive_file."createdAt" is 'The created date of the DriveFile.';

comment on column drive_file."userId" is 'The owner ID.';

comment on column drive_file."userHost" is 'The host of owner. It will be null if the user in local.';

comment on column drive_file.md5 is 'The MD5 hash of the DriveFile.';

comment on column drive_file.name is 'The file name of the DriveFile.';

comment on column drive_file.type is 'The content type (MIME) of the DriveFile.';

comment on column drive_file.size is 'The file size (bytes) of the DriveFile.';

comment on column drive_file.comment is 'The comment of the DriveFile.';

comment on column drive_file.properties is 'The any properties of the DriveFile. For example, it includes image width/height.';

comment on column drive_file.url is 'The URL of the DriveFile.';

comment on column drive_file."thumbnailUrl" is 'The URL of the thumbnail of the DriveFile.';

comment on column drive_file."webpublicUrl" is 'The URL of the webpublic of the DriveFile.';

comment on column drive_file.uri is 'The URI of the DriveFile. it will be null when the DriveFile is local.';

comment on column drive_file."folderId" is 'The parent folder ID. If null, it means the DriveFile is located in root.';

comment on column drive_file."isSensitive" is 'Whether the DriveFile is NSFW.';

comment on column drive_file."isLink" is 'Whether the DriveFile is direct link to remote server.';

comment on column drive_file.blurhash is 'The BlurHash string.';

comment on column drive_file."maybeSensitive" is 'Whether the DriveFile is NSFW. (predict)';

create index if not exists "IDX_c8dfad3b72196dd1d6b5db168a"
    on drive_file ("createdAt");

create index if not exists "IDX_860fa6f6c7df5bb887249fba22"
    on drive_file ("userId");

create index if not exists "IDX_92779627994ac79277f070c91e"
    on drive_file ("userHost");

create index if not exists "IDX_37bb9a1b4585f8a3beb24c62d6"
    on drive_file (md5);

create index if not exists "IDX_a40b8df8c989d7db937ea27cf6"
    on drive_file (type);

create unique index if not exists "IDX_d85a184c2540d2deba33daf642"
    on drive_file ("accessKey");

create unique index if not exists "IDX_e74022ce9a074b3866f70e0d27"
    on drive_file ("thumbnailAccessKey");

create unique index if not exists "IDX_c55b2b7c284d9fef98026fc88e"
    on drive_file ("webpublicAccessKey");

create index if not exists "IDX_e5848eac4940934e23dbc17581"
    on drive_file (uri);

create index if not exists "IDX_bb90d1956dafc4068c28aa7560"
    on drive_file ("folderId");

create index if not exists "IDX_55720b33a61a7c806a8215b825"
    on drive_file ("userId", "folderId", id);

create index if not exists "IDX_a7eba67f8b3fa27271e85d2e26"
    on drive_file ("isSensitive");

create index if not exists "IDX_315c779174fe8247ab324f036e"
    on drive_file ("isLink");

create index if not exists "IDX_3b33dff77bb64b23c88151d23e"
    on drive_file ("maybeSensitive");

create index if not exists "IDX_8bdcd3dd2bddb78014999a16ce"
    on drive_file ("maybePorn");

create table if not exists "user"
(
    id                        varchar(32)                                      not null
        constraint "PK_cace4a159ff9f2512dd42373760"
            primary key,
    "createdAt"               timestamp with time zone                         not null,
    "updatedAt"               timestamp with time zone,
    "lastFetchedAt"           timestamp with time zone,
    username                  varchar(128)                                     not null,
    "usernameLower"           varchar(128)                                     not null,
    name                      varchar(128),
    "followersCount"          integer        default 0                         not null,
    "followingCount"          integer        default 0                         not null,
    "notesCount"              integer        default 0                         not null,
    "avatarId"                varchar(32)
        constraint "REL_58f5c71eaab331645112cf8cfa"
            unique
        constraint "FK_58f5c71eaab331645112cf8cfa5"
            references drive_file
            on delete set null,
    "bannerId"                varchar(32)
        constraint "REL_afc64b53f8db3707ceb34eb28e"
            unique
        constraint "FK_afc64b53f8db3707ceb34eb28e2"
            references drive_file
            on delete set null,
    tags                      varchar(128)[] default '{}'::character varying[] not null,
    "isSuspended"             boolean        default false                     not null,
    "isSilenced"              boolean        default false                     not null,
    "isLocked"                boolean        default false                     not null,
    "isBot"                   boolean        default false                     not null,
    "isCat"                   boolean        default false                     not null,
    "isAdmin"                 boolean        default false                     not null,
    "isModerator"             boolean        default false                     not null,
    emojis                    varchar(128)[] default '{}'::character varying[] not null,
    host                      varchar(128),
    inbox                     varchar(512),
    "sharedInbox"             varchar(512),
    featured                  varchar(512),
    uri                       varchar(512),
    token                     char(16)
        constraint "UQ_a854e557b1b14814750c7c7b0c9"
            unique,
    "isExplorable"            boolean        default true                      not null,
    "followersUri"            varchar(512),
    "lastActiveDate"          timestamp with time zone,
    "hideOnlineStatus"        boolean        default false                     not null,
    "isDeleted"               boolean        default false                     not null,
    "driveCapacityOverrideMb" integer,
    "movedToUri"              varchar(512),
    "alsoKnownAs"             text,
    "speakAsCat"              boolean        default true                      not null
);

comment on column "user"."createdAt" is 'The created date of the User.';

comment on column "user"."updatedAt" is 'The updated date of the User.';

comment on column "user".username is 'The username of the User.';

comment on column "user"."usernameLower" is 'The username (lowercased) of the User.';

comment on column "user".name is 'The name of the User.';

comment on column "user"."followersCount" is 'The count of followers.';

comment on column "user"."followingCount" is 'The count of following.';

comment on column "user"."notesCount" is 'The count of notes.';

comment on column "user"."avatarId" is 'The ID of avatar DriveFile.';

comment on column "user"."bannerId" is 'The ID of banner DriveFile.';

comment on column "user"."isSuspended" is 'Whether the User is suspended.';

comment on column "user"."isSilenced" is 'Whether the User is silenced.';

comment on column "user"."isLocked" is 'Whether the User is locked.';

comment on column "user"."isBot" is 'Whether the User is a bot.';

comment on column "user"."isCat" is 'Whether the User is a cat.';

comment on column "user"."isAdmin" is 'Whether the User is the admin.';

comment on column "user"."isModerator" is 'Whether the User is a moderator.';

comment on column "user".host is 'The host of the User. It will be null if the origin of the user is local.';

comment on column "user".inbox is 'The inbox URL of the User. It will be null if the origin of the user is local.';

comment on column "user"."sharedInbox" is 'The sharedInbox URL of the User. It will be null if the origin of the user is local.';

comment on column "user".featured is 'The featured URL of the User. It will be null if the origin of the user is local.';

comment on column "user".uri is 'The URI of the User. It will be null if the origin of the user is local.';

comment on column "user".token is 'The native access token of the User. It will be null if the origin of the user is local.';

comment on column "user"."isExplorable" is 'Whether the User is explorable.';

comment on column "user"."followersUri" is 'The URI of the user Follower Collection. It will be null if the origin of the user is local.';

comment on column "user"."isDeleted" is 'Whether the User is deleted.';

comment on column "user"."driveCapacityOverrideMb" is 'Overrides user drive capacity limit';

comment on column "user"."movedToUri" is 'The URI of the new account of the User';

comment on column "user"."alsoKnownAs" is 'URIs the user is known as too';

comment on column "user"."speakAsCat" is 'Whether to speak as a cat if isCat.';

alter table drive_folder
    add constraint "FK_f4fc06e49c0171c85f1c48060d2"
        foreign key ("userId") references "user"
            on delete cascade;


alter table drive_file
    add constraint "FK_860fa6f6c7df5bb887249fba22e"
        foreign key ("userId") references "user"
            on delete set null;

create index if not exists "IDX_e11e649824a45d8ed01d597fd9"
    on "user" ("createdAt");

create index if not exists "IDX_80ca6e6ef65fb9ef34ea8c90f4"
    on "user" ("updatedAt");

create index if not exists "IDX_a27b942a0d6dcff90e3ee9b5e8"
    on "user" ("usernameLower");

create index if not exists "IDX_fa99d777623947a5b05f394cae"
    on "user" (tags);

create index if not exists "IDX_3252a5df8d5bbd16b281f7799e"
    on "user" (host);

create index if not exists "IDX_be623adaa4c566baf5d29ce0c8"
    on "user" (uri);

create unique index if not exists "IDX_a854e557b1b14814750c7c7b0c"
    on "user" (token);

create unique index if not exists "IDX_5deb01ae162d1d70b80d064c27"
    on "user" ("usernameLower", host);

create index if not exists "IDX_d5a1b83c7cab66f167e6888188"
    on "user" ("isExplorable");

create index if not exists "IDX_c8cc87bd0f2f4487d17c651fbf"
    on "user" ("lastActiveDate");

create table if not exists app
(
    id            varchar(32)              not null
        constraint "PK_9478629fc093d229df09e560aea"
            primary key,
    "createdAt"   timestamp with time zone not null,
    "userId"      varchar(32)
        constraint "FK_3f5b0899ef90527a3462d7c2cb3"
            references "user"
            on delete set null,
    secret        varchar(64)              not null,
    name          varchar(128)             not null,
    description   varchar(512)             not null,
    permission    varchar(64)[]            not null,
    "callbackUrl" varchar(512)
);

comment on column app."createdAt" is 'The created date of the App.';

comment on column app."userId" is 'The owner ID.';

comment on column app.secret is 'The secret key of the App.';

comment on column app.name is 'The name of the App.';

comment on column app.description is 'The description of the App.';

comment on column app.permission is 'The permission of the App.';

comment on column app."callbackUrl" is 'The callbackUrl of the App.';

create index if not exists "IDX_048a757923ed8b157e9895da53"
    on app ("createdAt");

create index if not exists "IDX_3f5b0899ef90527a3462d7c2cb"
    on app ("userId");

create index if not exists "IDX_f49922d511d666848f250663c4"
    on app (secret);

create table if not exists access_token
(
    id           varchar(32)                                     not null
        constraint "PK_f20f028607b2603deabd8182d12"
            primary key,
    "createdAt"  timestamp with time zone                        not null,
    token        varchar(128)                                    not null,
    hash         varchar(128)                                    not null,
    "userId"     varchar(32)                                     not null
        constraint "FK_9949557d0e1b2c19e5344c171e9"
            references "user"
            on delete cascade,
    "appId"      varchar(32)
        constraint "FK_a3ff16c90cc87a82a0b5959e560"
            references app
            on delete cascade,
    "lastUsedAt" timestamp with time zone,
    session      varchar(128),
    name         varchar(128),
    description  varchar(512),
    "iconUrl"    varchar(512),
    permission   varchar(64)[] default '{}'::character varying[] not null,
    fetched      boolean       default false                     not null
);

comment on column access_token."createdAt" is 'The created date of the AccessToken.';

create index if not exists "IDX_70ba8f6af34bc924fc9e12adb8"
    on access_token (token);

create index if not exists "IDX_64c327441248bae40f7d92f34f"
    on access_token (hash);

create index if not exists "IDX_9949557d0e1b2c19e5344c171e"
    on access_token ("userId");

create index if not exists "IDX_bf3a053c07d9fb5d87317c56ee"
    on access_token (session);

create table if not exists meta
(
    id                                       varchar(32)                                                                                                                      not null
        constraint "PK_c4c17a6c2bd7651338b60fc590b"
            primary key,
    name                                     varchar(128),
    description                              varchar(1024),
    "maintainerName"                         varchar(128),
    "maintainerEmail"                        varchar(128),
    "disableRegistration"                    boolean                                      default false                                                                       not null,
    "disableLocalTimeline"                   boolean                                      default false                                                                       not null,
    "disableGlobalTimeline"                  boolean                                      default false                                                                       not null,
    "useStarForReactionFallback"             boolean                                      default false                                                                       not null,
    langs                                    varchar(64)[]                                default '{}'::character varying[]                                                   not null,
    "hiddenTags"                             varchar(256)[]                               default '{}'::character varying[]                                                   not null,
    "blockedHosts"                           varchar(256)[]                               default '{}'::character varying[]                                                   not null,
    "mascotImageUrl"                         varchar(512)                                 default '/assets/ai.png'::character varying,
    "bannerUrl"                              varchar(512),
    "errorImageUrl"                          varchar(512)                                 default 'https://xn--931a.moe/aiart/yubitun.png'::character varying,
    "iconUrl"                                varchar(512),
    "cacheRemoteFiles"                       boolean                                      default true                                                                        not null,
    "enableRecaptcha"                        boolean                                      default false                                                                       not null,
    "recaptchaSiteKey"                       varchar(64),
    "recaptchaSecretKey"                     varchar(64),
    "localDriveCapacityMb"                   integer                                      default 1024                                                                        not null,
    "remoteDriveCapacityMb"                  integer                                      default 32                                                                          not null,
    "summalyProxy"                           varchar(128),
    "enableEmail"                            boolean                                      default false                                                                       not null,
    email                                    varchar(128),
    "smtpSecure"                             boolean                                      default false                                                                       not null,
    "smtpHost"                               varchar(128),
    "smtpPort"                               integer,
    "smtpUser"                               varchar(128),
    "smtpPass"                               varchar(128),
    "enableServiceWorker"                    boolean                                      default false                                                                       not null,
    "swPublicKey"                            varchar(128),
    "swPrivateKey"                           varchar(128),
    "enableTwitterIntegration"               boolean                                      default false                                                                       not null,
    "twitterConsumerKey"                     varchar(128),
    "twitterConsumerSecret"                  varchar(128),
    "enableGithubIntegration"                boolean                                      default false                                                                       not null,
    "githubClientId"                         varchar(128),
    "githubClientSecret"                     varchar(128),
    "enableDiscordIntegration"               boolean                                      default false                                                                       not null,
    "discordClientId"                        varchar(128),
    "discordClientSecret"                    varchar(128),
    "pinnedUsers"                            varchar(256)[]                               default '{}'::character varying[]                                                   not null,
    "ToSUrl"                                 varchar(512),
    "repositoryUrl"                          varchar(512)                                 default 'https://github.com/misskey-dev/misskey'::character varying                 not null,
    "feedbackUrl"                            varchar(512)                                 default 'https://github.com/misskey-dev/misskey/issues/new'::character varying,
    "useObjectStorage"                       boolean                                      default false                                                                       not null,
    "objectStorageBucket"                    varchar(512),
    "objectStoragePrefix"                    varchar(512),
    "objectStorageBaseUrl"                   varchar(512),
    "objectStorageEndpoint"                  varchar(512),
    "objectStorageRegion"                    varchar(512),
    "objectStorageAccessKey"                 varchar(512),
    "objectStorageSecretKey"                 varchar(512),
    "objectStoragePort"                      integer,
    "objectStorageUseSSL"                    boolean                                      default true                                                                        not null,
    "proxyAccountId"                         varchar(32)
        constraint "FK_ab1bc0c1e209daa77b8e8d212ad"
            references "user"
            on delete set null,
    "objectStorageUseProxy"                  boolean                                      default true                                                                        not null,
    "enableHcaptcha"                         boolean                                      default false                                                                       not null,
    "hcaptchaSiteKey"                        varchar(64),
    "hcaptchaSecretKey"                      varchar(64),
    "objectStorageSetPublicRead"             boolean                                      default false                                                                       not null,
    "pinnedPages"                            varchar(512)[]                               default '{/featured,/channels,/explore,/pages,/about-misskey}'::character varying[] not null,
    "backgroundImageUrl"                     varchar(512),
    "logoImageUrl"                           varchar(512),
    "pinnedClipId"                           varchar(32),
    "objectStorageS3ForcePathStyle"          boolean                                      default true                                                                        not null,
    "allowedHosts"                           varchar(256)[]                               default '{}'::character varying[],
    "secureMode"                             boolean                                      default false,
    "privateMode"                            boolean                                      default false,
    "deeplAuthKey"                           varchar(128),
    "deeplIsPro"                             boolean                                      default false                                                                       not null,
    "emailRequiredForSignup"                 boolean                                      default false                                                                       not null,
    "themeColor"                             varchar(512),
    "defaultLightTheme"                      varchar(8192),
    "defaultDarkTheme"                       varchar(8192),
    "sensitiveMediaDetection"                meta_sensitivemediadetection_enum            default 'none'::meta_sensitivemediadetection_enum                                   not null,
    "sensitiveMediaDetectionSensitivity"     meta_sensitivemediadetectionsensitivity_enum default 'medium'::meta_sensitivemediadetectionsensitivity_enum                      not null,
    "setSensitiveFlagAutomatically"          boolean                                      default false                                                                       not null,
    "enableIpLogging"                        boolean                                      default false                                                                       not null,
    "enableSensitiveMediaDetectionForVideos" boolean                                      default false                                                                       not null,
    "enableActiveEmailValidation"            boolean                                      default true                                                                        not null,
    "customMOTD"                             varchar(256)[]                               default '{}'::character varying[]                                                   not null,
    "customSplashIcons"                      varchar(256)[]                               default '{}'::character varying[]                                                   not null,
    "disableRecommendedTimeline"             boolean                                      default true                                                                        not null,
    "recommendedInstances"                   varchar(256)[]                               default '{}'::character varying[]                                                   not null,
    "enableGuestTimeline"                    boolean                                      default false                                                                       not null,
    "defaultReaction"                        varchar(256)                                 default '⭐'::character varying                                                      not null,
    "libreTranslateApiUrl"                   varchar(512),
    "libreTranslateApiKey"                   varchar(128),
    "silencedHosts"                          varchar(256)[]                               default '{}'::character varying[]                                                   not null,
    "experimentalFeatures"                   jsonb                                        default '{}'::jsonb                                                                 not null
);

comment on column meta."localDriveCapacityMb" is 'Drive capacity of a local user (MB)';

comment on column meta."remoteDriveCapacityMb" is 'Drive capacity of a remote user (MB)';

comment on column meta."defaultReaction" is 'The fallback reaction for emoji reacts';

create table if not exists following
(
    id                    varchar(32)              not null
        constraint "PK_c76c6e044bdf76ecf8bfb82a645"
            primary key,
    "createdAt"           timestamp with time zone not null,
    "followeeId"          varchar(32)              not null
        constraint "FK_24e0042143a18157b234df186c3"
            references "user"
            on delete cascade,
    "followerId"          varchar(32)              not null
        constraint "FK_6516c5a6f3c015b4eed39978be5"
            references "user"
            on delete cascade,
    "followerHost"        varchar(128),
    "followerInbox"       varchar(512),
    "followerSharedInbox" varchar(512),
    "followeeHost"        varchar(128),
    "followeeInbox"       varchar(512),
    "followeeSharedInbox" varchar(512)
);

comment on column following."createdAt" is 'The created date of the Following.';

comment on column following."followeeId" is 'The followee user ID.';

comment on column following."followerId" is 'The follower user ID.';

comment on column following."followerHost" is '[Denormalized]';

comment on column following."followerInbox" is '[Denormalized]';

comment on column following."followerSharedInbox" is '[Denormalized]';

comment on column following."followeeHost" is '[Denormalized]';

comment on column following."followeeInbox" is '[Denormalized]';

comment on column following."followeeSharedInbox" is '[Denormalized]';

create index if not exists "IDX_582f8fab771a9040a12961f3e7"
    on following ("createdAt");

create index if not exists "IDX_24e0042143a18157b234df186c"
    on following ("followeeId");

create index if not exists "IDX_6516c5a6f3c015b4eed39978be"
    on following ("followerId");

create unique index if not exists "IDX_307be5f1d1252e0388662acb96"
    on following ("followerId", "followeeId");

create index if not exists "IDX_4ccd2239268ebbd1b35e318754"
    on following ("followerHost");

create index if not exists "IDX_fcdafee716dfe9c3b5fde90f30"
    on following ("followeeHost");

create table if not exists instance
(
    id                        varchar(32)              not null
        constraint "PK_eaf60e4a0c399c9935413e06474"
            primary key,
    "caughtAt"                timestamp with time zone not null,
    host                      varchar(128)             not null,
    "usersCount"              integer default 0        not null,
    "notesCount"              integer default 0        not null,
    "followingCount"          integer default 0        not null,
    "followersCount"          integer default 0        not null,
    "latestRequestSentAt"     timestamp with time zone,
    "latestStatus"            integer,
    "latestRequestReceivedAt" timestamp with time zone,
    "lastCommunicatedAt"      timestamp with time zone not null,
    "isNotResponding"         boolean default false    not null,
    "softwareName"            varchar(64),
    "softwareVersion"         varchar(64),
    "openRegistrations"       boolean,
    name                      varchar(256),
    description               varchar(4096),
    "maintainerName"          varchar(128),
    "maintainerEmail"         varchar(256),
    "infoUpdatedAt"           timestamp with time zone,
    "isSuspended"             boolean default false    not null,
    "iconUrl"                 varchar(256),
    "themeColor"              varchar(64),
    "faviconUrl"              varchar(256)
);

comment on column instance."caughtAt" is 'The caught date of the Instance.';

comment on column instance.host is 'The host of the Instance.';

comment on column instance."usersCount" is 'The count of the users of the Instance.';

comment on column instance."notesCount" is 'The count of the notes of the Instance.';

comment on column instance."softwareName" is 'The software of the Instance.';

create index if not exists "IDX_2cd3b2a6b4cf0b910b260afe08"
    on instance ("caughtAt");

create unique index if not exists "IDX_8d5afc98982185799b160e10eb"
    on instance (host);

create index if not exists "IDX_34500da2e38ac393f7bb6b299c"
    on instance ("isSuspended");

create table if not exists muting
(
    id          varchar(32)              not null
        constraint "PK_2e92d06c8b5c602eeb27ca9ba48"
            primary key,
    "createdAt" timestamp with time zone not null,
    "muteeId"   varchar(32)              not null
        constraint "FK_ec96b4fed9dae517e0dbbe0675c"
            references "user"
            on delete cascade,
    "muterId"   varchar(32)              not null
        constraint "FK_93060675b4a79a577f31d260c67"
            references "user"
            on delete cascade,
    "expiresAt" timestamp with time zone
);

comment on column muting."createdAt" is 'The created date of the Muting.';

comment on column muting."muteeId" is 'The mutee user ID.';

comment on column muting."muterId" is 'The muter user ID.';

create index if not exists "IDX_f86d57fbca33c7a4e6897490cc"
    on muting ("createdAt");

create index if not exists "IDX_ec96b4fed9dae517e0dbbe0675"
    on muting ("muteeId");

create index if not exists "IDX_93060675b4a79a577f31d260c6"
    on muting ("muterId");

create unique index if not exists "IDX_1eb9d9824a630321a29fd3b290"
    on muting ("muterId", "muteeId");

create index if not exists "IDX_c1fd1c3dfb0627aa36c253fd14"
    on muting ("expiresAt");

create index if not exists "IDX_renote_muting_createdAt"
    on muting ("createdAt");

create index if not exists "IDX_renote_muting_muteeId"
    on muting ("muteeId");

create index if not exists "IDX_renote_muting_muterId"
    on muting ("muterId");

create table if not exists sw_subscription
(
    id                varchar(32)              not null
        constraint "PK_e8f763631530051b95eb6279b91"
            primary key,
    "createdAt"       timestamp with time zone not null,
    "userId"          varchar(32)              not null
        constraint "FK_97754ca6f2baff9b4abb7f853dd"
            references "user"
            on delete cascade,
    endpoint          varchar(512)             not null,
    auth              varchar(256)             not null,
    publickey         varchar(128)             not null,
    "sendReadMessage" boolean default false    not null
);

create index if not exists "IDX_97754ca6f2baff9b4abb7f853d"
    on sw_subscription ("userId");

create table if not exists blocking
(
    id          varchar(32)              not null
        constraint "PK_e5d9a541cc1965ee7e048ea09dd"
            primary key,
    "createdAt" timestamp with time zone not null,
    "blockeeId" varchar(32)              not null
        constraint "FK_2cd4a2743a99671308f5417759e"
            references "user"
            on delete cascade,
    "blockerId" varchar(32)              not null
        constraint "FK_0627125f1a8a42c9a1929edb552"
            references "user"
            on delete cascade
);

comment on column blocking."createdAt" is 'The created date of the Blocking.';

comment on column blocking."blockeeId" is 'The blockee user ID.';

comment on column blocking."blockerId" is 'The blocker user ID.';

create index if not exists "IDX_b9a354f7941c1e779f3b33aea6"
    on blocking ("createdAt");

create index if not exists "IDX_2cd4a2743a99671308f5417759"
    on blocking ("blockeeId");

create index if not exists "IDX_0627125f1a8a42c9a1929edb55"
    on blocking ("blockerId");

create unique index if not exists "IDX_98a1bc5cb30dfd159de056549f"
    on blocking ("blockerId", "blockeeId");

create table if not exists user_list
(
    id          varchar(32)              not null
        constraint "PK_87bab75775fd9b1ff822b656402"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null

        constraint "FK_b7fcefbdd1c18dce86687531f99"
            references "user"
            on delete cascade,
    name        varchar(128)             not null
);

comment on column user_list."createdAt" is 'The created date of the UserList.';

comment on column user_list."userId" is 'The owner ID.';

comment on column user_list.name is 'The name of the UserList.';

create index if not exists "IDX_b7fcefbdd1c18dce86687531f9"
    on user_list ("userId");

create table if not exists user_list_joining
(
    id           varchar(32)              not null
        constraint "PK_11abb3768da1c5f8de101c9df45"
            primary key,
    "createdAt"  timestamp with time zone not null,
    "userId"     varchar(32)              not null
        constraint "FK_d844bfc6f3f523a05189076efaa"
            references "user"
            on delete cascade,
    "userListId" varchar(32)              not null
        constraint "FK_605472305f26818cc93d1baaa74"
            references user_list
            on delete cascade
);

comment on column user_list_joining."createdAt" is 'The created date of the UserListJoining.';

comment on column user_list_joining."userId" is 'The user ID.';

comment on column user_list_joining."userListId" is 'The list ID.';

create index if not exists "IDX_d844bfc6f3f523a05189076efa"
    on user_list_joining ("userId");

create index if not exists "IDX_605472305f26818cc93d1baaa7"
    on user_list_joining ("userListId");

create unique index if not exists "IDX_90f7da835e4c10aca6853621e1"
    on user_list_joining ("userId", "userListId");

create table if not exists hashtag
(
    id                          varchar(32)       not null
        constraint "PK_cb36eb8af8412bfa978f1165d78"
            primary key,
    name                        varchar(128)      not null,
    "mentionedUserIds"          varchar(32)[]     not null,
    "mentionedUsersCount"       integer default 0 not null,
    "mentionedLocalUserIds"     varchar(32)[]     not null,
    "mentionedLocalUsersCount"  integer default 0 not null,
    "mentionedRemoteUserIds"    varchar(32)[]     not null,
    "mentionedRemoteUsersCount" integer default 0 not null,
    "attachedUserIds"           varchar(32)[]     not null,
    "attachedUsersCount"        integer default 0 not null,
    "attachedLocalUserIds"      varchar(32)[]     not null,
    "attachedLocalUsersCount"   integer default 0 not null,
    "attachedRemoteUserIds"     varchar(32)[]     not null,
    "attachedRemoteUsersCount"  integer default 0 not null
);

create unique index if not exists "IDX_347fec870eafea7b26c8a73bac"
    on hashtag (name);

create index if not exists "IDX_2710a55f826ee236ea1a62698f"
    on hashtag ("mentionedUsersCount");

create index if not exists "IDX_0e206cec573f1edff4a3062923"
    on hashtag ("mentionedLocalUsersCount");

create index if not exists "IDX_4c02d38a976c3ae132228c6fce"
    on hashtag ("mentionedRemoteUsersCount");

create index if not exists "IDX_d57f9030cd3af7f63ffb1c267c"
    on hashtag ("attachedUsersCount");

create index if not exists "IDX_0c44bf4f680964145f2a68a341"
    on hashtag ("attachedLocalUsersCount");

create index if not exists "IDX_0b03cbcd7e6a7ce068efa8ecc2"
    on hashtag ("attachedRemoteUsersCount");

create table if not exists abuse_user_report
(
    id               varchar(32)              not null
        constraint "PK_87873f5f5cc5c321a1306b2d18c"
            primary key,
    "createdAt"      timestamp with time zone not null,
    "targetUserId"   varchar(32)              not null
        constraint fk_7f4e851a35d81b64dda28eee0
            references "user"
            on delete cascade,
    "reporterId"     varchar(32)              not null
        constraint "FK_04cc96756f89d0b7f9473e8cdf3"
            references "user"
            on delete cascade,
    "assigneeId"     varchar(32)
        constraint "FK_08b883dd5fdd6f9c4c1572b36de"
            references "user"
            on delete set null,
    resolved         boolean default false    not null,
    comment          varchar(2048)            not null,
    "targetUserHost" varchar(128),
    "reporterHost"   varchar(128),
    forwarded        boolean default false    not null
);

comment on column abuse_user_report."createdAt" is 'The created date of the AbuseUserReport.';

comment on column abuse_user_report."targetUserHost" is '[Denormalized]';

comment on column abuse_user_report."reporterHost" is '[Denormalized]';

create index if not exists "IDX_db2098070b2b5a523c58181f74"
    on abuse_user_report ("createdAt");

create index if not exists "IDX_04cc96756f89d0b7f9473e8cdf"
    on abuse_user_report ("reporterId");

create index if not exists "IDX_2b15aaf4a0dc5be3499af7ab6a"
    on abuse_user_report (resolved);

create index if not exists "IDX_4ebbf7f93cdc10e8d1ef2fc6cd"
    on abuse_user_report ("targetUserHost");

create index if not exists "IDX_f8d8b93740ad12c4ce8213a199"
    on abuse_user_report ("reporterHost");

create table if not exists registration_ticket
(
    id          varchar(32)              not null
        constraint "PK_f11696b6fafcf3662d4292734f8"
            primary key,
    "createdAt" timestamp with time zone not null,
    code        varchar(64)              not null
);

create unique index if not exists "IDX_0ff69e8dfa9fe31bb4a4660f59"
    on registration_ticket (code);

create table if not exists signin
(
    id          varchar(32)              not null
        constraint "PK_9e96ddc025712616fc492b3b588"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_2c308dbdc50d94dc625670055f7"
            references "user"
            on delete cascade,
    ip          varchar(128)             not null,
    headers     jsonb                    not null,
    success     boolean                  not null
);

comment on column signin."createdAt" is 'The created date of the Signin.';

create index if not exists "IDX_2c308dbdc50d94dc625670055f"
    on signin ("userId");

create table if not exists auth_session
(
    id          varchar(32)              not null
        constraint "PK_19354ed146424a728c1112a8cbf"
            primary key,
    "createdAt" timestamp with time zone not null,
    token       varchar(128)             not null,
    "userId"    varchar(32)
        constraint "FK_c072b729d71697f959bde66ade0"
            references "user"
            on delete cascade,
    "appId"     varchar(32)              not null
        constraint "FK_dbe037d4bddd17b03a1dc778dee"
            references app
            on delete cascade
);

comment on column auth_session."createdAt" is 'The created date of the AuthSession.';

create index if not exists "IDX_62cb09e1129f6ec024ef66e183"
    on auth_session (token);

create table if not exists follow_request
(
    id                    varchar(32)              not null
        constraint "PK_53a9aa3725f7a3deb150b39dbfc"
            primary key,
    "createdAt"           timestamp with time zone not null,
    "followeeId"          varchar(32)              not null
        constraint "FK_12c01c0d1a79f77d9f6c15fadd2"
            references "user"
            on delete cascade,
    "followerId"          varchar(32)              not null
        constraint "FK_a7fd92dd6dc519e6fb435dd108f"
            references "user"
            on delete cascade,
    "requestId"           varchar(128),
    "followerHost"        varchar(128),
    "followerInbox"       varchar(512),
    "followerSharedInbox" varchar(512),
    "followeeHost"        varchar(128),
    "followeeInbox"       varchar(512),
    "followeeSharedInbox" varchar(512)
);

comment on column follow_request."createdAt" is 'The created date of the FollowRequest.';

comment on column follow_request."followeeId" is 'The followee user ID.';

comment on column follow_request."followerId" is 'The follower user ID.';

comment on column follow_request."requestId" is 'id of Follow Activity.';

comment on column follow_request."followerHost" is '[Denormalized]';

comment on column follow_request."followerInbox" is '[Denormalized]';

comment on column follow_request."followerSharedInbox" is '[Denormalized]';

comment on column follow_request."followeeHost" is '[Denormalized]';

comment on column follow_request."followeeInbox" is '[Denormalized]';

comment on column follow_request."followeeSharedInbox" is '[Denormalized]';

create index if not exists "IDX_12c01c0d1a79f77d9f6c15fadd"
    on follow_request ("followeeId");

create index if not exists "IDX_a7fd92dd6dc519e6fb435dd108"
    on follow_request ("followerId");

create unique index if not exists "IDX_d54a512b822fac7ed52800f6b4"
    on follow_request ("followerId", "followeeId");

create table if not exists emoji
(
    id            varchar(32)                                      not null
        constraint "PK_df74ce05e24999ee01ea0bc50a3"
            primary key,
    "updatedAt"   timestamp with time zone,
    name          varchar(128)                                     not null,
    host          varchar(128),
    "originalUrl" varchar(512)                                     not null,
    uri           varchar(512),
    type          varchar(64),
    aliases       varchar(128)[] default '{}'::character varying[] not null,
    category      varchar(128),
    "publicUrl"   varchar(512)   default ''::character varying     not null,
    license       varchar(1024),
    width         integer,
    height        integer
);

comment on column emoji.width is 'Image width';

comment on column emoji.height is 'Image height';

create index if not exists "IDX_b37dafc86e9af007e3295c2781"
    on emoji (name);

create index if not exists "IDX_5900e907bb46516ddf2871327c"
    on emoji (host);

create unique index if not exists "IDX_4f4d35e1256c84ae3d1f0eab10"
    on emoji (name, host);

create table if not exists user_keypair
(
    "userId"     varchar(32)   not null
        constraint "PK_f4853eb41ab722fe05f81cedeb6"
            primary key
        constraint "UQ_f4853eb41ab722fe05f81cedeb6"
            unique
        constraint "FK_f4853eb41ab722fe05f81cedeb6"
            references "user"
            on delete cascade,
    "publicKey"  varchar(4096) not null,
    "privateKey" varchar(4096) not null
);

create table if not exists user_publickey
(
    "userId" varchar(32)   not null
        constraint "PK_10c146e4b39b443ede016f6736d"
            primary key
        constraint "UQ_10c146e4b39b443ede016f6736d"
            unique
        constraint "FK_10c146e4b39b443ede016f6736d"
            references "user"
            on delete cascade,
    "keyId"  varchar(256)  not null,
    "keyPem" varchar(4096) not null
);

create unique index if not exists "IDX_171e64971c780ebd23fae140bb"
    on user_publickey ("keyId");

create table if not exists __chart__active_users
(
    id                                     serial
        constraint "PK_317237a9f733b970604a11e314f"
            primary key,
    date                                   integer                                               not null
        constraint "UQ_0ad37b7ef50f4ddc84363d7ccca"
            unique,
    "unique_temp___registeredWithinWeek"   character varying[] default '{}'::character varying[] not null,
    "___registeredWithinWeek"              smallint            default '0'::smallint             not null,
    "unique_temp___registeredWithinMonth"  character varying[] default '{}'::character varying[] not null,
    "___registeredWithinMonth"             smallint            default '0'::smallint             not null,
    "unique_temp___registeredWithinYear"   character varying[] default '{}'::character varying[] not null,
    "___registeredWithinYear"              smallint            default '0'::smallint             not null,
    "unique_temp___registeredOutsideWeek"  character varying[] default '{}'::character varying[] not null,
    "___registeredOutsideWeek"             smallint            default '0'::smallint             not null,
    "unique_temp___registeredOutsideMonth" character varying[] default '{}'::character varying[] not null,
    "___registeredOutsideMonth"            smallint            default '0'::smallint             not null,
    "unique_temp___registeredOutsideYear"  character varying[] default '{}'::character varying[] not null,
    "___registeredOutsideYear"             smallint            default '0'::smallint             not null,
    "___readWrite"                         smallint            default '0'::smallint             not null,
    unique_temp___read                     character varying[] default '{}'::character varying[] not null,
    ___read                                smallint            default '0'::smallint             not null,
    unique_temp___write                    character varying[] default '{}'::character varying[] not null,
    ___write                               smallint            default '0'::smallint             not null
);

create unique index if not exists "IDX_0ad37b7ef50f4ddc84363d7ccc"
    on __chart__active_users (date);

create table if not exists __chart__drive
(
    id                   serial
        constraint "PK_f96bc548a765cd4b3b354221ce7"
            primary key,
    date                 integer                     not null
        constraint "UQ_13565815f618a1ff53886c5b28a"
            unique,
    "___local_incCount"  integer default '0'::bigint not null,
    "___local_incSize"   integer default '0'::bigint not null,
    "___local_decCount"  integer default '0'::bigint not null,
    "___local_decSize"   integer default '0'::bigint not null,
    "___remote_incCount" integer default '0'::bigint not null,
    "___remote_incSize"  integer default '0'::bigint not null,
    "___remote_decCount" integer default '0'::bigint not null,
    "___remote_decSize"  integer default '0'::bigint not null
);

create unique index if not exists "IDX_13565815f618a1ff53886c5b28"
    on __chart__drive (date);

create table if not exists __chart__federation
(
    id                                 serial
        constraint "PK_b39dcd31a0fe1a7757e348e85fd"
            primary key,
    date                               integer                                               not null
        constraint "UQ_36cb699c49580d4e6c2e6159f97"
            unique,
    "unique_temp___deliveredInstances" character varying[] default '{}'::character varying[] not null,
    "___deliveredInstances"            smallint            default '0'::smallint             not null,
    "unique_temp___inboxInstances"     character varying[] default '{}'::character varying[] not null,
    "___inboxInstances"                smallint            default '0'::smallint             not null,
    unique_temp___stalled              character varying[] default '{}'::character varying[] not null,
    ___stalled                         smallint            default '0'::smallint             not null,
    ___sub                             smallint            default '0'::smallint             not null,
    ___pub                             smallint            default '0'::smallint             not null,
    ___pubsub                          smallint            default '0'::smallint             not null,
    "___subActive"                     smallint            default '0'::smallint             not null,
    "___pubActive"                     smallint            default '0'::smallint             not null
);

create unique index if not exists "IDX_36cb699c49580d4e6c2e6159f9"
    on __chart__federation (date);

create table if not exists __chart__hashtag
(
    id                         serial
        constraint "PK_c32f1ea2b44a5d2f7881e37f8f9"
            primary key,
    date                       integer                                               not null,
    "group"                    varchar(128)                                          not null,
    ___local_users             integer             default 0                         not null,
    ___remote_users            integer             default 0                         not null,
    unique_temp___local_users  character varying[] default '{}'::character varying[] not null,
    unique_temp___remote_users character varying[] default '{}'::character varying[] not null,
    constraint "UQ_25a97c02003338124b2b75fdbc8"
        unique (date, "group")
);

create unique index if not exists "IDX_25a97c02003338124b2b75fdbc"
    on __chart__hashtag (date, "group");

create table if not exists __chart__instance
(
    id                        serial
        constraint "PK_1267c67c7c2d47b4903975f2c00"
            primary key,
    date                      integer                      not null,
    "group"                   varchar(128)                 not null,
    ___requests_failed        smallint default '0'::bigint not null,
    ___requests_succeeded     smallint default '0'::bigint not null,
    ___requests_received      smallint default '0'::bigint not null,
    ___notes_total            integer  default '0'::bigint not null,
    ___notes_inc              integer  default '0'::bigint not null,
    ___notes_dec              integer  default '0'::bigint not null,
    ___notes_diffs_normal     integer  default '0'::bigint not null,
    ___notes_diffs_reply      integer  default '0'::bigint not null,
    ___notes_diffs_renote     integer  default '0'::bigint not null,
    ___users_total            integer  default '0'::bigint not null,
    ___users_inc              smallint default '0'::bigint not null,
    ___users_dec              smallint default '0'::bigint not null,
    ___following_total        integer  default '0'::bigint not null,
    ___following_inc          smallint default '0'::bigint not null,
    ___following_dec          smallint default '0'::bigint not null,
    ___followers_total        integer  default '0'::bigint not null,
    ___followers_inc          smallint default '0'::bigint not null,
    ___followers_dec          smallint default '0'::bigint not null,
    "___drive_totalFiles"     integer  default '0'::bigint not null,
    "___drive_incFiles"       integer  default '0'::bigint not null,
    "___drive_incUsage"       integer  default '0'::bigint not null,
    "___drive_decFiles"       integer  default '0'::bigint not null,
    "___drive_decUsage"       integer  default '0'::bigint not null,
    "___notes_diffs_withFile" integer  default 0           not null,
    constraint "UQ_39ee857ab2f23493037c6b66311"
        unique (date, "group")
);

create unique index if not exists "IDX_39ee857ab2f23493037c6b6631"
    on __chart__instance (date, "group");

create table if not exists __chart__network
(
    id                    serial
        constraint "PK_bc4290c2e27fad14ef0c1ca93f3"
            primary key,
    date                  integer                     not null
        constraint "UQ_a1efd3e0048a5f2793a47360dc6"
            unique,
    "___incomingRequests" integer default '0'::bigint not null,
    "___outgoingRequests" integer default '0'::bigint not null,
    "___totalTime"        integer default '0'::bigint not null,
    "___incomingBytes"    integer default '0'::bigint not null,
    "___outgoingBytes"    integer default '0'::bigint not null
);

create unique index if not exists "IDX_a1efd3e0048a5f2793a47360dc"
    on __chart__network (date);

create table if not exists __chart__notes
(
    id                         serial
        constraint "PK_0aec823fa85c7f901bdb3863b14"
            primary key,
    date                       integer                     not null
        constraint "UQ_42eb716a37d381cdf566192b2be"
            unique,
    ___local_total             integer default '0'::bigint not null,
    ___local_inc               integer default '0'::bigint not null,
    ___local_dec               integer default '0'::bigint not null,
    ___local_diffs_normal      integer default '0'::bigint not null,
    ___local_diffs_reply       integer default '0'::bigint not null,
    ___local_diffs_renote      integer default '0'::bigint not null,
    ___remote_total            integer default '0'::bigint not null,
    ___remote_inc              integer default '0'::bigint not null,
    ___remote_dec              integer default '0'::bigint not null,
    ___remote_diffs_normal     integer default '0'::bigint not null,
    ___remote_diffs_reply      integer default '0'::bigint not null,
    ___remote_diffs_renote     integer default '0'::bigint not null,
    "___local_diffs_withFile"  integer default 0           not null,
    "___remote_diffs_withFile" integer default 0           not null
);

create unique index if not exists "IDX_42eb716a37d381cdf566192b2b"
    on __chart__notes (date);

create table if not exists __chart__per_user_drive
(
    id              serial
        constraint "PK_d0ef23d24d666e1a44a0cd3d208"
            primary key,
    date            integer                      not null,
    "group"         varchar(128)                 not null,
    "___totalCount" integer  default '0'::bigint not null,
    "___totalSize"  integer  default '0'::bigint not null,
    "___incCount"   smallint default '0'::bigint not null,
    "___incSize"    integer  default '0'::bigint not null,
    "___decCount"   smallint default '0'::bigint not null,
    "___decSize"    integer  default '0'::bigint not null,
    constraint "UQ_30bf67687f483ace115c5ca6429"
        unique (date, "group")
);

create unique index if not exists "IDX_30bf67687f483ace115c5ca642"
    on __chart__per_user_drive (date, "group");

create table if not exists __chart__per_user_following
(
    id                         serial
        constraint "PK_85bb1b540363a29c2fec83bd907"
            primary key,
    date                       integer                      not null,
    "group"                    varchar(128)                 not null,
    ___local_followings_total  integer  default '0'::bigint not null,
    ___local_followings_inc    smallint default '0'::bigint not null,
    ___local_followings_dec    smallint default '0'::bigint not null,
    ___local_followers_total   integer  default '0'::bigint not null,
    ___local_followers_inc     smallint default '0'::bigint not null,
    ___local_followers_dec     smallint default '0'::bigint not null,
    ___remote_followings_total integer  default '0'::bigint not null,
    ___remote_followings_inc   smallint default '0'::bigint not null,
    ___remote_followings_dec   smallint default '0'::bigint not null,
    ___remote_followers_total  integer  default '0'::bigint not null,
    ___remote_followers_inc    smallint default '0'::bigint not null,
    ___remote_followers_dec    smallint default '0'::bigint not null,
    constraint "UQ_b77d4dd9562c3a899d9a286fcd7"
        unique (date, "group")
);

create unique index if not exists "IDX_b77d4dd9562c3a899d9a286fcd"
    on __chart__per_user_following (date, "group");

create table if not exists __chart__per_user_notes
(
    id                  serial
        constraint "PK_334acf6e915af2f29edc11b8e50"
            primary key,
    date                integer                        not null,
    "group"             varchar(128)                   not null,
    ___total            integer  default '0'::bigint   not null,
    ___inc              smallint default '0'::bigint   not null,
    ___dec              smallint default '0'::bigint   not null,
    ___diffs_normal     smallint default '0'::bigint   not null,
    ___diffs_reply      smallint default '0'::bigint   not null,
    ___diffs_renote     smallint default '0'::bigint   not null,
    "___diffs_withFile" smallint default '0'::smallint not null,
    constraint "UQ_5048e9daccbbbc6d567bb142d34"
        unique (date, "group")
);

create unique index if not exists "IDX_5048e9daccbbbc6d567bb142d3"
    on __chart__per_user_notes (date, "group");

create table if not exists __chart__per_user_reaction
(
    id              serial
        constraint "PK_984f54dae441e65b633e8d27a7f"
            primary key,
    date            integer                      not null,
    "group"         varchar(128)                 not null,
    ___local_count  smallint default '0'::bigint not null,
    ___remote_count smallint default '0'::bigint not null,
    constraint "UQ_229a41ad465f9205f1f57032910"
        unique (date, "group")
);

create unique index if not exists "IDX_229a41ad465f9205f1f5703291"
    on __chart__per_user_reaction (date, "group");

create table if not exists __chart__test_grouped
(
    id           serial
        constraint "PK_f4a2b175d308695af30d4293272"
            primary key,
    date         integer not null,
    "group"      varchar(128),
    ___foo_total bigint  not null,
    ___foo_inc   bigint  not null,
    ___foo_dec   bigint  not null
);

create unique index if not exists "IDX_b14489029e4b3aaf4bba5fb524"
    on __chart__test_grouped (date, "group");

create unique index if not exists "IDX_da522b4008a9f5d7743b87ad55"
    on __chart__test_grouped (date)
    where ("group" IS NULL);

create table if not exists __chart__test_unique
(
    id      serial
        constraint "PK_409bac9c97cc612d8500012319d"
            primary key,
    date    integer                                               not null,
    "group" varchar(128),
    ___foo  character varying[] default '{}'::character varying[] not null
);

create unique index if not exists "IDX_a0cd75442dd10d0643a17c4a49"
    on __chart__test_unique (date, "group");

create unique index if not exists "IDX_16effb2e888f6763673b579f80"
    on __chart__test_unique (date)
    where ("group" IS NULL);

create table if not exists __chart__test
(
    id           serial
        constraint "PK_b4bc31dffbd1b785276a3ecfc1e"
            primary key,
    date         integer not null,
    "group"      varchar(128),
    ___foo_total bigint  not null,
    ___foo_inc   bigint  not null,
    ___foo_dec   bigint  not null
);

create unique index if not exists "IDX_a319e5dbf47e8a17497623beae"
    on __chart__test (date, "group");

create unique index if not exists "IDX_dab383a36f3c9db4a0c9b02cf3"
    on __chart__test (date)
    where ("group" IS NULL);

create table if not exists __chart__users
(
    id              serial
        constraint "PK_4dfcf2c78d03524b9eb2c99d328"
            primary key,
    date            integer                      not null
        constraint "UQ_845254b3eaf708ae8a6cac30265"
            unique,
    ___local_total  integer  default '0'::bigint not null,
    ___local_inc    smallint default '0'::bigint not null,
    ___local_dec    smallint default '0'::bigint not null,
    ___remote_total integer  default '0'::bigint not null,
    ___remote_inc   smallint default '0'::bigint not null,
    ___remote_dec   smallint default '0'::bigint not null
);

create unique index if not exists "IDX_845254b3eaf708ae8a6cac3026"
    on __chart__users (date);

create table if not exists page
(
    id                    varchar(32)                                      not null
        constraint "PK_742f4117e065c5b6ad21b37ba1f"
            primary key,
    "createdAt"           timestamp with time zone                         not null,
    "updatedAt"           timestamp with time zone                         not null,
    title                 varchar(256)                                     not null,
    name                  varchar(256)                                     not null,
    summary               varchar(256),
    "alignCenter"         boolean                                          not null,
    font                  varchar(32)                                      not null,
    "userId"              varchar(32)                                      not null
        constraint "FK_ae1d917992dd0c9d9bbdad06c4a"
            references "user"
            on delete cascade,
    "eyeCatchingImageId"  varchar(32)
        constraint "FK_a9ca79ad939bf06066b81c9d3aa"
            references drive_file
            on delete cascade,
    content               jsonb          default '[]'::jsonb               not null,
    variables             jsonb          default '[]'::jsonb               not null,
    visibility            page_visibility_enum                             not null,
    "visibleUserIds"      varchar(32)[]  default '{}'::character varying[] not null,
    "likedCount"          integer        default 0                         not null,
    "hideTitleWhenPinned" boolean        default false                     not null,
    script                varchar(16384) default ''::character varying     not null,
    "isPublic"            boolean        default true                      not null
);

comment on column page."createdAt" is 'The created date of the Page.';

comment on column page."updatedAt" is 'The updated date of the Page.';

comment on column page."userId" is 'The ID of author.';

create table if not exists user_profile
(
    "userId"                   varchar(32)                                                                                                     not null
        constraint "PK_51cb79b5555effaf7d69ba1cff9"
            primary key
        constraint "UQ_51cb79b5555effaf7d69ba1cff9"
            unique
        constraint "FK_51cb79b5555effaf7d69ba1cff9"
            references "user"
            on delete cascade,
    location                   varchar(128),
    birthday                   char(10),
    description                varchar(2048),
    fields                     jsonb                                       default '[]'::jsonb                                                 not null,
    url                        varchar(512),
    email                      varchar(128),
    "emailVerifyCode"          varchar(128),
    "emailVerified"            boolean                                     default false                                                       not null,
    "twoFactorTempSecret"      varchar(128),
    "twoFactorSecret"          varchar(128),
    "twoFactorEnabled"         boolean                                     default false                                                       not null,
    password                   varchar(128),
    "clientData"               jsonb                                       default '{}'::jsonb                                                 not null,
    "autoAcceptFollowed"       boolean                                     default false                                                       not null,
    "alwaysMarkNsfw"           boolean                                     default false                                                       not null,
    "carefulBot"               boolean                                     default false                                                       not null,
    "userHost"                 varchar(128),
    "securityKeysAvailable"    boolean                                     default false                                                       not null,
    "usePasswordLessLogin"     boolean                                     default false                                                       not null,
    "pinnedPageId"             varchar(32)
        constraint "UQ_6dc44f1ceb65b1e72bacef2ca27"
            unique
        constraint "FK_6dc44f1ceb65b1e72bacef2ca27"
            references page
            on delete set null,
    room                       jsonb                                       default '{}'::jsonb                                                 not null,
    integrations               jsonb                                       default '{}'::jsonb                                                 not null,
    "injectFeaturedNote"       boolean                                     default true                                                        not null,
    "enableWordMute"           boolean                                     default false                                                       not null,
    "mutedWords"               jsonb                                       default '[]'::jsonb                                                 not null,
    "mutingNotificationTypes"  user_profile_mutingnotificationtypes_enum[] default '{}'::user_profile_mutingnotificationtypes_enum[]           not null,
    "noCrawle"                 boolean                                     default false                                                       not null,
    "receiveAnnouncementEmail" boolean                                     default true                                                        not null,
    "emailNotificationTypes"   jsonb                                       default '["follow", "receiveFollowRequest", "groupInvited"]'::jsonb not null,
    lang                       varchar(32),
    "mutedInstances"           jsonb                                       default '[]'::jsonb                                                 not null,
    "publicReactions"          boolean                                     default false                                                       not null,
    "ffVisibility"             user_profile_ffvisibility_enum              default 'public'::user_profile_ffvisibility_enum                    not null,
    "autoSensitive"            boolean                                     default false                                                       not null,
    "moderationNote"           varchar(8192)                               default ''::character varying                                       not null,
    "preventAiLearning"        boolean                                     default true                                                        not null
);

comment on column user_profile.location is 'The location of the User.';

comment on column user_profile.birthday is 'The birthday (YYYY-MM-DD) of the User.';

comment on column user_profile.description is 'The description (bio) of the User.';

comment on column user_profile.url is 'Remote URL of the user.';

comment on column user_profile.email is 'The email address of the User.';

comment on column user_profile.password is 'The password hash of the User. It will be null if the origin of the user is local.';

comment on column user_profile."clientData" is 'The client-specific data of the User.';

comment on column user_profile."userHost" is '[Denormalized]';

comment on column user_profile.room is 'The room data of the User.';

comment on column user_profile."noCrawle" is 'Whether reject index by crawler.';

comment on column user_profile."mutedInstances" is 'List of instances muted by the user.';

create index if not exists "IDX_dce530b98e454793dac5ec2f5a"
    on user_profile ("userHost");

create index if not exists "IDX_3befe6f999c86aff06eb0257b4"
    on user_profile ("enableWordMute");

create index if not exists "IDX_fbb4297c927a9b85e9cefa2eb1"
    on page ("createdAt");

create index if not exists "IDX_af639b066dfbca78b01a920f8a"
    on page ("updatedAt");

create index if not exists "IDX_b82c19c08afb292de4600d99e4"
    on page (name);

create index if not exists "IDX_ae1d917992dd0c9d9bbdad06c4"
    on page ("userId");

create index if not exists "IDX_90148bbc2bf0854428786bfc15"
    on page ("visibleUserIds");

create unique index if not exists "IDX_2133ef8317e4bdb839c0dcbf13"
    on page ("userId", name);

create table if not exists page_like
(
    id          varchar(32)              not null
        constraint "PK_813f034843af992d3ae0f43c64c"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_0e61efab7f88dbb79c9166dbb48"
            references "user"
            on delete cascade,
    "pageId"    varchar(32)              not null
        constraint "FK_cf8782626dced3176038176a847"
            references page
            on delete cascade
);

create index if not exists "IDX_0e61efab7f88dbb79c9166dbb4"
    on page_like ("userId");

create unique index if not exists "IDX_4ce6fb9c70529b4c8ac46c9bfa"
    on page_like ("userId", "pageId");

create table if not exists user_group
(
    id          varchar(32)              not null
        constraint "PK_3c29fba6fe013ec8724378ce7c9"
            primary key,
    "createdAt" timestamp with time zone not null,
    name        varchar(256)             not null,
    "userId"    varchar(32)              not null
        constraint "FK_3d6b372788ab01be58853003c93"
            references "user"
            on delete cascade,
    "isPrivate" boolean default false    not null
);

comment on column user_group."createdAt" is 'The created date of the UserGroup.';

comment on column user_group."userId" is 'The ID of owner.';

create table if not exists messaging_message
(
    id            varchar(32)                                     not null
        constraint "PK_db398fd79dc95d0eb8c30456eaa"
            primary key,
    "createdAt"   timestamp with time zone                        not null,
    "userId"      varchar(32)                                     not null
        constraint "FK_5377c307783fce2b6d352e1203b"
            references "user"
            on delete cascade,
    "recipientId" varchar(32)
        constraint "FK_cac14a4e3944454a5ce7daa5142"
            references "user"
            on delete cascade,
    text          varchar(4096),
    "isRead"      boolean       default false                     not null,
    "fileId"      varchar(32)
        constraint "FK_535def119223ac05ad3fa9ef64b"
            references drive_file
            on delete cascade,
    "groupId"     varchar(32)
        constraint "FK_2c4be03b446884f9e9c502135be"
            references user_group
            on delete cascade,
    reads         varchar(32)[] default '{}'::character varying[] not null,
    uri           varchar(512)
);

comment on column messaging_message."createdAt" is 'The created date of the MessagingMessage.';

comment on column messaging_message."userId" is 'The sender user ID.';

comment on column messaging_message."recipientId" is 'The recipient user ID.';

comment on column messaging_message."groupId" is 'The recipient group ID.';

create index if not exists "IDX_e21cd3646e52ef9c94aaf17c2e"
    on messaging_message ("createdAt");

create index if not exists "IDX_5377c307783fce2b6d352e1203"
    on messaging_message ("userId");

create index if not exists "IDX_cac14a4e3944454a5ce7daa514"
    on messaging_message ("recipientId");

create index if not exists "IDX_2c4be03b446884f9e9c502135b"
    on messaging_message ("groupId");

create index if not exists "IDX_20e30aa35180e317e133d75316"
    on user_group ("createdAt");

create index if not exists "IDX_3d6b372788ab01be58853003c9"
    on user_group ("userId");

create table if not exists user_group_joining
(
    id            varchar(32)              not null
        constraint "PK_15f2425885253c5507e1599cfe7"
            primary key,
    "createdAt"   timestamp with time zone not null,
    "userId"      varchar(32)              not null
        constraint "FK_f3a1b4bd0c7cabba958a0c0b231"
            references "user"
            on delete cascade,
    "userGroupId" varchar(32)              not null
        constraint "FK_67dc758bc0566985d1b3d399865"
            references user_group
            on delete cascade
);

comment on column user_group_joining."createdAt" is 'The created date of the UserGroupJoining.';

comment on column user_group_joining."userId" is 'The user ID.';

comment on column user_group_joining."userGroupId" is 'The group ID.';

create index if not exists "IDX_f3a1b4bd0c7cabba958a0c0b23"
    on user_group_joining ("userId");

create index if not exists "IDX_67dc758bc0566985d1b3d39986"
    on user_group_joining ("userGroupId");

create unique index if not exists "IDX_d9ecaed8c6dc43f3592c229282"
    on user_group_joining ("userId", "userGroupId");

create table if not exists user_group_invite
(
    id            varchar(32)              not null
        constraint "PK_3893884af0d3a5f4d01e7921a97"
            primary key,
    "createdAt"   timestamp with time zone not null,
    "userId"      varchar(32)              not null
        constraint "FK_1039988afa3bf991185b277fe03"
            references "user"
            on delete cascade,
    "userGroupId" varchar(32)              not null
        constraint "FK_e10924607d058004304611a436a"
            references user_group
            on delete cascade
);

create index if not exists "IDX_1039988afa3bf991185b277fe0"
    on user_group_invite ("userId");

create index if not exists "IDX_e10924607d058004304611a436"
    on user_group_invite ("userGroupId");

create unique index if not exists "IDX_78787741f9010886796f2320a4"
    on user_group_invite ("userId", "userGroupId");

create table if not exists attestation_challenge
(
    id                      varchar(32)              not null,
    "userId"                varchar(32)              not null
        constraint "FK_f1a461a618fa1755692d0e0d592"
            references "user"
            on delete cascade,
    challenge               varchar(64)              not null,
    "createdAt"             timestamp with time zone not null,
    "registrationChallenge" boolean default false    not null,
    constraint "PK_d0ba6786e093f1bcb497572a6b5"
        primary key (id, "userId")
);

comment on column attestation_challenge.challenge is 'Hex-encoded sha256 hash of the challenge.';

comment on column attestation_challenge."createdAt" is 'The date challenge was created for expiry purposes.';

comment on column attestation_challenge."registrationChallenge" is 'Indicates that the challenge is only for registration purposes if true to prevent the challenge for being used as authentication.';

create index if not exists "IDX_f1a461a618fa1755692d0e0d59"
    on attestation_challenge ("userId");

create index if not exists "IDX_47efb914aed1f72dd39a306c7b"
    on attestation_challenge (challenge);

create table if not exists user_security_key
(
    id          varchar                  not null
        constraint "PK_3e508571121ab39c5f85d10c166"
            primary key,
    "userId"    varchar(32)              not null
        constraint "FK_ff9ca3b5f3ee3d0681367a9b447"
            references "user"
            on delete cascade,
    "publicKey" varchar                  not null,
    "lastUsed"  timestamp with time zone not null,
    name        varchar(30)              not null
);

comment on column user_security_key.id is 'Variable-length id given to navigator.credentials.get()';

comment on column user_security_key."publicKey" is 'Variable-length public key used to verify attestations (hex-encoded).';

comment on column user_security_key."lastUsed" is 'The date of the last time the UserSecurityKey was successfully validated.';

comment on column user_security_key.name is 'User-defined name for this key';

create index if not exists "IDX_ff9ca3b5f3ee3d0681367a9b44"
    on user_security_key ("userId");

create index if not exists "IDX_0d7718e562dcedd0aa5cf2c9f7"
    on user_security_key ("publicKey");

create table if not exists moderation_log
(
    id          varchar(32)              not null
        constraint "PK_d0adca6ecfd068db83e4526cc26"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_a08ad074601d204e0f69da9a954"
            references "user"
            on delete cascade,
    type        varchar(128)             not null,
    info        jsonb                    not null
);

comment on column moderation_log."createdAt" is 'The created date of the ModerationLog.';

create index if not exists "IDX_a08ad074601d204e0f69da9a95"
    on moderation_log ("userId");

create table if not exists used_username
(
    username    varchar(128)             not null
        constraint "PK_78fd79d2d24c6ac2f4cc9a31a5d"
            primary key,
    "createdAt" timestamp with time zone not null
);

create table if not exists announcement
(
    id          varchar(32)              not null
        constraint "PK_e0ef0550174fd1099a308fd18a0"
            primary key,
    "createdAt" timestamp with time zone not null,
    text        varchar(8192)            not null,
    title       varchar(256)             not null,
    "imageUrl"  varchar(1024),
    "updatedAt" timestamp with time zone
);

comment on column announcement."createdAt" is 'The created date of the Announcement.';

comment on column announcement."updatedAt" is 'The updated date of the Announcement.';

create index if not exists "IDX_118ec703e596086fc4515acb39"
    on announcement ("createdAt");

create table if not exists announcement_read
(
    id               varchar(32)              not null
        constraint "PK_4b90ad1f42681d97b2683890c5e"
            primary key,
    "userId"         varchar(32)              not null
        constraint "FK_8288151386172b8109f7239ab28"
            references "user"
            on delete cascade,
    "announcementId" varchar(32)              not null
        constraint "FK_603a7b1e7aa0533c6c88e9bfafe"
            references announcement
            on delete cascade,
    "createdAt"      timestamp with time zone not null
);

comment on column announcement_read."createdAt" is 'The created date of the AnnouncementRead.';

create index if not exists "IDX_8288151386172b8109f7239ab2"
    on announcement_read ("userId");

create index if not exists "IDX_603a7b1e7aa0533c6c88e9bfaf"
    on announcement_read ("announcementId");

create unique index if not exists "IDX_924fa71815cfa3941d003702a0"
    on announcement_read ("userId", "announcementId");

create table if not exists clip
(
    id          varchar(32)              not null
        constraint "PK_f0685dac8d4dd056d7255670b75"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_2b5ec6c574d6802c94c80313fb2"
            references "user"
            on delete cascade,
    name        varchar(128)             not null,
    "isPublic"  boolean default false    not null,
    description varchar(2048)
);

comment on column clip."createdAt" is 'The created date of the Clip.';

comment on column clip."userId" is 'The owner ID.';

comment on column clip.name is 'The name of the Clip.';

comment on column clip.description is 'The description of the Clip.';

create index if not exists "IDX_2b5ec6c574d6802c94c80313fb"
    on clip ("userId");

create table if not exists antenna
(
    id                   varchar(32)                                       not null
        constraint "PK_c170b99775e1dccca947c9f2d5f"
            primary key,
    "createdAt"          timestamp with time zone                          not null,
    "userId"             varchar(32)                                       not null
        constraint "FK_6446c571a0e8d0f05f01c789096"
            references "user"
            on delete cascade,
    name                 varchar(128)                                      not null,
    src                  antenna_src_enum                                  not null,
    "userListId"         varchar(32)
        constraint "FK_709d7d32053d0dd7620f678eeb9"
            references user_list
            on delete cascade,
    keywords             jsonb           default '[]'::jsonb               not null,
    "withFile"           boolean                                           not null,
    expression           varchar(2048),
    notify               boolean                                           not null,
    "caseSensitive"      boolean         default false                     not null,
    "withReplies"        boolean         default false                     not null,
    "userGroupJoiningId" varchar(32)
        constraint "FK_ccbf5a8c0be4511133dcc50ddeb"
            references user_group_joining
            on delete cascade,
    users                varchar(1024)[] default '{}'::character varying[] not null,
    "excludeKeywords"    jsonb           default '[]'::jsonb               not null,
    instances            jsonb           default '[]'::jsonb               not null
);

comment on column antenna."createdAt" is 'The created date of the Antenna.';

comment on column antenna."userId" is 'The owner ID.';

comment on column antenna.name is 'The name of the Antenna.';

create index if not exists "IDX_6446c571a0e8d0f05f01c78909"
    on antenna ("userId");

create table if not exists user_group_invitation
(
    id            varchar(32)              not null
        constraint "PK_160c63ec02bf23f6a5c5e8140d6"
            primary key,
    "createdAt"   timestamp with time zone not null,
    "userId"      varchar(32)              not null
        constraint "FK_bfbc6305547539369fe73eb144a"
            references "user"
            on delete cascade,
    "userGroupId" varchar(32)              not null
        constraint "FK_5cc8c468090e129857e9fecce5a"
            references user_group
            on delete cascade
);

comment on column user_group_invitation."createdAt" is 'The created date of the UserGroupInvitation.';

comment on column user_group_invitation."userId" is 'The user ID.';

comment on column user_group_invitation."userGroupId" is 'The group ID.';

create index if not exists "IDX_bfbc6305547539369fe73eb144"
    on user_group_invitation ("userId");

create index if not exists "IDX_5cc8c468090e129857e9fecce5"
    on user_group_invitation ("userGroupId");

create unique index if not exists "IDX_e9793f65f504e5a31fbaedbf2f"
    on user_group_invitation ("userId", "userGroupId");

create table if not exists relay
(
    id     varchar(32)       not null
        constraint "PK_78ebc9cfddf4292633b7ba57aee"
            primary key,
    inbox  varchar(512)      not null,
    status relay_status_enum not null
);

create unique index if not exists "IDX_0d9a1738f2cf7f3b1c3334dfab"
    on relay (inbox);

create table if not exists channel
(
    id            varchar(32)              not null
        constraint "PK_590f33ee6ee7d76437acf362e39"
            primary key,
    "createdAt"   timestamp with time zone not null,
    "lastNotedAt" timestamp with time zone,
    "userId"      varchar(32)
        constraint "FK_823bae55bd81b3be6e05cff4383"
            references "user"
            on delete set null,
    name          varchar(128)             not null,
    description   varchar(2048),
    "bannerId"    varchar(32)
        constraint "FK_999da2bcc7efadbfe0e92d3bc19"
            references drive_file
            on delete set null,
    "notesCount"  integer default 0        not null,
    "usersCount"  integer default 0        not null
);

comment on column channel."createdAt" is 'The created date of the Channel.';

comment on column channel."userId" is 'The owner ID.';

comment on column channel.name is 'The name of the Channel.';

comment on column channel.description is 'The description of the Channel.';

comment on column channel."bannerId" is 'The ID of banner Channel.';

comment on column channel."notesCount" is 'The count of notes.';

comment on column channel."usersCount" is 'The count of users.';

create table if not exists note
(
    id                     varchar(32)                                      not null
        constraint "PK_96d0c172a4fba276b1bbed43058"
            primary key,
    "createdAt"            timestamp with time zone                         not null,
    "replyId"              varchar(32)
        constraint "FK_17cb3553c700a4985dff5a30ff5"
            references note
            on delete cascade,
    "renoteId"             varchar(32)
        constraint "FK_52ccc804d7c69037d558bac4c96"
            references note
            on delete cascade,
    text                   text,
    name                   varchar(256),
    cw                     varchar(512),
    "userId"               varchar(32)                                      not null
        constraint "FK_5b87d9d19127bd5d92026017a7b"
            references "user"
            on delete cascade,
    "localOnly"            boolean        default false                     not null,
    "renoteCount"          smallint       default 0                         not null,
    "repliesCount"         smallint       default 0                         not null,
    reactions              jsonb          default '{}'::jsonb               not null,
    visibility             note_visibility_enum                             not null,
    uri                    varchar(512),
    score                  integer        default 0                         not null,
    "fileIds"              varchar(32)[]  default '{}'::character varying[] not null,
    "attachedFileTypes"    varchar(256)[] default '{}'::character varying[] not null,
    "visibleUserIds"       varchar(32)[]  default '{}'::character varying[] not null,
    mentions               varchar(32)[]  default '{}'::character varying[] not null,
    "mentionedRemoteUsers" text           default '[]'::text                not null,
    emojis                 varchar(128)[] default '{}'::character varying[] not null,
    tags                   varchar(128)[] default '{}'::character varying[] not null,
    "hasPoll"              boolean        default false                     not null,
    "userHost"             varchar(128),
    "replyUserId"          varchar(32),
    "replyUserHost"        varchar(128),
    "renoteUserId"         varchar(32),
    "renoteUserHost"       varchar(128),
    url                    varchar(512),
    "channelId"            varchar(32)
        constraint "FK_f22169eb10657bded6d875ac8f9"
            references channel
            on delete cascade,
    "threadId"             varchar(256),
    "updatedAt"            timestamp with time zone
);

comment on column note."createdAt" is 'The created date of the Note.';

comment on column note."replyId" is 'The ID of reply target.';

comment on column note."renoteId" is 'The ID of renote target.';

comment on column note."userId" is 'The ID of author.';

comment on column note.uri is 'The URI of a note. it will be null when the note is local.';

comment on column note."userHost" is '[Denormalized]';

comment on column note."replyUserId" is '[Denormalized]';

comment on column note."replyUserHost" is '[Denormalized]';

comment on column note."renoteUserId" is '[Denormalized]';

comment on column note."renoteUserHost" is '[Denormalized]';

comment on column note.url is 'The human readable url of a note. it will be null when the note is local.';

comment on column note."channelId" is 'The ID of source channel.';

comment on column note."updatedAt" is 'The updated date of the Note.';

create index if not exists "IDX_e7c0567f5261063592f022e9b5"
    on note ("createdAt");

create index if not exists "IDX_17cb3553c700a4985dff5a30ff"
    on note ("replyId");

create index if not exists "IDX_52ccc804d7c69037d558bac4c9"
    on note ("renoteId");

create index if not exists "IDX_5b87d9d19127bd5d92026017a7"
    on note ("userId");

create unique index if not exists "IDX_153536c67d05e9adb24e99fc2b"
    on note (uri);

create index if not exists "IDX_51c063b6a133a9cb87145450f5"
    on note ("fileIds");

create index if not exists "IDX_25dfc71b0369b003a4cd434d0b"
    on note ("attachedFileTypes");

create index if not exists "IDX_796a8c03959361f97dc2be1d5c"
    on note ("visibleUserIds");

create index if not exists "IDX_54ebcb6d27222913b908d56fd8"
    on note (mentions);

create index if not exists "IDX_88937d94d7443d9a99a76fa5c0"
    on note (tags);

create index if not exists "IDX_7125a826ab192eb27e11d358a5"
    on note ("userHost");

create index if not exists "IDX_NOTE_TAGS"
    on note using gin (tags);

create index if not exists "IDX_NOTE_MENTIONS"
    on note using gin (mentions);

create index if not exists "IDX_NOTE_VISIBLE_USER_IDS"
    on note using gin ("visibleUserIds");

create index if not exists "IDX_d4ebdef929896d6dc4a3c5bb48"
    on note ("threadId");

create index if not exists "IDX_f22169eb10657bded6d875ac8f"
    on note ("channelId");

create table if not exists poll_vote
(
    id          varchar(32)              not null
        constraint "PK_fd002d371201c472490ba89c6a0"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_66d2bd2ee31d14bcc23069a89f8"
            references "user"
            on delete cascade,
    "noteId"    varchar(32)              not null
        constraint "FK_aecfbd5ef60374918e63ee95fa7"
            references note
            on delete cascade,
    choice      integer                  not null
);

comment on column poll_vote."createdAt" is 'The created date of the PollVote.';

create index if not exists "IDX_0fb627e1c2f753262a74f0562d"
    on poll_vote ("createdAt");

create index if not exists "IDX_66d2bd2ee31d14bcc23069a89f"
    on poll_vote ("userId");

create index if not exists "IDX_aecfbd5ef60374918e63ee95fa"
    on poll_vote ("noteId");

create unique index if not exists "IDX_50bd7164c5b78f1f4a42c4d21f"
    on poll_vote ("userId", "noteId", choice);

create table if not exists note_reaction
(
    id          varchar(32)              not null
        constraint "PK_767ec729b108799b587a3fcc9cf"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_13761f64257f40c5636d0ff95ee"
            references "user"
            on delete cascade,
    "noteId"    varchar(32)              not null
        constraint "FK_45145e4953780f3cd5656f0ea6a"
            references note
            on delete cascade,
    reaction    varchar(260)             not null
);

comment on column note_reaction."createdAt" is 'The created date of the NoteReaction.';

create index if not exists "IDX_01f4581f114e0ebd2bbb876f0b"
    on note_reaction ("createdAt");

create index if not exists "IDX_13761f64257f40c5636d0ff95e"
    on note_reaction ("userId");

create index if not exists "IDX_45145e4953780f3cd5656f0ea6"
    on note_reaction ("noteId");

create unique index if not exists "IDX_ad0c221b25672daf2df320a817"
    on note_reaction ("userId", "noteId");

create table if not exists note_watching
(
    id           varchar(32)              not null
        constraint "PK_49286fdb23725945a74aa27d757"
            primary key,
    "createdAt"  timestamp with time zone not null,
    "userId"     varchar(32)              not null
        constraint "FK_b0134ec406e8d09a540f8182888"
            references "user"
            on delete cascade,
    "noteId"     varchar(32)              not null
        constraint "FK_03e7028ab8388a3f5e3ce2a8619"
            references note
            on delete cascade,
    "noteUserId" varchar(32)              not null
);

comment on column note_watching."createdAt" is 'The created date of the NoteWatching.';

comment on column note_watching."userId" is 'The watcher ID.';

comment on column note_watching."noteId" is 'The target Note ID.';

comment on column note_watching."noteUserId" is '[Denormalized]';

create index if not exists "IDX_318cdf42a9cfc11f479bd802bb"
    on note_watching ("createdAt");

create index if not exists "IDX_b0134ec406e8d09a540f818288"
    on note_watching ("userId");

create index if not exists "IDX_03e7028ab8388a3f5e3ce2a861"
    on note_watching ("noteId");

create index if not exists "IDX_44499765eec6b5489d72c4253b"
    on note_watching ("noteUserId");

create unique index if not exists "IDX_a42c93c69989ce1d09959df4cf"
    on note_watching ("userId", "noteId");

create table if not exists note_unread
(
    id              varchar(32) not null
        constraint "PK_1904eda61a784f57e6e51fa9c1f"
            primary key,
    "userId"        varchar(32) not null
        constraint "FK_56b0166d34ddae49d8ef7610bb9"
            references "user"
            on delete cascade,
    "noteId"        varchar(32) not null
        constraint "FK_e637cba4dc4410218c4251260e4"
            references note
            on delete cascade,
    "noteUserId"    varchar(32) not null,
    "isSpecified"   boolean     not null,
    "isMentioned"   boolean     not null,
    "noteChannelId" varchar(32)
);

comment on column note_unread."noteUserId" is '[Denormalized]';

comment on column note_unread."noteChannelId" is '[Denormalized]';

create index if not exists "IDX_56b0166d34ddae49d8ef7610bb"
    on note_unread ("userId");

create index if not exists "IDX_e637cba4dc4410218c4251260e"
    on note_unread ("noteId");

create unique index if not exists "IDX_d908433a4953cc13216cd9c274"
    on note_unread ("userId", "noteId");

create index if not exists "IDX_25b1dd384bec391b07b74b861c"
    on note_unread ("isMentioned");

create index if not exists "IDX_89a29c9237b8c3b6b3cbb4cb30"
    on note_unread ("isSpecified");

create index if not exists "IDX_29e8c1d579af54d4232939f994"
    on note_unread ("noteUserId");

create index if not exists "IDX_6a57f051d82c6d4036c141e107"
    on note_unread ("noteChannelId");

create table if not exists notification
(
    id                      varchar(32)              not null
        constraint "PK_705b6c7cdf9b2c2ff7ac7872cb7"
            primary key,
    "createdAt"             timestamp with time zone not null,
    "notifieeId"            varchar(32)              not null
        constraint "FK_3c601b70a1066d2c8b517094cb9"
            references "user"
            on delete cascade,
    "notifierId"            varchar(32)
        constraint "FK_3b4e96eec8d36a8bbb9d02aa710"
            references "user"
            on delete cascade,
    "isRead"                boolean default false    not null,
    "noteId"                varchar(32)
        constraint "FK_769cb6b73a1efe22ddf733ac453"
            references note
            on delete cascade,
    reaction                varchar(128),
    choice                  integer,
    "followRequestId"       varchar(32)
        constraint "FK_bd7fab507621e635b32cd31892c"
            references follow_request
            on delete cascade,
    type                    notification_type_enum   not null,
    "userGroupInvitationId" varchar(32)
        constraint "FK_8fe87814e978053a53b1beb7e98"
            references user_group_invitation
            on delete cascade,
    "customBody"            varchar(2048),
    "customHeader"          varchar(256),
    "customIcon"            varchar(1024),
    "appAccessTokenId"      varchar(32)
        constraint "FK_e22bf6bda77b6adc1fd9e75c8c9"
            references access_token
            on delete cascade
);

comment on column notification."createdAt" is 'The created date of the Notification.';

comment on column notification."notifieeId" is 'The ID of recipient user of the Notification.';

comment on column notification."notifierId" is 'The ID of sender user of the Notification.';

comment on column notification."isRead" is 'Whether the Notification is read.';

comment on column notification.type is 'The type of the Notification.';

create index if not exists "IDX_b11a5e627c41d4dc3170f1d370"
    on notification ("createdAt");

create index if not exists "IDX_3c601b70a1066d2c8b517094cb"
    on notification ("notifieeId");

create index if not exists "IDX_3b4e96eec8d36a8bbb9d02aa71"
    on notification ("notifierId");

create index if not exists "IDX_080ab397c379af09b9d2169e5b"
    on notification ("isRead");

create index if not exists "IDX_e22bf6bda77b6adc1fd9e75c8c"
    on notification ("appAccessTokenId");

create index if not exists "IDX_33f33cc8ef29d805a97ff4628b"
    on notification (type);

create table if not exists note_favorite
(
    id          varchar(32)              not null
        constraint "PK_af0da35a60b9fa4463a62082b36"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_47f4b1892f5d6ba8efb3057d81a"
            references "user"
            on delete cascade,
    "noteId"    varchar(32)              not null
        constraint "FK_0e00498f180193423c992bc4370"
            references note
            on delete cascade
);

comment on column note_favorite."createdAt" is 'The created date of the NoteFavorite.';

create index if not exists "IDX_47f4b1892f5d6ba8efb3057d81"
    on note_favorite ("userId");

create unique index if not exists "IDX_0f4fb9ad355f3effff221ef245"
    on note_favorite ("userId", "noteId");

create table if not exists user_note_pining
(
    id          varchar(32)              not null
        constraint "PK_a6a2dad4ae000abce2ea9d9b103"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_bfbc6f79ba4007b4ce5097f08d6"
            references "user"
            on delete cascade,
    "noteId"    varchar(32)              not null
        constraint "FK_68881008f7c3588ad7ecae471cf"
            references note
            on delete cascade
);

comment on column user_note_pining."createdAt" is 'The created date of the UserNotePinings.';

create index if not exists "IDX_bfbc6f79ba4007b4ce5097f08d"
    on user_note_pining ("userId");

create unique index if not exists "IDX_410cd649884b501c02d6e72738"
    on user_note_pining ("userId", "noteId");

create table if not exists poll
(
    "noteId"         varchar(32)                                      not null
        constraint "PK_da851e06d0dfe2ef397d8b1bf1b"
            primary key
        constraint "UQ_da851e06d0dfe2ef397d8b1bf1b"
            unique
        constraint "FK_da851e06d0dfe2ef397d8b1bf1b"
            references note
            on delete cascade,
    "expiresAt"      timestamp with time zone,
    multiple         boolean                                          not null,
    choices          varchar(256)[] default '{}'::character varying[] not null,
    votes            integer[]                                        not null,
    "noteVisibility" poll_notevisibility_enum                         not null,
    "userId"         varchar(32)                                      not null,
    "userHost"       varchar(128)
);

comment on column poll."noteVisibility" is '[Denormalized]';

comment on column poll."userId" is '[Denormalized]';

comment on column poll."userHost" is '[Denormalized]';

create index if not exists "IDX_0610ebcfcfb4a18441a9bcdab2"
    on poll ("userId");

create index if not exists "IDX_7fa20a12319c7f6dc3aed98c0a"
    on poll ("userHost");

create table if not exists clip_note
(
    id       varchar(32) not null
        constraint "PK_e94cda2f40a99b57e032a1a738b"
            primary key,
    "noteId" varchar(32) not null
        constraint "FK_a012eaf5c87c65da1deb5fdbfa3"
            references note
            on delete cascade,
    "clipId" varchar(32) not null
        constraint "FK_ebe99317bbbe9968a0c6f579adf"
            references clip
            on delete cascade
);

comment on column clip_note."noteId" is 'The note ID.';

comment on column clip_note."clipId" is 'The clip ID.';

create index if not exists "IDX_a012eaf5c87c65da1deb5fdbfa"
    on clip_note ("noteId");

create index if not exists "IDX_ebe99317bbbe9968a0c6f579ad"
    on clip_note ("clipId");

create unique index if not exists "IDX_6fc0ec357d55a18646262fdfff"
    on clip_note ("noteId", "clipId");

create table if not exists antenna_note
(
    id          varchar(32)           not null
        constraint "PK_fb28d94d0989a3872df19fd6ef8"
            primary key,
    "noteId"    varchar(32)           not null
        constraint "FK_bd0397be22147e17210940e125b"
            references note
            on delete cascade,
    "antennaId" varchar(32)           not null
        constraint "FK_0d775946662d2575dfd2068a5f5"
            references antenna
            on delete cascade,
    read        boolean default false not null
);

comment on column antenna_note."noteId" is 'The note ID.';

comment on column antenna_note."antennaId" is 'The antenna ID.';

create index if not exists "IDX_bd0397be22147e17210940e125"
    on antenna_note ("noteId");

create index if not exists "IDX_0d775946662d2575dfd2068a5f"
    on antenna_note ("antennaId");

create unique index if not exists "IDX_335a0bf3f904406f9ef3dd51c2"
    on antenna_note ("noteId", "antennaId");

create index if not exists "IDX_9937ea48d7ae97ffb4f3f063a4"
    on antenna_note (read);

create table if not exists promo_note
(
    "noteId"    varchar(32)              not null
        constraint "PK_e263909ca4fe5d57f8d4230dd5c"
            primary key
        constraint "UQ_e263909ca4fe5d57f8d4230dd5c"
            unique
        constraint "FK_e263909ca4fe5d57f8d4230dd5c"
            references note
            on delete cascade,
    "expiresAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
);

comment on column promo_note."userId" is '[Denormalized]';

create index if not exists "IDX_83f0862e9bae44af52ced7099e"
    on promo_note ("userId");

create table if not exists promo_read
(
    id          varchar(32)              not null
        constraint "PK_61917c1541002422b703318b7c9"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_9657d55550c3d37bfafaf7d4b05"
            references "user"
            on delete cascade,
    "noteId"    varchar(32)              not null
        constraint "FK_a46a1a603ecee695d7db26da5f4"
            references note
            on delete cascade
);

comment on column promo_read."createdAt" is 'The created date of the PromoRead.';

create index if not exists "IDX_9657d55550c3d37bfafaf7d4b0"
    on promo_read ("userId");

create unique index if not exists "IDX_2882b8a1a07c7d281a98b6db16"
    on promo_read ("userId", "noteId");

create table if not exists muted_note
(
    id       varchar(32)            not null
        constraint "PK_897e2eff1c0b9b64e55ca1418a4"
            primary key,
    "noteId" varchar(32)            not null
        constraint "FK_70ab9786313d78e4201d81cdb89"
            references note
            on delete cascade,
    "userId" varchar(32)            not null
        constraint "FK_d8e07aa18c2d64e86201601aec1"
            references "user"
            on delete cascade,
    reason   muted_note_reason_enum not null
);

comment on column muted_note."noteId" is 'The note ID.';

comment on column muted_note."userId" is 'The user ID.';

comment on column muted_note.reason is 'The reason of the MutedNote.';

create index if not exists "IDX_70ab9786313d78e4201d81cdb8"
    on muted_note ("noteId");

create index if not exists "IDX_d8e07aa18c2d64e86201601aec"
    on muted_note ("userId");

create unique index if not exists "IDX_a8c6bfd637d3f1d67a27c48e27"
    on muted_note ("noteId", "userId");

create index if not exists "IDX_636e977ff90b23676fb5624b25"
    on muted_note (reason);

create index if not exists "IDX_71cb7b435b7c0d4843317e7e16"
    on channel ("createdAt");

create index if not exists "IDX_29ef80c6f13bcea998447fce43"
    on channel ("lastNotedAt");

create index if not exists "IDX_823bae55bd81b3be6e05cff438"
    on channel ("userId");

create index if not exists "IDX_0f58c11241e649d2a638a8de94"
    on channel ("notesCount");

create index if not exists "IDX_094b86cd36bb805d1aa1e8cc9a"
    on channel ("usersCount");

create table if not exists channel_following
(
    id           varchar(32)              not null
        constraint "PK_8b104be7f7415113f2a02cd5bdd"
            primary key,
    "createdAt"  timestamp with time zone not null,
    "followeeId" varchar(32)              not null
        constraint "FK_0e43068c3f92cab197c3d3cd86e"
            references channel
            on delete cascade,
    "followerId" varchar(32)              not null
        constraint "FK_6d8084ec9496e7334a4602707e1"
            references "user"
            on delete cascade
);

comment on column channel_following."createdAt" is 'The created date of the ChannelFollowing.';

comment on column channel_following."followeeId" is 'The followee channel ID.';

comment on column channel_following."followerId" is 'The follower user ID.';

create index if not exists "IDX_11e71f2511589dcc8a4d3214f9"
    on channel_following ("createdAt");

create index if not exists "IDX_0e43068c3f92cab197c3d3cd86"
    on channel_following ("followeeId");

create index if not exists "IDX_6d8084ec9496e7334a4602707e"
    on channel_following ("followerId");

create unique index if not exists "IDX_2e230dd45a10e671d781d99f3e"
    on channel_following ("followerId", "followeeId");

create table if not exists channel_note_pining
(
    id          varchar(32)              not null
        constraint "PK_44f7474496bcf2e4b741681146d"
            primary key,
    "createdAt" timestamp with time zone not null,
    "channelId" varchar(32)              not null
        constraint "FK_8125f950afd3093acb10d2db8a8"
            references channel
            on delete cascade,
    "noteId"    varchar(32)              not null
        constraint "FK_10b19ef67d297ea9de325cd4502"
            references note
            on delete cascade
);

comment on column channel_note_pining."createdAt" is 'The created date of the ChannelNotePining.';

create index if not exists "IDX_8125f950afd3093acb10d2db8a"
    on channel_note_pining ("channelId");

create unique index if not exists "IDX_f36fed37d6d4cdcc68c803cd9c"
    on channel_note_pining ("channelId", "noteId");

create table if not exists registry_item
(
    id          varchar(32)                                       not null
        constraint "PK_64b3f7e6008b4d89b826cd3af95"
            primary key,
    "createdAt" timestamp with time zone                          not null,
    "updatedAt" timestamp with time zone                          not null,
    "userId"    varchar(32)                                       not null
        constraint "FK_fb9d21ba0abb83223263df6bcb3"
            references "user"
            on delete cascade,
    key         varchar(1024)                                     not null,
    scope       varchar(1024)[] default '{}'::character varying[] not null,
    domain      varchar(512),
    value       jsonb           default '{}'::jsonb
);

comment on column registry_item."createdAt" is 'The created date of the RegistryItem.';

comment on column registry_item."updatedAt" is 'The updated date of the RegistryItem.';

comment on column registry_item."userId" is 'The owner ID.';

comment on column registry_item.key is 'The key of the RegistryItem.';

comment on column registry_item.value is 'The value of the RegistryItem.';

create index if not exists "IDX_fb9d21ba0abb83223263df6bcb"
    on registry_item ("userId");

create index if not exists "IDX_22baca135bb8a3ea1a83d13df3"
    on registry_item (scope);

create index if not exists "IDX_0a72bdfcdb97c0eca11fe7ecad"
    on registry_item (domain);

create table if not exists gallery_post
(
    id            varchar(32)                                      not null
        constraint "PK_8e90d7b6015f2c4518881b14753"
            primary key,
    "createdAt"   timestamp with time zone                         not null,
    "updatedAt"   timestamp with time zone                         not null,
    title         varchar(256)                                     not null,
    description   varchar(2048),
    "userId"      varchar(32)                                      not null
        constraint "FK_985b836dddd8615e432d7043ddb"
            references "user"
            on delete cascade,
    "fileIds"     varchar(32)[]  default '{}'::character varying[] not null,
    "isSensitive" boolean        default false                     not null,
    "likedCount"  integer        default 0                         not null,
    tags          varchar(128)[] default '{}'::character varying[] not null
);

comment on column gallery_post."createdAt" is 'The created date of the GalleryPost.';

comment on column gallery_post."updatedAt" is 'The updated date of the GalleryPost.';

comment on column gallery_post."userId" is 'The ID of author.';

comment on column gallery_post."isSensitive" is 'Whether the post is sensitive.';

create index if not exists "IDX_8f1a239bd077c8864a20c62c2c"
    on gallery_post ("createdAt");

create index if not exists "IDX_f631d37835adb04792e361807c"
    on gallery_post ("updatedAt");

create index if not exists "IDX_985b836dddd8615e432d7043dd"
    on gallery_post ("userId");

create index if not exists "IDX_3ca50563facd913c425e7a89ee"
    on gallery_post ("fileIds");

create index if not exists "IDX_f2d744d9a14d0dfb8b96cb7fc5"
    on gallery_post ("isSensitive");

create index if not exists "IDX_1a165c68a49d08f11caffbd206"
    on gallery_post ("likedCount");

create index if not exists "IDX_05cca34b985d1b8edc1d1e28df"
    on gallery_post (tags);

create table if not exists gallery_like
(
    id          varchar(32)              not null
        constraint "PK_853ab02be39b8de45cd720cc15f"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_8fd5215095473061855ceb948cf"
            references "user"
            on delete cascade,
    "postId"    varchar(32)              not null
        constraint "FK_b1cb568bfe569e47b7051699fc8"
            references gallery_post
            on delete cascade
);

create index if not exists "IDX_8fd5215095473061855ceb948c"
    on gallery_like ("userId");

create unique index if not exists "IDX_df1b5f4099e99fb0bc5eae53b6"
    on gallery_like ("userId", "postId");

create table if not exists password_reset_request
(
    id          varchar(32)              not null
        constraint "PK_fcf4b02eae1403a2edaf87fd074"
            primary key,
    "createdAt" timestamp with time zone not null,
    token       varchar(256)             not null,
    "userId"    varchar(32)              not null
        constraint "FK_4bb7fd4a34492ae0e6cc8d30ac8"
            references "user"
            on delete cascade
);

create unique index if not exists "IDX_0b575fa9a4cfe638a925949285"
    on password_reset_request (token);

create index if not exists "IDX_4bb7fd4a34492ae0e6cc8d30ac"
    on password_reset_request ("userId");

create table if not exists ad
(
    id          varchar(32)              not null
        constraint "PK_0193d5ef09746e88e9ea92c634d"
            primary key,
    "createdAt" timestamp with time zone not null,
    "expiresAt" timestamp with time zone not null,
    place       varchar(32)              not null,
    priority    varchar(32)              not null,
    url         varchar(1024)            not null,
    "imageUrl"  varchar(1024)            not null,
    memo        varchar(8192)            not null,
    ratio       integer default 1        not null
);

comment on column ad."createdAt" is 'The created date of the Ad.';

comment on column ad."expiresAt" is 'The expired date of the Ad.';

create index if not exists "IDX_1129c2ef687fc272df040bafaa"
    on ad ("createdAt");

create index if not exists "IDX_2da24ce20ad209f1d9dc032457"
    on ad ("expiresAt");

create table if not exists user_pending
(
    id          varchar(32)              not null
        constraint "PK_d4c84e013c98ec02d19b8fbbafa"
            primary key,
    "createdAt" timestamp with time zone not null,
    code        varchar(128)             not null,
    username    varchar(128)             not null,
    email       varchar(128)             not null,
    password    varchar(128)             not null
);

create unique index if not exists "IDX_4e5c4c99175638ec0761714ab0"
    on user_pending (code);

create table if not exists note_thread_muting
(
    id          varchar(32)              not null
        constraint "PK_ec5936d94d1a0369646d12a3a47"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null
        constraint "FK_29c11c7deb06615076f8c95b80a"
            references "user"
            on delete cascade,
    "threadId"  varchar(256)             not null
);

create index if not exists "IDX_29c11c7deb06615076f8c95b80"
    on note_thread_muting ("userId");

create index if not exists "IDX_c426394644267453e76f036926"
    on note_thread_muting ("threadId");

create unique index if not exists "IDX_ae7aab18a2641d3e5f25e0c4ea"
    on note_thread_muting ("userId", "threadId");

create table if not exists __chart_day__federation
(
    id                                 serial
        constraint "PK_7ca721c769f31698e0e1331e8e6"
            primary key,
    date                               integer                                               not null
        constraint "UQ_617a8fe225a6e701d89e02d2c74"
            unique,
    "unique_temp___deliveredInstances" character varying[] default '{}'::character varying[] not null,
    "___deliveredInstances"            smallint            default '0'::smallint             not null,
    "unique_temp___inboxInstances"     character varying[] default '{}'::character varying[] not null,
    "___inboxInstances"                smallint            default '0'::smallint             not null,
    unique_temp___stalled              character varying[] default '{}'::character varying[] not null,
    ___stalled                         smallint            default '0'::smallint             not null,
    ___sub                             smallint            default '0'::smallint             not null,
    ___pub                             smallint            default '0'::smallint             not null,
    ___pubsub                          smallint            default '0'::smallint             not null,
    "___subActive"                     smallint            default '0'::smallint             not null,
    "___pubActive"                     smallint            default '0'::smallint             not null
);

create unique index if not exists "IDX_617a8fe225a6e701d89e02d2c7"
    on __chart_day__federation (date);

create table if not exists __chart_day__notes
(
    id                         serial
        constraint "PK_1fa4139e1f338272b758d05e090"
            primary key,
    date                       integer                     not null
        constraint "UQ_1a527b423ad0858a1af5a056d43"
            unique,
    ___local_total             integer default '0'::bigint not null,
    ___local_inc               integer default '0'::bigint not null,
    ___local_dec               integer default '0'::bigint not null,
    ___local_diffs_normal      integer default '0'::bigint not null,
    ___local_diffs_reply       integer default '0'::bigint not null,
    ___local_diffs_renote      integer default '0'::bigint not null,
    ___remote_total            integer default '0'::bigint not null,
    ___remote_inc              integer default '0'::bigint not null,
    ___remote_dec              integer default '0'::bigint not null,
    ___remote_diffs_normal     integer default '0'::bigint not null,
    ___remote_diffs_reply      integer default '0'::bigint not null,
    ___remote_diffs_renote     integer default '0'::bigint not null,
    "___local_diffs_withFile"  integer default 0           not null,
    "___remote_diffs_withFile" integer default 0           not null
);

create unique index if not exists "IDX_1a527b423ad0858a1af5a056d4"
    on __chart_day__notes (date);

create table if not exists __chart_day__users
(
    id              serial
        constraint "PK_d7f7185abb9851f70c4726c54bd"
            primary key,
    date            integer                      not null
        constraint "UQ_cad6e07c20037f31cdba8a350c3"
            unique,
    ___local_total  integer  default '0'::bigint not null,
    ___local_inc    smallint default '0'::bigint not null,
    ___local_dec    smallint default '0'::bigint not null,
    ___remote_total integer  default '0'::bigint not null,
    ___remote_inc   smallint default '0'::bigint not null,
    ___remote_dec   smallint default '0'::bigint not null
);

create unique index if not exists "IDX_cad6e07c20037f31cdba8a350c"
    on __chart_day__users (date);

create table if not exists __chart_day__network
(
    id                    serial
        constraint "PK_cac499d6f471042dfed1e7e0132"
            primary key,
    date                  integer                     not null
        constraint "UQ_8bfa548c2b31f9e07db113773ee"
            unique,
    "___incomingRequests" integer default '0'::bigint not null,
    "___outgoingRequests" integer default '0'::bigint not null,
    "___totalTime"        integer default '0'::bigint not null,
    "___incomingBytes"    integer default '0'::bigint not null,
    "___outgoingBytes"    integer default '0'::bigint not null
);

create unique index if not exists "IDX_8bfa548c2b31f9e07db113773e"
    on __chart_day__network (date);

create table if not exists __chart_day__active_users
(
    id                                     serial
        constraint "PK_b1790489b14f005ae8f404f5795"
            primary key,
    date                                   integer                                               not null
        constraint "UQ_d5954f3df5e5e3bdfc3c03f3906"
            unique,
    "unique_temp___registeredWithinWeek"   character varying[] default '{}'::character varying[] not null,
    "___registeredWithinWeek"              smallint            default '0'::smallint             not null,
    "unique_temp___registeredWithinMonth"  character varying[] default '{}'::character varying[] not null,
    "___registeredWithinMonth"             smallint            default '0'::smallint             not null,
    "unique_temp___registeredWithinYear"   character varying[] default '{}'::character varying[] not null,
    "___registeredWithinYear"              smallint            default '0'::smallint             not null,
    "unique_temp___registeredOutsideWeek"  character varying[] default '{}'::character varying[] not null,
    "___registeredOutsideWeek"             smallint            default '0'::smallint             not null,
    "unique_temp___registeredOutsideMonth" character varying[] default '{}'::character varying[] not null,
    "___registeredOutsideMonth"            smallint            default '0'::smallint             not null,
    "unique_temp___registeredOutsideYear"  character varying[] default '{}'::character varying[] not null,
    "___registeredOutsideYear"             smallint            default '0'::smallint             not null,
    "___readWrite"                         smallint            default '0'::smallint             not null,
    unique_temp___read                     character varying[] default '{}'::character varying[] not null,
    ___read                                smallint            default '0'::smallint             not null,
    unique_temp___write                    character varying[] default '{}'::character varying[] not null,
    ___write                               smallint            default '0'::smallint             not null
);

create unique index if not exists "IDX_d5954f3df5e5e3bdfc3c03f390"
    on __chart_day__active_users (date);

create table if not exists __chart_day__instance
(
    id                        serial
        constraint "PK_479a8ff9d959274981087043023"
            primary key,
    date                      integer                      not null,
    "group"                   varchar(128)                 not null,
    ___requests_failed        smallint default '0'::bigint not null,
    ___requests_succeeded     smallint default '0'::bigint not null,
    ___requests_received      smallint default '0'::bigint not null,
    ___notes_total            integer  default '0'::bigint not null,
    ___notes_inc              integer  default '0'::bigint not null,
    ___notes_dec              integer  default '0'::bigint not null,
    ___notes_diffs_normal     integer  default '0'::bigint not null,
    ___notes_diffs_reply      integer  default '0'::bigint not null,
    ___notes_diffs_renote     integer  default '0'::bigint not null,
    ___users_total            integer  default '0'::bigint not null,
    ___users_inc              smallint default '0'::bigint not null,
    ___users_dec              smallint default '0'::bigint not null,
    ___following_total        integer  default '0'::bigint not null,
    ___following_inc          smallint default '0'::bigint not null,
    ___following_dec          smallint default '0'::bigint not null,
    ___followers_total        integer  default '0'::bigint not null,
    ___followers_inc          smallint default '0'::bigint not null,
    ___followers_dec          smallint default '0'::bigint not null,
    "___drive_totalFiles"     integer  default '0'::bigint not null,
    "___drive_incFiles"       integer  default '0'::bigint not null,
    "___drive_incUsage"       integer  default '0'::bigint not null,
    "___drive_decFiles"       integer  default '0'::bigint not null,
    "___drive_decUsage"       integer  default '0'::bigint not null,
    "___notes_diffs_withFile" integer  default 0           not null,
    constraint "UQ_fea7c0278325a1a2492f2d6acbf"
        unique (date, "group")
);

create unique index if not exists "IDX_fea7c0278325a1a2492f2d6acb"
    on __chart_day__instance (date, "group");

create table if not exists __chart_day__per_user_notes
(
    id                  serial
        constraint "PK_58bab6b6d3ad9310cbc7460fd28"
            primary key,
    date                integer                        not null,
    "group"             varchar(128)                   not null,
    ___total            integer  default '0'::bigint   not null,
    ___inc              smallint default '0'::bigint   not null,
    ___dec              smallint default '0'::bigint   not null,
    ___diffs_normal     smallint default '0'::bigint   not null,
    ___diffs_reply      smallint default '0'::bigint   not null,
    ___diffs_renote     smallint default '0'::bigint   not null,
    "___diffs_withFile" smallint default '0'::smallint not null,
    constraint "UQ_c5545d4b31cdc684034e33b81c3"
        unique (date, "group")
);

create unique index if not exists "IDX_c5545d4b31cdc684034e33b81c"
    on __chart_day__per_user_notes (date, "group");

create table if not exists __chart_day__drive
(
    id                   serial
        constraint "PK_e7ec0de057c77c40fc8d8b62151"
            primary key,
    date                 integer                     not null
        constraint "UQ_0b60ebb3aa0065f10b0616c1171"
            unique,
    "___local_incCount"  integer default '0'::bigint not null,
    "___local_incSize"   integer default '0'::bigint not null,
    "___local_decCount"  integer default '0'::bigint not null,
    "___local_decSize"   integer default '0'::bigint not null,
    "___remote_incCount" integer default '0'::bigint not null,
    "___remote_incSize"  integer default '0'::bigint not null,
    "___remote_decCount" integer default '0'::bigint not null,
    "___remote_decSize"  integer default '0'::bigint not null
);

create unique index if not exists "IDX_0b60ebb3aa0065f10b0616c117"
    on __chart_day__drive (date);

create table if not exists __chart_day__per_user_reaction
(
    id              serial
        constraint "PK_8af24e2d51ff781a354fe595eda"
            primary key,
    date            integer                      not null,
    "group"         varchar(128)                 not null,
    ___local_count  smallint default '0'::bigint not null,
    ___remote_count smallint default '0'::bigint not null,
    constraint "UQ_d54b653660d808b118e36c184c0"
        unique (date, "group")
);

create unique index if not exists "IDX_d54b653660d808b118e36c184c"
    on __chart_day__per_user_reaction (date, "group");

create table if not exists __chart_day__hashtag
(
    id                         serial
        constraint "PK_13d5a3b089344e5557f8e0980b4"
            primary key,
    date                       integer                                               not null,
    "group"                    varchar(128)                                          not null,
    ___local_users             integer             default 0                         not null,
    ___remote_users            integer             default 0                         not null,
    unique_temp___local_users  character varying[] default '{}'::character varying[] not null,
    unique_temp___remote_users character varying[] default '{}'::character varying[] not null,
    constraint "UQ_8f589cf056ff51f09d6096f6450"
        unique (date, "group")
);

create unique index if not exists "IDX_8f589cf056ff51f09d6096f645"
    on __chart_day__hashtag (date, "group");

create table if not exists __chart_day__per_user_following
(
    id                         serial
        constraint "PK_68ce6b67da57166da66fc8fb27e"
            primary key,
    date                       integer                      not null,
    "group"                    varchar(128)                 not null,
    ___local_followings_total  integer  default '0'::bigint not null,
    ___local_followings_inc    smallint default '0'::bigint not null,
    ___local_followings_dec    smallint default '0'::bigint not null,
    ___local_followers_total   integer  default '0'::bigint not null,
    ___local_followers_inc     smallint default '0'::bigint not null,
    ___local_followers_dec     smallint default '0'::bigint not null,
    ___remote_followings_total integer  default '0'::bigint not null,
    ___remote_followings_inc   smallint default '0'::bigint not null,
    ___remote_followings_dec   smallint default '0'::bigint not null,
    ___remote_followers_total  integer  default '0'::bigint not null,
    ___remote_followers_inc    smallint default '0'::bigint not null,
    ___remote_followers_dec    smallint default '0'::bigint not null,
    constraint "UQ_e4849a3231f38281280ea4c0eee"
        unique (date, "group")
);

create unique index if not exists "IDX_e4849a3231f38281280ea4c0ee"
    on __chart_day__per_user_following (date, "group");

create table if not exists __chart_day__per_user_drive
(
    id              serial
        constraint "PK_1ae135254c137011645da7f4045"
            primary key,
    date            integer                      not null,
    "group"         varchar(128)                 not null,
    "___totalCount" integer  default '0'::bigint not null,
    "___totalSize"  integer  default '0'::bigint not null,
    "___incCount"   smallint default '0'::bigint not null,
    "___incSize"    integer  default '0'::bigint not null,
    "___decCount"   smallint default '0'::bigint not null,
    "___decSize"    integer  default '0'::bigint not null,
    constraint "UQ_62aa5047b5aec92524f24c701d7"
        unique (date, "group")
);

create unique index if not exists "IDX_62aa5047b5aec92524f24c701d"
    on __chart_day__per_user_drive (date, "group");

create table if not exists __chart__ap_request
(
    id                    serial
        constraint "PK_56a25cd447c7ee08876b3baf8d8"
            primary key,
    date                  integer           not null
        constraint "UQ_e56f4beac5746d44bc3e19c80d0"
            unique,
    "___deliverFailed"    integer default 0 not null,
    "___deliverSucceeded" integer default 0 not null,
    "___inboxReceived"    integer default 0 not null
);

create unique index if not exists "IDX_e56f4beac5746d44bc3e19c80d"
    on __chart__ap_request (date);

create table if not exists __chart_day__ap_request
(
    id                    serial
        constraint "PK_9318b49daee320194e23f712e69"
            primary key,
    date                  integer           not null
        constraint "UQ_a848f66d6cec11980a5dd595822"
            unique,
    "___deliverFailed"    integer default 0 not null,
    "___deliverSucceeded" integer default 0 not null,
    "___inboxReceived"    integer default 0 not null
);

create unique index if not exists "IDX_a848f66d6cec11980a5dd59582"
    on __chart_day__ap_request (date);

create table if not exists webhook
(
    id             varchar(32)                                      not null
        constraint "PK_e6765510c2d078db49632b59020"
            primary key,
    "createdAt"    timestamp with time zone                         not null,
    "userId"       varchar(32)                                      not null
        constraint "FK_f272c8c8805969e6a6449c77b3c"
            references "user"
            on delete cascade,
    name           varchar(128)                                     not null,
    "on"           varchar(128)[] default '{}'::character varying[] not null,
    url            varchar(1024)                                    not null,
    secret         varchar(1024)                                    not null,
    active         boolean        default true                      not null,
    "latestSentAt" timestamp with time zone,
    "latestStatus" integer
);

comment on column webhook."createdAt" is 'The created date of the Antenna.';

comment on column webhook."userId" is 'The owner ID.';

comment on column webhook.name is 'The name of the Antenna.';

create index if not exists "IDX_f272c8c8805969e6a6449c77b3"
    on webhook ("userId");

create index if not exists "IDX_8063a0586ed1dfbe86e982d961"
    on webhook ("on");

create index if not exists "IDX_5a056076f76b2efe08216ba655"
    on webhook (active);

create table if not exists user_ip
(
    id          serial
        constraint "PK_2c44ddfbf7c0464d028dcef325e"
            primary key,
    "createdAt" timestamp with time zone not null,
    "userId"    varchar(32)              not null,
    ip          varchar(128)             not null
);

create index if not exists "IDX_7f7f1c66f48e9a8e18a33bc515"
    on user_ip ("userId");

create unique index if not exists "IDX_361b500e06721013c124b7b6c5"
    on user_ip ("userId", ip);

create table if not exists renote_muting
(
    id          varchar(32)              not null
        constraint "PK_renoteMuting_id"
            primary key,
    "createdAt" timestamp with time zone not null,
    "muteeId"   varchar(32)              not null,
    "muterId"   varchar(32)              not null
);

create table if not exists note_edit
(
    id          varchar(32)                                     not null
        constraint "PK_736fc6e0d4e222ecc6f82058e08"
            primary key,
    "noteId"    varchar(32)                                     not null
        constraint "FK_702ad5ae993a672e4fbffbcd38c"
            references note
            on delete cascade,
    text        text,
    cw          varchar(512),
    "fileIds"   varchar(32)[] default '{}'::character varying[] not null,
    "updatedAt" timestamp with time zone                        not null
);

comment on column note_edit."noteId" is 'The ID of note.';

comment on column note_edit."updatedAt" is 'The updated date of the Note.';

create index if not exists "IDX_702ad5ae993a672e4fbffbcd38"
    on note_edit ("noteId");

create or replace function note_replies(start_id character varying, max_depth integer, max_breadth integer)
    returns TABLE(id character varying)
    language sql
as
$$
				SELECT DISTINCT id FROM (
					WITH RECURSIVE tree (id, ancestors, depth) AS (
						SELECT start_id, '{}'::VARCHAR[], 0
					UNION
						SELECT
							note.id,
							CASE
								WHEN note."replyId" = tree.id THEN tree.ancestors || note."replyId"
								ELSE tree.ancestors || note."renoteId"
							END,
							depth + 1
						FROM note, tree
						WHERE (
							note."replyId" = tree.id
							OR
							(
								-- get renotes but not pure renotes
								note."renoteId" = tree.id
								AND
								(
									note.text IS NOT NULL
									OR
									CARDINALITY(note."fileIds") != 0
									OR
									note."hasPoll" = TRUE
								)
							)
						) AND depth < max_depth
					)
					SELECT
						id,
						-- apply the limit per node
						row_number() OVER (PARTITION BY ancestors[array_upper(ancestors, 1)]) AS nth_child
					FROM tree
					WHERE depth > 0
				) AS recursive WHERE nth_child < max_breadth
			$$;

