diff --git a/Makefile b/Makefile index 7e2537e0..0a300eb8 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/hushline/db.py b/hushline/db.py index 248751a0..bc079ed2 100644 --- a/hushline/db.py +++ b/hushline/db.py @@ -11,4 +11,4 @@ } ) -db = SQLAlchemy() +db = SQLAlchemy(metadata=metadata) diff --git a/hushline/model.py b/hushline/model.py index 51f9a6ca..a0d2a6eb 100644 --- a/hushline/model.py +++ b/hushline/model.py @@ -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")) @@ -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] diff --git a/migrations/versions/166a3402c391_add_extra_fields_to_user.py b/migrations/versions/166a3402c391_add_extra_fields_to_user.py index 7daaa33f..de187211 100644 --- a/migrations/versions/166a3402c391_add_extra_fields_to_user.py +++ b/migrations/versions/166a3402c391_add_extra_fields_to_user.py @@ -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)) @@ -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") @@ -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 ### diff --git a/migrations/versions/46aedec8fd9b_create_alias_tables.py b/migrations/versions/46aedec8fd9b_create_alias_tables.py index c4d76cb4..1db03f1e 100644 --- a/migrations/versions/46aedec8fd9b_create_alias_tables.py +++ b/migrations/versions/46aedec8fd9b_create_alias_tables.py @@ -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 @@ -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( @@ -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") @@ -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: @@ -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( @@ -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( @@ -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") diff --git a/migrations/versions/568f77aefcb4_add_timecode_to_authenticationlog.py b/migrations/versions/568f77aefcb4_add_timecode_to_authenticationlog.py index 323df089..f35a096c 100644 --- a/migrations/versions/568f77aefcb4_add_timecode_to_authenticationlog.py +++ b/migrations/versions/568f77aefcb4_add_timecode_to_authenticationlog.py @@ -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 ### 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")) diff --git a/migrations/versions/62551ed63cbf_make_boolean_fields_required.py b/migrations/versions/62551ed63cbf_make_boolean_fields_required.py index 07f52911..065724dc 100644 --- a/migrations/versions/62551ed63cbf_make_boolean_fields_required.py +++ b/migrations/versions/62551ed63cbf_make_boolean_fields_required.py @@ -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 ### diff --git a/migrations/versions/691dba936e64_initial_migration.py b/migrations/versions/691dba936e64_initial_migration.py index c717b64f..5fd84905 100644 --- a/migrations/versions/691dba936e64_initial_migration.py +++ b/migrations/versions/691dba936e64_initial_migration.py @@ -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", @@ -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", @@ -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", @@ -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 ### diff --git a/migrations/versions/6e53eac9ea14_smtp_encryption_protocol.py b/migrations/versions/6e53eac9ea14_smtp_encryption_protocol.py index 1a72b599..7b144867 100644 --- a/migrations/versions/6e53eac9ea14_smtp_encryption_protocol.py +++ b/migrations/versions/6e53eac9ea14_smtp_encryption_protocol.py @@ -19,7 +19,7 @@ depends_on = None -def upgrade(): +def upgrade() -> None: op.add_column( "users", sa.Column( @@ -32,6 +32,6 @@ def upgrade(): op.add_column("users", sa.Column("smtp_sender", sa.String(), nullable=True)) -def downgrade(): +def downgrade() -> None: op.drop_column("users", "smtp_encryption") op.drop_column("users", "smtp_sender") diff --git a/migrations/versions/83a6b3b09eca_add_verified_fields.py b/migrations/versions/83a6b3b09eca_add_verified_fields.py index 95c8d179..3f5463fc 100644 --- a/migrations/versions/83a6b3b09eca_add_verified_fields.py +++ b/migrations/versions/83a6b3b09eca_add_verified_fields.py @@ -17,24 +17,17 @@ 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.add_column(sa.Column("extra_field_verified1", sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column("extra_field_verified2", sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column("extra_field_verified3", sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column("extra_field_verified4", sa.Boolean(), nullable=True)) - # ### 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.drop_column("extra_field_verified4") batch_op.drop_column("extra_field_verified3") batch_op.drop_column("extra_field_verified2") batch_op.drop_column("extra_field_verified1") - - # ### end Alembic commands ### diff --git a/migrations/versions/c2b6eff6e213_.py b/migrations/versions/c2b6eff6e213_.py deleted file mode 100644 index 350ad4e9..00000000 --- a/migrations/versions/c2b6eff6e213_.py +++ /dev/null @@ -1,21 +0,0 @@ -"""empty message - -Revision ID: c2b6eff6e213 -Revises: 62551ed63cbf, 83a6b3b09eca -Create Date: 2024-09-05 09:02:54.497837 - -""" - -# revision identifiers, used by Alembic. -revision = "c2b6eff6e213" -down_revision = ("62551ed63cbf", "83a6b3b09eca") -branch_labels = None -depends_on = None - - -def upgrade(): - pass - - -def downgrade(): - pass diff --git a/migrations/versions/ff59dfd3bdf6_add_authenticationlog.py b/migrations/versions/ff59dfd3bdf6_add_authenticationlog.py index c7a5d918..719ae8f3 100644 --- a/migrations/versions/ff59dfd3bdf6_add_authenticationlog.py +++ b/migrations/versions/ff59dfd3bdf6_add_authenticationlog.py @@ -9,7 +9,6 @@ from alembic import op import sqlalchemy as sa - # revision identifiers, used by Alembic. revision = "ff59dfd3bdf6" down_revision = "691dba936e64" @@ -17,8 +16,7 @@ depends_on = None -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### +def upgrade() -> None: op.create_table( "authentication_logs", sa.Column("id", sa.Integer(), nullable=False), @@ -29,26 +27,15 @@ def upgrade(): sa.ForeignKeyConstraint( ["user_id"], ["users.id"], + name=op.f("authentication_logs_user_id_fkey"), ), - sa.PrimaryKeyConstraint("id"), - ) - # ### end Alembic commands ### - - # Create an index on user_id, timestamp, successful - op.create_index( - "idx_authentication_logs_user_id_timestamp_successful", - "authentication_logs", - ["user_id", "timestamp", "successful"], + sa.Index( + op.f("idx_authentication_logs_user_id_timestamp_successful"), + *["user_id", "timestamp", "successful"], + ), + sa.PrimaryKeyConstraint("id", name=op.f("authentication_logs_pkey")), ) -def downgrade(): - # Drop the index - op.drop_index( - "idx_authentication_logs_user_id_timestamp_successful", - table_name="authentication_logs", - ) - - # ### commands auto generated by Alembic - please adjust! ### +def downgrade() -> None: op.drop_table("authentication_logs") - # ### end Alembic commands ###