CREATE TABLE posts
(
    id           INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    title        TEXT        NOT NULL,
    cover_id     INTEGER,
    slug         TEXT UNIQUE NOT NULL,
    content      TEXT        NOT NULL,
    summary      TEXT        NOT NULL,
    status       SMALLINT    NOT NULL DEFAULT 1,
    sort         INTEGER              DEFAULT 0,
    published_at TIMESTAMPTZ   NOT NULL DEFAULT NOW(),
    created_at   TIMESTAMPTZ   NOT NULL DEFAULT NOW(),
    updated_at   TIMESTAMPTZ,

    CHECK (status IN (0, 1, 2))
);

CREATE TRIGGER update_posts_updated_at
    BEFORE UPDATE
    ON posts
    FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();


COMMENT ON COLUMN posts.id IS '主键ID';
COMMENT ON COLUMN posts.title IS '文章标题';
COMMENT ON COLUMN posts.cover_id IS '文章封面图片id';
COMMENT ON COLUMN posts.slug IS 'URL友好地址';
COMMENT ON COLUMN posts.content IS '文章正文';
COMMENT ON COLUMN posts.summary IS '摘要';
COMMENT ON COLUMN posts.status IS '状态 0:草稿 1:已发布 2:已下线';
COMMENT ON COLUMN posts.sort IS '文章排序';
COMMENT ON COLUMN posts.published_at IS '发布时间';
COMMENT ON COLUMN posts.created_at IS '创建时间';
COMMENT ON COLUMN posts.updated_at IS '修改时间';