fix(migrations): guard migration 036 against missing user_profiles table
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
@@ -24,21 +24,47 @@ depends_on: Union[str, None] = None
|
|||||||
|
|
||||||
def upgrade() -> None:
|
def upgrade() -> None:
|
||||||
"""Add translation columns to files and user_profiles."""
|
"""Add translation columns to files and user_profiles."""
|
||||||
with op.batch_alter_table("files") as batch_op:
|
conn = op.get_bind()
|
||||||
batch_op.add_column(sa.Column("detected_language", sa.String(10), nullable=True))
|
inspector = sa.inspect(conn)
|
||||||
batch_op.add_column(sa.Column("default_language_text", sa.Text(), nullable=True))
|
|
||||||
batch_op.add_column(sa.Column("default_language_code", sa.String(10), nullable=True))
|
|
||||||
|
|
||||||
with op.batch_alter_table("user_profiles") as batch_op:
|
if "files" in inspector.get_table_names():
|
||||||
batch_op.add_column(sa.Column("default_document_language", sa.String(10), nullable=True))
|
existing_file_cols = {col["name"] for col in inspector.get_columns("files")}
|
||||||
|
cols_to_add = {"detected_language", "default_language_text", "default_language_code"} - existing_file_cols
|
||||||
|
if cols_to_add:
|
||||||
|
with op.batch_alter_table("files") as batch_op:
|
||||||
|
if "detected_language" in cols_to_add:
|
||||||
|
batch_op.add_column(sa.Column("detected_language", sa.String(10), nullable=True))
|
||||||
|
if "default_language_text" in cols_to_add:
|
||||||
|
batch_op.add_column(sa.Column("default_language_text", sa.Text(), nullable=True))
|
||||||
|
if "default_language_code" in cols_to_add:
|
||||||
|
batch_op.add_column(sa.Column("default_language_code", sa.String(10), nullable=True))
|
||||||
|
|
||||||
|
if "user_profiles" in inspector.get_table_names():
|
||||||
|
existing_profile_cols = {col["name"] for col in inspector.get_columns("user_profiles")}
|
||||||
|
if "default_document_language" not in existing_profile_cols:
|
||||||
|
with op.batch_alter_table("user_profiles") as batch_op:
|
||||||
|
batch_op.add_column(sa.Column("default_document_language", sa.String(10), nullable=True))
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
def downgrade() -> None:
|
||||||
"""Remove translation columns."""
|
"""Remove translation columns."""
|
||||||
with op.batch_alter_table("user_profiles") as batch_op:
|
conn = op.get_bind()
|
||||||
batch_op.drop_column("default_document_language")
|
inspector = sa.inspect(conn)
|
||||||
|
|
||||||
with op.batch_alter_table("files") as batch_op:
|
if "user_profiles" in inspector.get_table_names():
|
||||||
batch_op.drop_column("default_language_code")
|
existing_profile_cols = {col["name"] for col in inspector.get_columns("user_profiles")}
|
||||||
batch_op.drop_column("default_language_text")
|
if "default_document_language" in existing_profile_cols:
|
||||||
batch_op.drop_column("detected_language")
|
with op.batch_alter_table("user_profiles") as batch_op:
|
||||||
|
batch_op.drop_column("default_document_language")
|
||||||
|
|
||||||
|
if "files" in inspector.get_table_names():
|
||||||
|
existing_file_cols = {col["name"] for col in inspector.get_columns("files")}
|
||||||
|
cols_to_drop = {"detected_language", "default_language_text", "default_language_code"} & existing_file_cols
|
||||||
|
if cols_to_drop:
|
||||||
|
with op.batch_alter_table("files") as batch_op:
|
||||||
|
if "default_language_code" in cols_to_drop:
|
||||||
|
batch_op.drop_column("default_language_code")
|
||||||
|
if "default_language_text" in cols_to_drop:
|
||||||
|
batch_op.drop_column("default_language_text")
|
||||||
|
if "detected_language" in cols_to_drop:
|
||||||
|
batch_op.drop_column("detected_language")
|
||||||
|
|||||||
Reference in New Issue
Block a user