MySQL 5.7 报 1101:`BLOB, TEXT, GEOMETRY or JSON column 'hashtags' can't have a default value`。而补列发生在**启动阶段**,ALTER 一失败应用就起不来,容器反复重启。 两处修: * `monitor_note.hashtags` 改成 `VARCHAR(2000)` —— 话题列表本来也不长,能带 DEFAULT; * `_column_ddl` 对 TEXT/BLOB **一律不加 DEFAULT**,靠 MySQL 给已有行填隐式默认值。 这是结构性保护:以后谁再给 NOT NULL 的 Text 列写 default,也不会再把启动弄挂。 并把「所有 NOT NULL 列都必须带 DEFAULT」那条测试限定到非 TEXT/BLOB —— 那条假设正是 这次事故的根源。
106 lines
4.0 KiB
Python
106 lines
4.0 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Guards for the in-place column migration.
|
||
|
||
``create_all`` creates missing tables but never adds columns to a table that
|
||
already exists, so new model columns are applied by ``_ensure_columns``. That step
|
||
was originally driven by a hand-kept list, and forgetting to update it did not
|
||
fail loudly -- the app still started, connected, and then failed on every query
|
||
and every scheduler tick. These tests pin down its replacement, which derives the
|
||
work from the model metadata.
|
||
"""
|
||
|
||
import pytest
|
||
from sqlalchemy import Boolean, Column, Integer, LargeBinary, MetaData, String, Table, Text
|
||
|
||
from api.monitor import db
|
||
from api.monitor.models import MonitorBase
|
||
|
||
|
||
def _migratable_columns():
|
||
for table in MonitorBase.metadata.sorted_tables:
|
||
for column in table.columns:
|
||
if column.primary_key:
|
||
continue
|
||
yield pytest.param(column, id=f"{table.name}.{column.name}")
|
||
|
||
|
||
def _ddl(column: Column) -> str:
|
||
"""Render a detached column, so the tests never mutate the real metadata."""
|
||
scratch = Table("scratch", MetaData(), column)
|
||
return db._column_ddl(scratch.columns[0])
|
||
|
||
|
||
@pytest.mark.parametrize("column", _migratable_columns())
|
||
def test_column_renders_as_ddl(column):
|
||
ddl = db._column_ddl(column)
|
||
|
||
# SQLAlchemy back-quotes an identifier only when it has to, so the rendered
|
||
# name matches the column's with the quoting stripped -- which is the case for
|
||
# reserved words like monitor_run.trigger. That quoting is a feature: the
|
||
# hand-kept list this replaced would have emitted bare `trigger` and died on a
|
||
# syntax error.
|
||
assert ddl.split(" ", 1)[0].strip("`") == column.name
|
||
# MySQL refuses AUTO_INCREMENT together with the DEFAULT this helper appends
|
||
# to NOT NULL columns.
|
||
assert "AUTO_INCREMENT" not in ddl
|
||
|
||
|
||
@pytest.mark.parametrize("column", _migratable_columns())
|
||
def test_not_null_columns_carry_a_default(column):
|
||
"""So ADD COLUMN cannot fail on a table that already holds rows.
|
||
|
||
Without a DEFAULT, whether the ALTER succeeds depends on the server's
|
||
sql_mode -- not something a deployment should hinge on.
|
||
"""
|
||
if column.nullable:
|
||
pytest.skip("nullable column needs no seed value")
|
||
# TEXT/BLOB 例外:MySQL 5.7 直接拒绝 DEFAULT(报 1101)。这类列靠 MySQL 给已有行
|
||
# 填隐式默认值 —— 真踩过,启动时的 ALTER 失败会让整个服务反复重启。
|
||
if isinstance(column.type, (Text, LargeBinary)):
|
||
pytest.skip("TEXT/BLOB 不能带 DEFAULT(MySQL 5.7)")
|
||
|
||
assert "DEFAULT" in db._column_ddl(column)
|
||
|
||
|
||
def test_boolean_default_becomes_a_mysql_literal():
|
||
"""Python's True is not a SQL keyword; it has to become 1."""
|
||
assert "DEFAULT 1" in _ddl(Column("flag", Boolean, nullable=False, default=True))
|
||
|
||
|
||
def test_string_defaults_are_quoted():
|
||
assert "DEFAULT 'interval'" in _ddl(
|
||
Column("mode", String(16), nullable=False, default="interval")
|
||
)
|
||
|
||
|
||
def test_integer_defaults_are_not_quoted():
|
||
ddl = _ddl(Column("n", Integer, nullable=False, default=0))
|
||
|
||
assert "DEFAULT 0" in ddl
|
||
assert "DEFAULT '0'" not in ddl
|
||
|
||
|
||
def test_a_not_null_column_without_a_model_default_falls_back_to_zero():
|
||
"""Belt and braces: even a column the model gives no default still migrates."""
|
||
ddl = _ddl(Column("n", Integer, nullable=False))
|
||
|
||
assert "DEFAULT 0" in ddl
|
||
|
||
|
||
def test_text_columns_never_get_a_default():
|
||
"""MySQL 5.7 不允许 TEXT/BLOB 列带 DEFAULT。
|
||
|
||
补列在**启动阶段**跑:一旦拼出 `TEXT NOT NULL DEFAULT ''`,ALTER 直接报 1101,
|
||
应用起不来、容器反复重启。真的踩过(hashtags 列)。
|
||
"""
|
||
ddl = db._column_ddl(Column("hashtags", Text, nullable=False))
|
||
|
||
assert "DEFAULT" not in ddl.upper()
|
||
|
||
|
||
def test_varchar_columns_still_get_a_default():
|
||
"""VARCHAR 可以带 DEFAULT,而且带了对已有行更友好 —— 别一刀切。"""
|
||
ddl = db._column_ddl(Column("hashtags", String(2000), nullable=False))
|
||
|
||
assert "DEFAULT ''" in ddl
|