BEGIN;

CREATE TABLE IF NOT EXISTS "post" (
    "id" SERIAL PRIMARY KEY,
    "title" TEXT NOT NULL,
    "content" TEXT NOT NULL,
    "description" TEXT NOT NULL,
    "preview_image_url" TEXT NOT NULL,
    "published_time" TIMESTAMP,
    "created_time" TIMESTAMP NOT NULL,
    "updated_time" TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS "label" (
    "id" SERIAL PRIMARY KEY,
    "name" TEXT NOT NULL,
    "color" TEXT NOT NULL,
    "created_time" TIMESTAMP NOT NULL,
    "updated_time" TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS "post_label" (
    "post_id" SERIAL NOT NULL,
    "label_id" SERIAL NOT NULL,
    "label_order" INTEGER NOT NULL,
    PRIMARY KEY ("post_id", "label_id"),
    FOREIGN KEY ("post_id") REFERENCES "post" ("id") ON DELETE CASCADE,
    FOREIGN KEY ("label_id") REFERENCES "label" ("id") ON DELETE CASCADE
);

COMMIT;