Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update models to match alembic migrations #530

Merged
merged 7 commits into from
Aug 30, 2024
Merged

Conversation

micahflee
Copy link
Collaborator

This PR tweaks the models so that when you autogenerate a migration with alembic, it does not need to create a new one.

For example, from the main branch:

$ rm instance/hushline.db
$ make migrate
$ make revision message="test"
. ./dev_env.sh && \
poetry run flask db upgrade
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> 691dba936e64, Initial migration
INFO  [alembic.runtime.migration] Running upgrade 691dba936e64 -> ff59dfd3bdf6, add AuthenticationLog
INFO  [alembic.runtime.migration] Running upgrade ff59dfd3bdf6 -> 568f77aefcb4, add timecode to AuthenticationLog
INFO  [alembic.runtime.migration] Running upgrade 568f77aefcb4 -> 6e53eac9ea14, smtp encryption protocol
INFO  [alembic.runtime.migration] Running upgrade 6e53eac9ea14 -> 166a3402c391, add extra fields to user
. ./dev_env.sh && \
poetry run flask db revision -m "test" --autogenerate
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected removed index 'idx_authentication_logs_user_id_timestamp_successful' on 'authentication_logs'
INFO  [alembic.autogenerate.compare] Detected NULL on column 'message.content'
INFO  [alembic.autogenerate.compare] Detected NOT NULL on column 'users.password_hash'
INFO  [alembic.autogenerate.compare] Detected NOT NULL on column 'users.is_verified'
INFO  [alembic.autogenerate.compare] Detected NOT NULL on column 'users.is_admin'
INFO  [alembic.autogenerate.compare] Detected NOT NULL on column 'users.show_in_directory'
  Generating /home/user/code/scidsg/hushline/migrations/versions/a961a97b5f17_test.py ...  done

Here's the migration it created:

"""test

Revision ID: a961a97b5f17
Revises: 166a3402c391
Create Date: 2024-08-29 08:42:38.998331

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'a961a97b5f17'
down_revision = '166a3402c391'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('authentication_logs', schema=None) as batch_op:
        batch_op.drop_index('idx_authentication_logs_user_id_timestamp_successful')

    with op.batch_alter_table('message', schema=None) as batch_op:
        batch_op.alter_column('content',
               existing_type=sa.TEXT(),
               nullable=True)

    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)

    with op.batch_alter_table('message', schema=None) as batch_op:
        batch_op.alter_column('content',
               existing_type=sa.TEXT(),
               nullable=False)

    with op.batch_alter_table('authentication_logs', schema=None) as batch_op:
        batch_op.create_index('idx_authentication_logs_user_id_timestamp_successful', ['user_id', 'timestamp', 'successful'], unique=False)

    # ### end Alembic commands ###

However from this branch:

$ rm instance/hushline.db
$ make migrate
$ make revision message="test"
. ./dev_env.sh && \
poetry run flask db revision -m "test" --autogenerate
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.env] No changes in schema detected.

It no longer needs to autogenerate a migration.

hushline/model.py Outdated Show resolved Hide resolved
hushline/model.py Outdated Show resolved Hide resolved
hushline/model.py Outdated Show resolved Hide resolved
hushline/model.py Outdated Show resolved Hide resolved
hushline/model.py Outdated Show resolved Hide resolved
@jeremywmoore
Copy link
Collaborator

After experimenting a bit with my suggested changes, we might actually want to apply the autogenerated changes that this PR is attempting to squash. Boolean columns should probably have non-null value. We may also want to update the encrypt/decrypt methods to return '' instead of None.

@micahflee micahflee mentioned this pull request Aug 30, 2024
@micahflee
Copy link
Collaborator Author

Okay this is ready for another review! It includes a migration that just makes those boolean fields required.

I briefly tried making the encrypt_field and decrypt_field functions always return a string, but that caused all sorts of problems. So instead I just made it so the Message.content setter stores a blank string if encrypt_fields returns None.

@micahflee micahflee merged commit bfe0645 into main Aug 30, 2024
6 checks passed
@micahflee micahflee deleted the update-migrations branch August 30, 2024 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants