refactor: replace post_status ENUM with TEXT + CHECK constraint

This commit is contained in:
xfy 2026-06-01 18:29:46 +08:00
parent 42ed54a752
commit 327738c2e0
2 changed files with 4 additions and 5 deletions

View File

@ -1,5 +1,3 @@
CREATE TYPE post_status AS ENUM ('draft', 'published');
CREATE TABLE posts ( CREATE TABLE posts (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
author_id INT NOT NULL REFERENCES users(id) ON DELETE RESTRICT, author_id INT NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
@ -11,14 +9,15 @@ CREATE TABLE posts (
content_md TEXT NOT NULL, content_md TEXT NOT NULL,
content_html TEXT, content_html TEXT,
status post_status NOT NULL DEFAULT 'draft', status TEXT NOT NULL DEFAULT 'draft',
published_at TIMESTAMPTZ, published_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
deleted_at TIMESTAMPTZ, 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; CREATE INDEX idx_posts_status_published ON posts(status, published_at DESC) WHERE deleted_at IS NULL;

View File

@ -634,7 +634,7 @@ pub async fn update_post(
}; };
tx.execute( 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", WHERE id = $8",
&[ &[
&title.trim(), &title.trim(),