From 327738c2e0330abf73fd7304bc5ca64692c2bee6 Mon Sep 17 00:00:00 2001 From: xfy Date: Mon, 1 Jun 2026 18:29:46 +0800 Subject: [PATCH] refactor: replace post_status ENUM with TEXT + CHECK constraint --- migrations/002_posts.sql | 7 +++---- src/api/posts.rs | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/migrations/002_posts.sql b/migrations/002_posts.sql index 284a83a..a9c2de7 100644 --- a/migrations/002_posts.sql +++ b/migrations/002_posts.sql @@ -1,5 +1,3 @@ -CREATE TYPE post_status AS ENUM ('draft', 'published'); - CREATE TABLE posts ( id SERIAL PRIMARY KEY, author_id INT NOT NULL REFERENCES users(id) ON DELETE RESTRICT, @@ -11,14 +9,15 @@ CREATE TABLE posts ( content_md TEXT NOT NULL, content_html TEXT, - status post_status NOT NULL DEFAULT 'draft', + status TEXT NOT NULL DEFAULT 'draft', published_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), deleted_at TIMESTAMPTZ, - CONSTRAINT posts_slug_unique UNIQUE (slug) + CONSTRAINT posts_slug_unique UNIQUE (slug), + CONSTRAINT posts_status_check CHECK (status IN ('draft', 'published')) ); CREATE INDEX idx_posts_status_published ON posts(status, published_at DESC) WHERE deleted_at IS NULL; diff --git a/src/api/posts.rs b/src/api/posts.rs index f1d0e8a..c09b3fb 100644 --- a/src/api/posts.rs +++ b/src/api/posts.rs @@ -634,7 +634,7 @@ pub async fn update_post( }; tx.execute( - "UPDATE posts SET title = $1, slug = $2, summary = $3, content_md = $4, content_html = $5, status = $6, published_at = $7, updated_at = NOW() + "UPDATE posts SET title = $1, slug = $2, summary = $3, content_md = $4, content_html = $5, status = $6, published_at = $7, updated_at = NOW() WHERE id = $8", &[ &title.trim(),