Skip to content

Commit

Permalink
Merge pull request #612 from scidsg/cleanup-migrations
Browse files Browse the repository at this point in the history
Cleanup migrations
  • Loading branch information
brassy-endomorph committed Sep 23, 2024
2 parents 935551f + 5e7f522 commit 16c4b5d
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 146 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ revision: migrate-prod ## Create a new migration
ifndef message
$(error 'message' must be set when invoking the revision target, eg `make revision message="short message"`)
endif
. ./dev_env.sh && \
poetry run flask db revision -m "$(message)" --autogenerate

.PHONY: test
Expand Down
2 changes: 1 addition & 1 deletion hushline/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
}
)

db = SQLAlchemy()
db = SQLAlchemy(metadata=metadata)
4 changes: 4 additions & 0 deletions hushline/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ def __init__(


class Message(Model):
__tablename__ = "messages"

id: Mapped[int] = mapped_column(primary_key=True)
_content: Mapped[str] = mapped_column("content", db.Text) # Encrypted content stored here
username_id: Mapped[int] = mapped_column(db.ForeignKey("usernames.id"))
Expand All @@ -286,6 +288,8 @@ def content(self, value: str) -> None:


class InviteCode(Model):
__tablename__ = "invite_codes"

id: Mapped[int] = mapped_column(primary_key=True)
code: Mapped[str] = mapped_column(db.String(255), unique=True)
expiration_date: Mapped[datetime]
Expand Down
8 changes: 2 additions & 6 deletions migrations/versions/166a3402c391_add_extra_fields_to_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
depends_on = None


def upgrade():
def upgrade() -> None:
with op.batch_alter_table("users") as batch_op:
batch_op.add_column(sa.Column("extra_field_label1", sa.String(), nullable=True))
batch_op.add_column(sa.Column("extra_field_value1", sa.String(), nullable=True))
Expand All @@ -28,10 +28,8 @@ def upgrade():
batch_op.add_column(sa.Column("extra_field_label4", sa.String(), nullable=True))
batch_op.add_column(sa.Column("extra_field_value4", sa.String(), nullable=True))

# ### end Alembic commands ###


def downgrade():
def downgrade() -> None:
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.drop_column("extra_field_value4")
batch_op.drop_column("extra_field_label4")
Expand All @@ -41,5 +39,3 @@ def downgrade():
batch_op.drop_column("extra_field_label2")
batch_op.drop_column("extra_field_value1")
batch_op.drop_column("extra_field_label1")

# ### end Alembic commands ###
97 changes: 42 additions & 55 deletions migrations/versions/46aedec8fd9b_create_alias_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "46aedec8fd9b"
down_revision = "c2b6eff6e213"
down_revision = "62551ed63cbf"
branch_labels = None
depends_on = None

Expand Down Expand Up @@ -55,9 +54,10 @@ def upgrade() -> None:
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
op.f("usernames_user_id_fkey"),
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("username"),
sa.PrimaryKeyConstraint("id", name=op.f("usernames_pkey")),
sa.UniqueConstraint("username", name=op.f("usernames_username_key")),
)

