Files

41 lines
1.4 KiB
Python

"""Сохранение допустимого Unicode имён и названий, включая U+0000."""
import sqlalchemy as sa
from alembic import op
revision = "006"
down_revision = "005"
branch_labels = None
depends_on = None
def upgrade():
for table, column in [("users", "display_name"), ("chats", "title")]:
op.drop_constraint(f"{table}_{column}_check", table, type_="check")
op.alter_column(
table,
column,
existing_type=sa.String(100),
type_=sa.LargeBinary(),
existing_nullable=False,
postgresql_using=f"convert_to({column}, 'UTF8')",
)
op.create_check_constraint(
f"{table}_{column}_bytes_check", table, f"octet_length({column}) BETWEEN 1 AND 400"
)
def downgrade():
# PostgreSQL откажет при U+0000 и откатит транзакцию вместо потери символов.
for table, column in [("users", "display_name"), ("chats", "title")]:
op.drop_constraint(f"{table}_{column}_bytes_check", table, type_="check")
op.alter_column(
table,
column,
existing_type=sa.LargeBinary(),
type_=sa.String(100),
existing_nullable=False,
postgresql_using=f"convert_from({column}, 'UTF8')",
)
op.create_check_constraint(f"{table}_{column}_check", table, f"length(trim({column})) > 0")