CREATE SCHEMA tbm;
CREATE TYPE tbm."tbmcr_triggeredBy" AS ENUM (
    'thread',
    'reply',
    'subReply'
);
CREATE TYPE tbm."tbmi_metadata_bmp_bitsPerPixel" AS ENUM (
    'Pixel1',
    'Pixel2',
    'Pixel4',
    'Pixel8',
    'Pixel16',
    'Pixel24',
    'Pixel32'
);
CREATE TYPE tbm."tbmi_metadata_bmp_infoHeaderType" AS ENUM (
    'WinVersion2',
    'Os2Version2Short',
    'WinVersion3',
    'AdobeVersion3',
    'AdobeVersion3WithAlpha',
    'Os2Version2',
    'WinVersion4',
    'WinVersion5'
);
CREATE TYPE tbm.tbmi_metadata_format AS ENUM (
    'jpeg',
    'png',
    'gif',
    'bmp'
);
CREATE TYPE tbm."tbmi_metadata_gif_colorTableMode" AS ENUM (
    'Global',
    'Local'
);
CREATE TYPE tbm."tbmi_metadata_jpg_colorType" AS ENUM (
    'YCbCrRatio420',
    'YCbCrRatio444',
    'YCbCrRatio422',
    'YCbCrRatio411',
    'YCbCrRatio410',
    'Luminance',
    'Rgb',
    'Cmyk',
    'Ycck'
);
CREATE TYPE tbm."tbmi_metadata_png_bitDepth" AS ENUM (
    'Bit1',
    'Bit2',
    'Bit4',
    'Bit8',
    'Bit16'
);
CREATE TYPE tbm."tbmi_metadata_png_colorType" AS ENUM (
    'Grayscale',
    'Rgb',
    'Palette',
    'GrayscaleWithAlpha',
    'RgbWithAlpha'
);
CREATE TYPE tbm."tbmi_metadata_png_interlaceMethod" AS ENUM (
    'None',
    'Adam7'
);
CREATE TYPE tbm.tbmi_ocr_box_recognizer AS ENUM (
    'TesseractHorizontal',
    'TesseractVertical',
    'PaddleOCRv4',
    'PaddleOCRv3'
);
CREATE TYPE tbm.tbmi_script AS ENUM (
    'Hans',
    'Hant',
    'Jpan',
    'Latn'
);

CREATE TABLE tbm."tbm_bilibiliVote" (
    pid bigint NOT NULL,
    "authorUid" bigint NOT NULL,
    "authorExpGrade" smallint,
    "isValid" boolean NOT NULL,
    "voteBy" text,
    "voteFor" text,
    "replyContent" jsonb NOT NULL,
    "postTime" timestamp with time zone NOT NULL
);
ALTER TABLE tbm."tbm_bilibiliVote" ADD CONSTRAINT "tbm_bilibiliVote_pkey" PRIMARY KEY (pid);
CREATE INDEX "tbm_bilibiliVote_authorUid_idx" ON tbm."tbm_bilibiliVote" USING btree ("authorUid");
CREATE INDEX "tbm_bilibiliVote_isValid_idx" ON tbm."tbm_bilibiliVote" USING btree ("isValid");
CREATE INDEX "tbm_bilibiliVote_isValid_voteFor_idx" ON tbm."tbm_bilibiliVote" USING btree ("voteFor", "isValid");
CREATE INDEX "tbm_bilibiliVote_postTime_idx" ON tbm."tbm_bilibiliVote" USING btree ("postTime");

CREATE TABLE tbm.tbm_forum (
    fid integer NOT NULL,
    name text NOT NULL,
    "isCrawling" boolean NOT NULL
);
ALTER TABLE tbm.tbm_forum ADD CONSTRAINT tbm_forum_pkey PRIMARY KEY (fid);