op.execute(
Expand Down Expand Up @@ -102,16 +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("message_secondary_user_id_fkey", type_="foreignkey")
batch_op.drop_constraint("message_user_id_fkey", type_="foreignkey")
batch_op.create_foreign_key(None, "usernames", ["username_id"], ["id"])
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(
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("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 All @@ -126,39 +128,26 @@ def upgrade() -> None:

def downgrade() -> None:
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.add_column(
sa.Column("display_name", sa.VARCHAR(length=80), autoincrement=False, nullable=True)
)
batch_op.add_column(sa.Column("display_name", sa.VARCHAR(length=80), nullable=True))
batch_op.add_column(
sa.Column(
"primary_username",
sa.VARCHAR(length=80),
autoincrement=False,
nullable=True,
)
)
batch_op.add_column(
sa.Column("is_verified", sa.BOOLEAN(), autoincrement=False, nullable=True)
)
batch_op.add_column(sa.Column("bio", sa.TEXT(), autoincrement=False, nullable=True))
batch_op.add_column(
sa.Column("show_in_directory", sa.BOOLEAN(), autoincrement=False, nullable=True)
)
batch_op.add_column(sa.Column("is_verified", sa.BOOLEAN(), nullable=True))
batch_op.add_column(sa.Column("bio", sa.TEXT(), nullable=True))
batch_op.add_column(sa.Column("show_in_directory", sa.BOOLEAN(), nullable=True))

for i in range(1, 5):
batch_op.add_column(
sa.Column(f"extra_field_value{i}", sa.VARCHAR(), autoincrement=False, nullable=True)
)
batch_op.add_column(
sa.Column(f"extra_field_label{i}", sa.VARCHAR(), autoincrement=False, nullable=True)
)
batch_op.add_column(
sa.Column(
f"extra_field_verified{i}", sa.BOOLEAN(), autoincrement=False, nullable=True
)
)
batch_op.add_column(sa.Column(f"extra_field_value{i}", sa.VARCHAR(), nullable=True))
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("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 All @@ -182,24 +171,17 @@ def downgrade() -> None:
)

with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.alter_column(
"is_verified", existing_type=sa.BOOLEAN(), autoincrement=False, nullable=False
)
batch_op.alter_column("is_verified", existing_type=sa.BOOLEAN(), nullable=False)
batch_op.alter_column(
"primary_username",
existing_type=sa.VARCHAR(length=80),
autoincrement=False,
nullable=False,
)
batch_op.alter_column(
"show_in_directory", existing_type=sa.BOOLEAN(), autoincrement=False, nullable=False
)
batch_op.alter_column("show_in_directory", existing_type=sa.BOOLEAN(), nullable=False)

with op.batch_alter_table("message", schema=None) as batch_op:
batch_op.add_column(sa.Column("user_id", sa.INTEGER(), autoincrement=False, nullable=True))
batch_op.add_column(
sa.Column("secondary_user_id", sa.INTEGER(), autoincrement=False, nullable=True)
)
batch_op.add_column(sa.Column("user_id", sa.INTEGER(), nullable=True))
batch_op.add_column(sa.Column("secondary_user_id", sa.INTEGER(), nullable=True))

op.execute(
sa.text(
Expand Down Expand Up @@ -231,19 +213,19 @@ def downgrade() -> None:
)

with op.batch_alter_table("message", schema=None) as batch_op:
batch_op.alter_column(
"user_id", existing_type=sa.INTEGER(), autoincrement=False, nullable=False
)
batch_op.alter_column("user_id", existing_type=sa.INTEGER(), nullable=False)

op.create_table(
"secondary_usernames",
sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column("username", sa.VARCHAR(length=80), autoincrement=False, nullable=False),
sa.Column("user_id", sa.INTEGER(), autoincrement=False, nullable=False),
sa.Column("display_name", sa.VARCHAR(length=80), autoincrement=False, nullable=True),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], name="secondary_usernames_user_id_fkey"),
sa.PrimaryKeyConstraint("id", name="secondary_usernames_pkey"),
sa.UniqueConstraint("username", name="secondary_usernames_username_key"),
sa.Column("id", sa.INTEGER(), nullable=False),
sa.Column("username", sa.VARCHAR(length=80), nullable=False),
sa.Column("user_id", sa.INTEGER(), nullable=False),
sa.Column("display_name", sa.VARCHAR(length=80), nullable=True),
sa.ForeignKeyConstraint(
["user_id"], ["users.id"], name=op.f("secondary_usernames_user_id_fkey")
),
sa.PrimaryKeyConstraint("id", name=op.f("secondary_usernames_pkey")),
sa.UniqueConstraint("username", name=op.f("secondary_usernames_username_key")),
)

op.execute(
Expand All @@ -258,10 +240,15 @@ def downgrade() -> None:
)

with op.batch_alter_table("message", schema=None) as batch_op:
batch_op.drop_constraint("message_username_id_fkey", type_="foreignkey")
batch_op.create_foreign_key("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(
"message_secondary_user_id_fkey", "secondary_usernames", ["secondary_user_id"], ["id"]
batch_op.f("message_secondary_user_id_fkey"),
"secondary_usernames",
["secondary_user_id"],
["id"],
)
batch_op.drop_column("username_id")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,9 @@
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###

def upgrade() -> None:
op.add_column("authentication_logs", sa.Column("timecode", sa.Integer(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
def downgrade() -> None:
op.drop_column("authentication_logs", "timecode")
# ### end Alembic commands ###
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"))
13 changes: 3 additions & 10 deletions migrations/versions/62551ed63cbf_make_boolean_fields_required.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,24 @@
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "62551ed63cbf"
down_revision = "166a3402c391"
down_revision = "83a6b3b09eca"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
def upgrade() -> None:
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.alter_column("password_hash", existing_type=sa.VARCHAR(length=512), nullable=False)
batch_op.alter_column("is_verified", existing_type=sa.BOOLEAN(), nullable=False)
batch_op.alter_column("is_admin", existing_type=sa.BOOLEAN(), nullable=False)
batch_op.alter_column("show_in_directory", existing_type=sa.BOOLEAN(), nullable=False)

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
def downgrade() -> None:
with op.batch_alter_table("users", schema=None) as batch_op:
batch_op.alter_column("show_in_directory", existing_type=sa.BOOLEAN(), nullable=True)
batch_op.alter_column("is_admin", existing_type=sa.BOOLEAN(), nullable=True)
batch_op.alter_column("is_verified", existing_type=sa.BOOLEAN(), nullable=True)
batch_op.alter_column("password_hash", existing_type=sa.VARCHAR(length=512), nullable=True)

# ### end Alembic commands ###
25 changes: 12 additions & 13 deletions migrations/versions/691dba936e64_initial_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
def upgrade() -> None:
op.create_table(
"invite_code",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("code", sa.String(length=255), nullable=False),
sa.Column("expiration_date", sa.DateTime(), nullable=False),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("code"),
sa.PrimaryKeyConstraint("id", name=op.f("invite_code_pkey")),
sa.UniqueConstraint("code", name=op.f("invite_code_code_key")),
)
op.create_table(
"users",
Expand All @@ -44,8 +43,8 @@ def upgrade():
sa.Column("is_admin", sa.Boolean(), nullable=True),
sa.Column("show_in_directory", sa.Boolean(), nullable=True),
sa.Column("bio", sa.Text(), nullable=True),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("primary_username"),
sa.PrimaryKeyConstraint("id", name=op.f("users_pkey")),
sa.UniqueConstraint("primary_username", name=op.f("users_primary_username_key")),
)
op.create_table(
"secondary_usernames",
Expand All @@ -56,9 +55,10 @@ def upgrade():
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
name=op.f("secondary_usernames_user_id_fkey"),
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("username"),
sa.PrimaryKeyConstraint("id", name=op.f("secondary_usernames_pkey")),
sa.UniqueConstraint("username", name=op.f("secondary_usernames_username_key")),
)
op.create_table(
"message",
Expand All @@ -69,20 +69,19 @@ def upgrade():
sa.ForeignKeyConstraint(
["secondary_user_id"],
["secondary_usernames.id"],
name=op.f("message_secondary_user_id_fkey"),
),
sa.ForeignKeyConstraint(
["user_id"],
["users.id"],
name=op.f("message_user_id_fkey"),
),
sa.PrimaryKeyConstraint("id"),
sa.PrimaryKeyConstraint("id", name=op.f("message_pkey")),
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
def downgrade() -> None:
op.drop_table("message")
op.drop_table("secondary_usernames")
op.drop_table("users")
op.drop_table("invite_code")
# ### end Alembic commands ###
Loading

0 comments on commit 16c4b5d

Please sign in to comment.