Skip to content

Commit

Permalink
Merge pull request #530 from scidsg/update-migrations
Browse files Browse the repository at this point in the history
Update models to match alembic migrations
  • Loading branch information
micahflee authored Aug 30, 2024
2 parents 83705c2 + 68b1bcd commit bfe0645
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
build
coverage
hushline/static/vendor/*
.pytest_cache
20 changes: 16 additions & 4 deletions hushline/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask import current_app
from flask_sqlalchemy.model import Model
from passlib.hash import scrypt
from sqlalchemy import Index

from .crypto import decrypt_field, encrypt_field
from .db import db
Expand Down Expand Up @@ -177,6 +178,15 @@ class AuthenticationLog(Model):
otp_code: Mapped[Optional[str]] = mapped_column(db.String(6))
timecode: Mapped[Optional[int]]

__table_args__ = (
Index(
"idx_authentication_logs_user_id_timestamp_successful",
"user_id",
"timestamp",
"successful",
),
)

# Open question: should we store the IP address and user agent?
# It's useful for auditing, but it's identifable
# ip_address = db.Column(db.String(45), nullable=False)
Expand Down Expand Up @@ -208,9 +218,7 @@ class SecondaryUsername(Model):

class Message(Model):
id: Mapped[int] = mapped_column(primary_key=True)
_content: Mapped[Optional[str]] = mapped_column(
"content", db.Text
) # Encrypted content stored here
_content: Mapped[str] = mapped_column("content", db.Text) # Encrypted content stored here
user_id: Mapped[int] = mapped_column(db.ForeignKey("users.id"))
user: Mapped["User"] = relationship(backref=db.backref("messages", lazy=True))
secondary_user_id: Mapped[Optional[int]] = mapped_column(
Expand All @@ -231,7 +239,11 @@ def content(self) -> str | None:

@content.setter
def content(self, value: str) -> None:
self._content = encrypt_field(value)
val = encrypt_field(value)
if val is not None:
self._content = val
else:
self._content = ""


class InviteCode(Model):
Expand Down
39 changes: 39 additions & 0 deletions migrations/versions/62551ed63cbf_make_boolean_fields_required.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""make boolean fields required
Revision ID: 62551ed63cbf
Revises: 166a3402c391
Create Date: 2024-08-30 09:51:53.990304
"""

from alembic import op
import sqlalchemy as sa


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


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
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! ###
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 ###

0 comments on commit bfe0645

Please sign in to comment.