CREATE TABLE tbm."tbmc_latestReplier" (
    id integer NOT NULL,
    uid bigint,
    name text,
    "displayName" bytea,
    "createdAt" integer NOT NULL,
    "updatedAt" integer
);
ALTER TABLE tbm."tbmc_latestReplier" ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME tbm."tbmc_latestReplier_id_seq"
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);
ALTER TABLE tbm."tbmc_latestReplier" ADD CONSTRAINT "tbmc_latestReplier_displayName_name_key" UNIQUE (name, "displayName");
ALTER TABLE tbm."tbmc_latestReplier" ADD CONSTRAINT "tbmc_latestReplier_pkey" PRIMARY KEY (id);

CREATE TABLE tbm.tbmc_reply (
    fid integer NOT NULL,
    tid bigint NOT NULL,
    pid bigint NOT NULL,
    floor integer NOT NULL,
    "authorUid" bigint NOT NULL,
    "subReplyCount" integer,
    "postedAt" integer NOT NULL,
    "isFold" smallint,
    "agreeCount" integer,
    "disagreeCount" integer,
    geolocation bytea,
    "signatureId" integer,
    "createdAt" integer NOT NULL,
    "updatedAt" integer,
    "lastSeenAt" integer
);
ALTER TABLE tbm.tbmc_reply ADD CONSTRAINT tbmc_reply_pkey PRIMARY KEY (pid);
CREATE INDEX "tbmc_reply_authorUid_idx" ON tbm.tbmc_reply USING btree ("authorUid");
CREATE INDEX tbmc_reply_fid_spid_idx ON tbm.tbmc_reply USING btree (fid, pid);
CREATE INDEX "tbmc_reply_postedAt_idx" ON tbm.tbmc_reply USING btree ("postedAt");
CREATE INDEX tbmc_reply_posts_query_by_fid_idx ON tbm.tbmc_reply USING btree (fid, "postedAt", pid, tid);
CREATE INDEX tbmc_reply_sitemap_idx ON tbm.tbmc_reply USING btree (tid, "postedAt");
CREATE INDEX tbmc_reply_tid_idx ON tbm.tbmc_reply USING btree (tid);

CREATE TABLE tbm.tbmc_reply_content (
    pid bigint NOT NULL,
    "protoBufBytes" bytea
);
ALTER TABLE tbm.tbmc_reply_content ADD CONSTRAINT tbmc_reply_content_pkey PRIMARY KEY (pid);

CREATE TABLE tbm.tbmc_reply_content_image (
    pid bigint NOT NULL,
    "imageId" integer NOT NULL
);

CREATE TABLE tbm.tbmc_reply_signature (
    uid bigint NOT NULL,
    "signatureId" integer NOT NULL,
    "xxHash3" bytea NOT NULL,
    "protoBufBytes" bytea NOT NULL,
    "firstSeenAt" integer NOT NULL,
    "lastSeenAt" integer NOT NULL
);
ALTER TABLE tbm.tbmc_reply_signature ADD CONSTRAINT tbmc_reply_signature_pkey PRIMARY KEY ("signatureId", "xxHash3");
CREATE INDEX tbmc_reply_signature_uid_idx ON tbm.tbmc_reply_signature USING btree (uid);

