refactor(todo): 移除 due_at 字段,改用 order 字段管理象限内顺序
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
"""replace todo due_at with order field
|
||||
|
||||
Revision ID: 202603200001
|
||||
Revises: 20260319_0004
|
||||
Create Date: 2026-03-20 12:00:00
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision: str = "202603200001"
|
||||
down_revision: Union[str, Sequence[str], None] = "20260319_0004"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
columns = {column["name"] for column in inspector.get_columns("todos")}
|
||||
has_order = "order" in columns
|
||||
has_due_at = "due_at" in columns
|
||||
|
||||
if not has_order:
|
||||
op.add_column("todos", sa.Column("order", sa.Integer(), nullable=True))
|
||||
|
||||
if has_due_at:
|
||||
op.execute(
|
||||
"""
|
||||
WITH ranked AS (
|
||||
SELECT
|
||||
id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY owner_id, priority
|
||||
ORDER BY due_at NULLS LAST, created_at ASC, id ASC
|
||||
) - 1 AS seq
|
||||
FROM todos
|
||||
WHERE deleted_at IS NULL
|
||||
)
|
||||
UPDATE todos t
|
||||
SET "order" = ranked.seq
|
||||
FROM ranked
|
||||
WHERE t.id = ranked.id
|
||||
"""
|
||||
)
|
||||
else:
|
||||
op.execute(
|
||||
"""
|
||||
WITH ranked AS (
|
||||
SELECT
|
||||
id,
|
||||
ROW_NUMBER() OVER (
|
||||
PARTITION BY owner_id, priority
|
||||
ORDER BY created_at ASC, id ASC
|
||||
) - 1 AS seq
|
||||
FROM todos
|
||||
WHERE deleted_at IS NULL
|
||||
)
|
||||
UPDATE todos t
|
||||
SET "order" = ranked.seq
|
||||
FROM ranked
|
||||
WHERE t.id = ranked.id AND t."order" IS NULL
|
||||
"""
|
||||
)
|
||||
|
||||
op.execute('UPDATE todos SET "order" = 0 WHERE "order" IS NULL')
|
||||
op.alter_column("todos", "order", nullable=False, server_default=sa.text("0"))
|
||||
|
||||
op.execute("DROP INDEX IF EXISTS ix_todos_owner_status_due")
|
||||
op.execute("DROP INDEX IF EXISTS ix_todos_pending_due")
|
||||
|
||||
op.execute(
|
||||
'CREATE INDEX IF NOT EXISTS ix_todos_owner_status_order ON todos (owner_id, status, priority, "order")'
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_todos_pending_order ON todos (owner_id, priority, \"order\") WHERE status = 'pending'"
|
||||
)
|
||||
|
||||
if has_due_at:
|
||||
op.drop_column("todos", "due_at")
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
inspector = sa.inspect(bind)
|
||||
columns = {column["name"] for column in inspector.get_columns("todos")}
|
||||
has_order = "order" in columns
|
||||
has_due_at = "due_at" in columns
|
||||
|
||||
if not has_due_at:
|
||||
op.add_column(
|
||||
"todos", sa.Column("due_at", sa.DateTime(timezone=True), nullable=True)
|
||||
)
|
||||
|
||||
op.execute("DROP INDEX IF EXISTS ix_todos_pending_order")
|
||||
op.execute("DROP INDEX IF EXISTS ix_todos_owner_status_order")
|
||||
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_todos_owner_status_due ON todos (owner_id, status, due_at)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_todos_pending_due ON todos (owner_id, due_at) WHERE status = 'pending'"
|
||||
)
|
||||
|
||||
if has_order:
|
||||
op.drop_column("todos", "order")
|
||||
Reference in New Issue
Block a user