refactor: 移除迁移脚本中不必要的 backfill SQL

快照字段(username/api_key_name)已在业务层写入时填充,无需迁移时回填历史数据
This commit is contained in:
fawney19
2026-03-09 12:17:45 +08:00
parent 0a256adc94
commit 40736a8334
2 changed files with 2 additions and 51 deletions

View File

@@ -7,9 +7,9 @@ Create Date: 2026-03-08 03:48:49.622091+00:00
"""
import sqlalchemy as sa
from helpers import new_cache, replace_fk_if_needed
from alembic import op
from helpers import new_cache, replace_fk_if_needed
# revision identifiers, used by Alembic.
revision = "45b118150a78"
@@ -80,31 +80,6 @@ def upgrade() -> None:
),
)
# --- Backfill: populate snapshots from existing FK joins ---
# One LEFT JOIN UPDATE per table covers all rows regardless of which FK is present.
op.execute("""
UPDATE usage u
SET username = COALESCE(u.username, usr.username),
api_key_name = COALESCE(u.api_key_name, ak.name)
FROM usage u2
LEFT JOIN users usr ON usr.id = u2.user_id
LEFT JOIN api_keys ak ON ak.id = u2.api_key_id
WHERE u.id = u2.id
AND (u.username IS NULL OR u.api_key_name IS NULL)
""")
op.execute("""
UPDATE stats_user_daily s
SET username = usr.username
FROM users usr
WHERE s.user_id = usr.id AND s.username IS NULL
""")
op.execute("""
UPDATE stats_daily_api_key s
SET api_key_name = ak.name
FROM api_keys ak
WHERE s.api_key_id = ak.id AND s.api_key_name IS NULL
""")
def downgrade() -> None:
c = new_cache()

View File

@@ -7,9 +7,9 @@ Create Date: 2026-03-08 12:15:00.000000+00:00
"""
import sqlalchemy as sa
from helpers import new_cache, replace_fk_if_needed
from alembic import op
from helpers import new_cache, replace_fk_if_needed
# revision identifiers, used by Alembic.
revision = "13a4c8f6d9e0"
@@ -100,30 +100,6 @@ def upgrade() -> None:
"SET NULL",
)
# --- Backfill: populate snapshots from existing FK joins ---
# One LEFT JOIN UPDATE per table covers all rows regardless of which FK is present.
op.execute("""
UPDATE request_candidates rc
SET username = COALESCE(rc.username, usr.username),
api_key_name = COALESCE(rc.api_key_name, ak.name)
FROM request_candidates rc2
LEFT JOIN users usr ON usr.id = rc2.user_id
LEFT JOIN api_keys ak ON ak.id = rc2.api_key_id
WHERE rc.id = rc2.id
AND (rc.username IS NULL OR rc.api_key_name IS NULL)
""")
op.execute("""
UPDATE video_tasks vt
SET username = COALESCE(vt.username, usr.username),
api_key_name = COALESCE(vt.api_key_name, ak.name)
FROM video_tasks vt2
LEFT JOIN users usr ON usr.id = vt2.user_id
LEFT JOIN api_keys ak ON ak.id = vt2.api_key_id
WHERE vt.id = vt2.id
AND (vt.username IS NULL OR vt.api_key_name IS NULL)
""")
def downgrade() -> None:
c = new_cache()