CREATE TABLE tbm."tbmc_subReply" (
    fid integer NOT NULL,
    tid bigint NOT NULL,
    pid bigint NOT NULL,
    spid bigint NOT NULL,
    "authorUid" bigint NOT NULL,
    "postedAt" integer NOT NULL,
    "agreeCount" integer,
    "disagreeCount" integer,
    "createdAt" integer NOT NULL,
    "updatedAt" integer,
    "lastSeenAt" integer
);
ALTER TABLE tbm."tbmc_subReply" ADD CONSTRAINT "tbmc_subReply_pkey" PRIMARY KEY (spid);
CREATE INDEX "tbmc_subReply_authorUid_idx" ON tbm."tbmc_subReply" USING btree ("authorUid");
CREATE INDEX "tbmc_subReply_fid_spid_idx" ON tbm."tbmc_subReply" USING btree (fid, spid);
CREATE INDEX "tbmc_subReply_pid_idx" ON tbm."tbmc_subReply" USING btree (pid);
CREATE INDEX "tbmc_subReply_postedAt_idx" ON tbm."tbmc_subReply" USING btree ("postedAt");
CREATE INDEX "tbmc_subReply_posts_query_by_fid_idx" ON tbm."tbmc_subReply" USING btree (fid, "postedAt", spid, pid, tid);
CREATE INDEX "tbmc_subReply_sitemap_idx" ON tbm."tbmc_subReply" USING btree (tid, "postedAt");
CREATE INDEX "tbmc_subReply_tid_idx" ON tbm."tbmc_subReply" USING btree (tid);

CREATE TABLE tbm."tbmc_subReply_content" (
    spid bigint NOT NULL,
    "protoBufBytes" bytea
);
ALTER TABLE tbm."tbmc_subReply_content" ADD CONSTRAINT "tbmc_subReply_content_pkey" PRIMARY KEY (spid);

CREATE TABLE tbm.tbmc_thread (
    fid integer NOT NULL,
    tid bigint NOT NULL,
    "threadType" bigint NOT NULL,
    "stickyType" text,
    "topicType" text,
    "isGood" smallint,
    title text NOT NULL,
    "authorUid" bigint NOT NULL,
    "postedAt" integer NOT NULL,
    "latestReplyPostedAt" integer NOT NULL,
    "latestReplierId" integer,
    "replyCount" integer,
    "viewCount" integer,
    "shareCount" integer,
    "agreeCount" integer,
    "disagreeCount" integer,
    zan bytea,
    geolocation bytea,
    "authorPhoneType" text,
    "createdAt" integer NOT NULL,
    "updatedAt" integer,
    "lastSeenAt" integer
);
ALTER TABLE tbm.tbmc_thread ADD CONSTRAINT tbmc_thread_pkey PRIMARY KEY (tid);
CREATE INDEX "tbmc_thread_authorUid_idx" ON tbm.tbmc_thread USING btree ("authorUid");
CREATE INDEX "tbmc_thread_postedAt_idx" ON tbm.tbmc_thread USING btree ("postedAt");
CREATE INDEX tbmc_thread_posts_query_by_fid_idx ON tbm.tbmc_thread USING btree (fid, "postedAt", tid);
CREATE INDEX tbmc_thread_sitemap_idx ON tbm.tbmc_thread USING btree (fid, tid, "postedAt");

CREATE TABLE tbm."tbmc_thread_missingFirstReply" (
    tid bigint NOT NULL,
    pid bigint,
    excerpt bytea,
    "lastSeenAt" integer
);
ALTER TABLE tbm."tbmc_thread_missingFirstReply" ADD CONSTRAINT "tbmc_thread_missingFirstReply_pkey" PRIMARY KEY (tid);
CREATE UNIQUE INDEX "tbmc_thread_missingFirstReply_pid_idx" ON tbm."tbmc_thread_missingFirstReply" USING btree (pid);

CREATE TABLE tbm.tbmc_user (
    uid bigint NOT NULL,
    name text,
    "displayName" bytea,
    portrait text NOT NULL,
    "portraitUpdatedAt" integer,
    gender smallint,
    "fansNickname" text,
    icon bytea,
    "ipGeolocation" text,
    "createdAt" integer NOT NULL,
    "updatedAt" integer
);
ALTER TABLE tbm.tbmc_user ADD CONSTRAINT tbmc_user_pkey PRIMARY KEY (uid);
CREATE INDEX "tbmc_user_displayName_idx" ON tbm.tbmc_user USING btree ("displayName");
CREATE INDEX tbmc_user_name_idx ON tbm.tbmc_user USING btree (name);

