From c5fecbd2aba3a6ef86ba6553e1bf4c0d91a4f2ea Mon Sep 17 00:00:00 2001 From: brassy endomorph Date: Fri, 20 Sep 2024 17:19:19 +0000 Subject: [PATCH] create migration that updates tables to new standards --- .../46aedec8fd9b_create_alias_tables.py | 20 ++++++++----- ...c8e9a_update_entities_to_match_metadata.py | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 migrations/versions/5ffe5a5c8e9a_update_entities_to_match_metadata.py diff --git a/migrations/versions/46aedec8fd9b_create_alias_tables.py b/migrations/versions/46aedec8fd9b_create_alias_tables.py index c735a9d1..1db03f1e 100644 --- a/migrations/versions/46aedec8fd9b_create_alias_tables.py +++ b/migrations/versions/46aedec8fd9b_create_alias_tables.py @@ -102,10 +102,10 @@ def upgrade() -> None: with op.batch_alter_table("message", schema=None) as batch_op: batch_op.alter_column("username_id", existing_type=sa.Integer(), nullable=False) - batch_op.drop_constraint(op.f("message_secondary_user_id_fkey"), type_="foreignkey") - batch_op.drop_constraint(op.f("message_user_id_fkey"), type_="foreignkey") + batch_op.drop_constraint(batch_op.f("message_secondary_user_id_fkey"), type_="foreignkey") + batch_op.drop_constraint(batch_op.f("message_user_id_fkey"), type_="foreignkey") batch_op.create_foreign_key( - op.f("fk_message_username_id_usernames"), "usernames", ["username_id"], ["id"] + batch_op.f("fk_message_username_id_usernames"), "usernames", ["username_id"], ["id"] ) batch_op.drop_column("user_id") batch_op.drop_column("secondary_user_id") @@ -113,7 +113,7 @@ def upgrade() -> None: op.drop_table("secondary_usernames") with op.batch_alter_table("users", schema=None) as batch_op: - batch_op.drop_constraint(op.f("users_primary_username_key"), type_="unique") + batch_op.drop_constraint(batch_op.f("users_primary_username_key"), type_="unique") batch_op.drop_column("primary_username") batch_op.drop_column("display_name") batch_op.drop_column("bio") @@ -145,7 +145,9 @@ def downgrade() -> None: batch_op.add_column(sa.Column(f"extra_field_label{i}", sa.VARCHAR(), nullable=True)) batch_op.add_column(sa.Column(f"extra_field_verified{i}", sa.BOOLEAN(), nullable=True)) - batch_op.create_unique_constraint(op.f("users_primary_username_key"), ["primary_username"]) + batch_op.create_unique_constraint( + batch_op.f("users_primary_username_key"), ["primary_username"] + ) users_insert_str = "" for field in user_common_fields: @@ -238,10 +240,12 @@ def downgrade() -> None: ) with op.batch_alter_table("message", schema=None) as batch_op: - batch_op.drop_constraint(op.f("fk_message_username_id_usernames"), type_="foreignkey") - batch_op.create_foreign_key(op.f("message_user_id_fkey"), "users", ["user_id"], ["id"]) + batch_op.drop_constraint(batch_op.f("fk_message_username_id_usernames"), type_="foreignkey") + batch_op.create_foreign_key( + batch_op.f("message_user_id_fkey"), "users", ["user_id"], ["id"] + ) batch_op.create_foreign_key( - op.f("message_secondary_user_id_fkey"), + batch_op.f("message_secondary_user_id_fkey"), "secondary_usernames", ["secondary_user_id"], ["id"], diff --git a/migrations/versions/5ffe5a5c8e9a_update_entities_to_match_metadata.py b/migrations/versions/5ffe5a5c8e9a_update_entities_to_match_metadata.py new file mode 100644 index 00000000..65b56c84 --- /dev/null +++ b/migrations/versions/5ffe5a5c8e9a_update_entities_to_match_metadata.py @@ -0,0 +1,30 @@ +"""update entities to match metadata + +Revision ID: 5ffe5a5c8e9a +Revises: 46aedec8fd9b +Create Date: 2024-09-20 17:09:10.819963 + +""" + +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = "5ffe5a5c8e9a" +down_revision = "46aedec8fd9b" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.execute(sa.text("ALTER INDEX invite_code_code_key RENAME TO uq_invite_codes_code")) + op.execute(sa.text("ALTER INDEX usernames_username_key RENAME TO uq_usernames_username")) + op.execute(sa.text("ALTER TABLE invite_code RENAME TO invite_codes")) + op.execute(sa.text("ALTER TABLE message RENAME TO messages")) + + +def downgrade() -> None: + op.execute(sa.text("ALTER INDEX uq_usernames_username RENAME TO usernames_username_key")) + op.execute(sa.text("ALTER TABLE messages RENAME TO message")) + op.execute(sa.text("ALTER TABLE invite_codes RENAME TO invite_code")) + op.execute(sa.text("ALTER INDEX uq_invite_codes_code RENAME TO invite_code_code_key"))