Skip to content

Commit

Permalink
create migration that updates tables to new standards
Browse files Browse the repository at this point in the history
  • Loading branch information
brassy-endomorph committed Sep 20, 2024
1 parent 0534ce4 commit c5fecbd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
20 changes: 12 additions & 8 deletions migrations/versions/46aedec8fd9b_create_alias_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ 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")

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")
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"],
Expand Down
Original file line number Diff line number Diff line change
@@ -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"))

0 comments on commit c5fecbd

Please sign in to comment.