CREATE TABLE tbm."tbmcr_authorExpGrade" (
    "discoveredAt" integer NOT NULL,
    fid integer NOT NULL,
    uid bigint NOT NULL,
    "triggeredBy" tbm."tbmcr_triggeredBy" NOT NULL,
    "authorExpGrade" smallint NOT NULL
);
ALTER TABLE tbm."tbmcr_authorExpGrade" ADD CONSTRAINT "tbmcr_authorExpGrade_pkey" PRIMARY KEY (fid, uid, "discoveredAt");
CREATE INDEX "tbmcr_authorExpGrade_uid_idx" ON tbm."tbmcr_authorExpGrade" USING btree (uid);

CREATE TABLE tbm."tbmcr_forumModerator" (
    "discoveredAt" integer NOT NULL,
    fid integer NOT NULL,
    portrait text NOT NULL,
    "moderatorTypes" text NOT NULL
);
ALTER TABLE tbm."tbmcr_forumModerator" ADD CONSTRAINT "tbmcr_forumModerator_pkey" PRIMARY KEY (fid, portrait, "discoveredAt", "moderatorTypes");
CREATE INDEX "tbmcr_forumModerator_portrait_idx" ON tbm."tbmcr_forumModerator" USING btree (portrait);

CREATE TABLE tbm."tbmcr_latestReplier" (
    "takenAt" integer NOT NULL,
    id integer NOT NULL,
    uid bigint NOT NULL,
    name text,
    "displayName" bytea
);
ALTER TABLE tbm."tbmcr_latestReplier" ADD CONSTRAINT "tbmcr_latestReplier_pkey" PRIMARY KEY ("takenAt", uid);

CREATE TABLE tbm.tbmcr_reply (
    pid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "isFold" smallint,
    "disagreeCount" integer,
    geolocation bytea,
    "nullFieldsBitMask" smallint
);
ALTER TABLE tbm.tbmcr_reply ADD CONSTRAINT tbmcr_reply_pkey PRIMARY KEY (pid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_reply_agreeCount" (
    pid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "agreeCount" integer NOT NULL
);
ALTER TABLE tbm."tbmcr_reply_agreeCount" ADD CONSTRAINT "tbmcr_reply_agreeCount_pkey" PRIMARY KEY (pid, "takenAt", "duplicateIndex");

CREATE TABLE tbm.tbmcr_reply_floor (
    pid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    floor integer NOT NULL
);
ALTER TABLE tbm.tbmcr_reply_floor ADD CONSTRAINT tbmcr_reply_floor_pkey PRIMARY KEY (pid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_reply_subReplyCount" (
    pid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "subReplyCount" integer NOT NULL
);
ALTER TABLE tbm."tbmcr_reply_subReplyCount" ADD CONSTRAINT "tbmcr_reply_subReplyCount_pkey" PRIMARY KEY (pid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_subReply" (
    spid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "nullFieldsBitMask" smallint
);
ALTER TABLE tbm."tbmcr_subReply" ADD CONSTRAINT "tbmcr_subReply_pkey" PRIMARY KEY (spid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_subReply_agreeCount" (
    spid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "agreeCount" integer NOT NULL
);
ALTER TABLE tbm."tbmcr_subReply_agreeCount" ADD CONSTRAINT "tbmcr_subReply_agreeCount_pkey" PRIMARY KEY (spid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_subReply_disagreeCount" (
    spid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "disagreeCount" integer NOT NULL
);
ALTER TABLE tbm."tbmcr_subReply_disagreeCount" ADD CONSTRAINT "tbmcr_subReply_disagreeCount_pkey" PRIMARY KEY (spid, "takenAt", "duplicateIndex");

