From 0b652cf3f812964dc13174e05faab453f6c353f8 Mon Sep 17 00:00:00 2001 From: Son NK <> Date: Fri, 15 May 2020 16:35:57 +0200 Subject: [PATCH] remove AliasMailbox.user_id column --- app/api/views/alias.py | 4 +-- app/dashboard/views/custom_alias.py | 4 +-- app/models.py | 2 -- .../versions/2020_051516_552d735a2f1f_.py | 31 +++++++++++++++++++ server.py | 6 ++-- 5 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 migrations/versions/2020_051516_552d735a2f1f_.py diff --git a/app/api/views/alias.py b/app/api/views/alias.py index 7c73646f..db7cb9ac 100644 --- a/app/api/views/alias.py +++ b/app/api/views/alias.py @@ -309,9 +309,7 @@ def update_alias(alias_id): if i == 0: alias.mailbox_id = mailboxes[0].id else: - AliasMailbox.create( - user_id=alias.user_id, alias_id=alias.id, mailbox_id=mailbox.id - ) + AliasMailbox.create(alias_id=alias.id, mailbox_id=mailbox.id) # <<< END update alias mailboxes >>> changed = True diff --git a/app/dashboard/views/custom_alias.py b/app/dashboard/views/custom_alias.py index 2828a23f..5ac2278d 100644 --- a/app/dashboard/views/custom_alias.py +++ b/app/dashboard/views/custom_alias.py @@ -111,9 +111,7 @@ def custom_alias(): for i in range(1, len(mailboxes)): AliasMailbox.create( - user_id=alias.user_id, - alias_id=alias.id, - mailbox_id=mailboxes[i].id, + alias_id=alias.id, mailbox_id=mailboxes[i].id, ) # get the custom_domain_id if alias is created with a custom domain diff --git a/app/models.py b/app/models.py index aa8f3434..9ad42692 100644 --- a/app/models.py +++ b/app/models.py @@ -1304,8 +1304,6 @@ class AliasMailbox(db.Model, ModelMixin): db.UniqueConstraint("alias_id", "mailbox_id", name="uq_alias_mailbox"), ) - user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False) - alias_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False) mailbox_id = db.Column( db.ForeignKey(Mailbox.id, ondelete="cascade"), nullable=False diff --git a/migrations/versions/2020_051516_552d735a2f1f_.py b/migrations/versions/2020_051516_552d735a2f1f_.py new file mode 100644 index 00000000..a8f54ed7 --- /dev/null +++ b/migrations/versions/2020_051516_552d735a2f1f_.py @@ -0,0 +1,31 @@ +"""empty message + +Revision ID: 552d735a2f1f +Revises: 1759f73274ee +Create Date: 2020-05-15 16:33:23.558895 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '552d735a2f1f' +down_revision = '1759f73274ee' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint('alias_mailbox_user_id_fkey', 'alias_mailbox', type_='foreignkey') + op.drop_column('alias_mailbox', 'user_id') + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('alias_mailbox', sa.Column('user_id', sa.INTEGER(), autoincrement=False, nullable=False)) + op.create_foreign_key('alias_mailbox_user_id_fkey', 'alias_mailbox', 'users', ['user_id'], ['id'], ondelete='CASCADE') + # ### end Alembic commands ### diff --git a/server.py b/server.py index 18d6e916..2d7b4da6 100644 --- a/server.py +++ b/server.py @@ -175,11 +175,9 @@ def fake_data(): if i % 5 == 0: if i % 2 == 0: - AliasMailbox.create( - user_id=user.id, alias_id=a.id, mailbox_id=user.default_mailbox_id - ) + AliasMailbox.create(alias_id=a.id, mailbox_id=user.default_mailbox_id) else: - AliasMailbox.create(user_id=user.id, alias_id=a.id, mailbox_id=m1.id) + AliasMailbox.create(alias_id=a.id, mailbox_id=m1.id) db.session.commit() # some aliases don't have any activity