CREATE TABLE tbm.tbmcr_thread (
    tid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "threadType" bigint,
    "stickyType" text,
    "topicType" text,
    "isGood" smallint,
    "latestReplyPostedAt" integer,
    "latestReplierId" integer,
    "replyCount" integer,
    "shareCount" integer,
    "agreeCount" integer,
    "disagreeCount" integer,
    geolocation bytea,
    "nullFieldsBitMask" smallint
);
ALTER TABLE tbm.tbmcr_thread ADD CONSTRAINT tbmcr_thread_pkey PRIMARY KEY (tid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_thread_viewCount" (
    tid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "viewCount" integer NOT NULL
);
ALTER TABLE tbm."tbmcr_thread_viewCount" ADD CONSTRAINT "tbmcr_thread_viewCount_pkey" PRIMARY KEY (tid, "takenAt", "duplicateIndex");

CREATE TABLE tbm.tbmcr_user (
    uid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "triggeredBy" tbm."tbmcr_triggeredBy" NOT NULL,
    name text,
    portrait text,
    gender smallint,
    icon bytea,
    "nullFieldsBitMask" smallint
);
ALTER TABLE tbm.tbmcr_user ADD CONSTRAINT tbmcr_user_pkey PRIMARY KEY (uid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_user_displayName" (
    uid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "triggeredBy" tbm."tbmcr_triggeredBy" NOT NULL,
    "displayName" bytea
);
ALTER TABLE tbm."tbmcr_user_displayName" ADD CONSTRAINT "tbmcr_user_displayName_pkey" PRIMARY KEY (uid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_user_ipGeolocation" (
    uid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "triggeredBy" tbm."tbmcr_triggeredBy" NOT NULL,
    "ipGeolocation" text
);
ALTER TABLE tbm."tbmcr_user_ipGeolocation" ADD CONSTRAINT "tbmcr_user_ipGeolocation_pkey" PRIMARY KEY (uid, "takenAt", "duplicateIndex");

CREATE TABLE tbm."tbmcr_user_portraitUpdatedAt" (
    uid bigint NOT NULL,
    "takenAt" integer NOT NULL,
    "duplicateIndex" smallint NOT NULL,
    "triggeredBy" tbm."tbmcr_triggeredBy" NOT NULL,
    "portraitUpdatedAt" integer
);
ALTER TABLE tbm."tbmcr_user_portraitUpdatedAt" ADD CONSTRAINT "tbmcr_user_portraitUpdatedAt_pkey" PRIMARY KEY (uid, "takenAt", "duplicateIndex");

CREATE TABLE tbm.tbmi_failed (
    id integer NOT NULL,
    "imageId" integer NOT NULL,
    exception text NOT NULL
);
ALTER TABLE tbm.tbmi_failed ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME tbm.tbmi_failed_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);
ALTER TABLE tbm.tbmi_failed ADD CONSTRAINT tbmi_failed_pkey PRIMARY KEY (id);
CREATE INDEX "tbmi_failed_imageId_idx" ON tbm.tbmi_failed USING btree ("imageId");

CREATE TABLE tbm.tbmi_forum_script (
    fid integer NOT NULL,
    script tbm.tbmi_script NOT NULL
);
ALTER TABLE tbm.tbmi_forum_script ADD CONSTRAINT tbmi_forum_script_pkey PRIMARY KEY (fid, script);

CREATE TABLE tbm.tbmi_hash (
    "imageId" integer NOT NULL,
    "frameIndex" integer NOT NULL,
    "pHash" bytea NOT NULL,
    "averageHash" bytea NOT NULL,
    "blockMeanHash" bytea NOT NULL,
    "marrHildrethHash" bytea NOT NULL,
    "thumbHash" bytea NOT NULL
);
ALTER TABLE tbm.tbmi_hash ADD CONSTRAINT tbmi_hash_pkey PRIMARY KEY ("imageId", "frameIndex");

CREATE TABLE tbm."tbmi_imageInReply" (
    "imageId" integer NOT NULL,
    "urlFilename" text NOT NULL,
    "expectedByteSize" integer NOT NULL,
    "metadataConsumed" boolean NOT NULL,
    "hashConsumed" boolean NOT NULL,
    "qrCodeConsumed" boolean NOT NULL,
    "ocrConsumed" boolean NOT NULL
);
ALTER TABLE tbm."tbmi_imageInReply" ALTER COLUMN "imageId" ADD GENERATED BY DEFAULT AS IDENTITY (
    SEQUENCE NAME tbm."tbmi_imageInReply_imageId_seq"
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1
);
ALTER TABLE tbm."tbmi_imageInReply" ADD CONSTRAINT "tbmi_imageInReply_pkey" PRIMARY KEY ("imageId");
CREATE UNIQUE INDEX "tbmi_imageInReply_urlFilename_idx" ON tbm."tbmi_imageInReply" USING btree ("urlFilename");

CREATE TABLE tbm.tbmi_metadata (
    "imageId" integer NOT NULL,
    format tbm.tbmi_metadata_format NOT NULL,
    width integer NOT NULL,
    height integer NOT NULL,
    "bitsPerPixel" smallint NOT NULL,
    "frameCount" integer NOT NULL,
    "xxHash3" bytea NOT NULL
);
ALTER TABLE tbm.tbmi_metadata ADD CONSTRAINT tbmi_metadata_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_metadata_bmp (
    "imageId" integer NOT NULL,
    "infoHeaderType" tbm."tbmi_metadata_bmp_infoHeaderType" NOT NULL,
    "bitsPerPixel" tbm."tbmi_metadata_bmp_bitsPerPixel" NOT NULL
);
ALTER TABLE tbm.tbmi_metadata_bmp ADD CONSTRAINT tbmi_metadata_bmp_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm."tbmi_metadata_downloadedByteSize" (
    "imageId" integer NOT NULL,
    "downloadedByteSize" integer NOT NULL
);
ALTER TABLE tbm."tbmi_metadata_downloadedByteSize" ADD CONSTRAINT "tbmi_metadata_downloadedByteSize_pkey" PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_metadata_embedded_exif (
    "imageId" integer NOT NULL,
    orientation text,
    "imageDescription" text,
    "userComment" bytea,
    artist text,
    "xpAuthor" bytea,
    copyright text,
    "imageUniqueId" text,
    "bodySerialNumber" text,
    make text,
    model text,
    software text,
    "customRendered" smallint,
    "dateTime" timestamp with time zone,
    "dateTimeOffset" text,
    "dateTimeDigitized" timestamp with time zone,
    "dateTimeDigitizedOffset" text,
    "dateTimeOriginal" timestamp with time zone,
    "dateTimeOriginalOffset" text,
    "offsetTime" text,
    "offsetTimeDigitized" text,
    "offsetTimeOriginal" text,
    "gpsDateTime" timestamp with time zone,
    "gpsCoordinate" point,
    "gpsImgDirection" real,
    "gpsImgDirectionRef" text,
    "rawBytes" bytea,
    "xxHash3" bytea NOT NULL
);
ALTER TABLE tbm.tbmi_metadata_embedded_exif ADD CONSTRAINT tbmi_metadata_embedded_exif_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm."tbmi_metadata_embedded_exif_tagName" (
    "imageId" integer NOT NULL,
    name text NOT NULL
);
ALTER TABLE tbm."tbmi_metadata_embedded_exif_tagName" ADD CONSTRAINT "tbmi_metadata_embedded_exif_tagName_pkey" PRIMARY KEY ("imageId", name);

CREATE TABLE tbm.tbmi_metadata_embedded_icc (
    "imageId" integer NOT NULL,
    "rawBytes" bytea,
    "xxHash3" bytea NOT NULL
);
ALTER TABLE tbm.tbmi_metadata_embedded_icc ADD CONSTRAINT tbmi_metadata_embedded_icc_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_metadata_embedded_iptc (
    "imageId" integer NOT NULL,
    "rawBytes" bytea,
    "xxHash3" bytea NOT NULL
);
ALTER TABLE tbm.tbmi_metadata_embedded_iptc ADD CONSTRAINT tbmi_metadata_embedded_iptc_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_metadata_embedded_xmp (
    "imageId" integer NOT NULL,
    "rawBytes" bytea,
    "xxHash3" bytea NOT NULL
);
ALTER TABLE tbm.tbmi_metadata_embedded_xmp ADD CONSTRAINT tbmi_metadata_embedded_xmp_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_metadata_gif (
    "imageId" integer NOT NULL,
    "repeatCount" integer NOT NULL,
    "colorTableMode" tbm."tbmi_metadata_gif_colorTableMode" NOT NULL,
    "globalColorTableLength" integer NOT NULL,
    comments json
);
ALTER TABLE tbm.tbmi_metadata_gif ADD CONSTRAINT tbmi_metadata_gif_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_metadata_jpg (
    "imageId" integer NOT NULL,
    quality integer NOT NULL,
    "colorType" tbm."tbmi_metadata_jpg_colorType",
    interleaved boolean,
    progressive boolean
);
ALTER TABLE tbm.tbmi_metadata_jpg ADD CONSTRAINT tbmi_metadata_jpg_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_metadata_png (
    "imageId" integer NOT NULL,
    "bitDepth" tbm."tbmi_metadata_png_bitDepth",
    "colorType" tbm."tbmi_metadata_png_colorType",
    "interlaceMethod" tbm."tbmi_metadata_png_interlaceMethod",
    gamma real NOT NULL,
    "transparentR" smallint,
    "transparentG" smallint,
    "transparentB" smallint,
    "textData" jsonb
);
ALTER TABLE tbm.tbmi_metadata_png ADD CONSTRAINT tbmi_metadata_png_pkey PRIMARY KEY ("imageId");

CREATE TABLE tbm.tbmi_ocr_box (
    script tbm.tbmi_script NOT NULL,
    "imageId" integer NOT NULL,
    "frameIndex" smallint NOT NULL,
    "centerPointX" smallint NOT NULL,
    "centerPointY" smallint NOT NULL,
    width smallint NOT NULL,
    height smallint NOT NULL,
    "rotationDegrees" smallint NOT NULL,
    recognizer tbm.tbmi_ocr_box_recognizer NOT NULL,
    confidence smallint NOT NULL,
    text text NOT NULL
);
ALTER TABLE tbm.tbmi_ocr_box ADD CONSTRAINT tbmi_ocr_box_pkey PRIMARY KEY ("imageId", "frameIndex", "centerPointX", "centerPointY", width, height, "rotationDegrees", recognizer, script);

CREATE TABLE tbm.tbmi_ocr_line (
    script tbm.tbmi_script NOT NULL,
    "imageId" integer NOT NULL,
    "frameIndex" integer NOT NULL,
    "textLines" text NOT NULL
);
CREATE INDEX "tbmi_ocr_line_imageId_script_idx" ON tbm.tbmi_ocr_line USING btree ("imageId", script);

CREATE TABLE tbm."tbmi_qrCode" (
    "imageId" integer NOT NULL,
    "frameIndex" integer NOT NULL,
    "point1X" smallint NOT NULL,
    "point1Y" smallint NOT NULL,
    "point2X" smallint NOT NULL,
    "point2Y" smallint NOT NULL,
    "point3X" smallint NOT NULL,
    "point3Y" smallint NOT NULL,
    "point4X" smallint NOT NULL,
    "point4Y" smallint NOT NULL,
    text bytea
);
ALTER TABLE tbm."tbmi_qrCode" ADD CONSTRAINT "tbmi_qrCode_pkey" PRIMARY KEY ("imageId", "frameIndex", "point1X", "point1Y", "point2X", "point2Y", "point3X", "point3Y", "point4X", "point4Y");
