Add File.user_id, Contact.user_id, EmailLog.user_id columns

This commit is contained in:
Son NK 2020-03-20 09:51:15 +01:00 committed by Son NK
parent 2a33a21219
commit 43cd9e72d8
2 changed files with 60 additions and 1 deletions

View file

@ -80,6 +80,7 @@ class ModelMixin(object):
class File(db.Model, ModelMixin): class File(db.Model, ModelMixin):
path = db.Column(db.String(128), unique=True, nullable=False) path = db.Column(db.String(128), unique=True, nullable=False)
user_id = db.Column(db.ForeignKey("users.id", ondelete="cascade"), nullable=True)
def get_url(self, expires_in=3600): def get_url(self, expires_in=3600):
return s3.get_url(self.path, expires_in) return s3.get_url(self.path, expires_in)
@ -143,7 +144,7 @@ class User(db.Model, ModelMixin, UserMixin):
db.ForeignKey("mailbox.id"), nullable=True, default=None db.ForeignKey("mailbox.id"), nullable=True, default=None
) )
profile_picture = db.relationship(File) profile_picture = db.relationship(File, foreign_keys=[profile_picture_id])
@classmethod @classmethod
def create(cls, email, name, password=None, **kwargs): def create(cls, email, name, password=None, **kwargs):
@ -700,6 +701,7 @@ class Contact(db.Model, ModelMixin):
db.UniqueConstraint("alias_id", "website_email", name="uq_contact"), db.UniqueConstraint("alias_id", "website_email", name="uq_contact"),
) )
user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=True)
alias_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False) alias_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False)
# used to be envelope header, should be mail header from instead # used to be envelope header, should be mail header from instead
@ -745,6 +747,7 @@ class Contact(db.Model, ModelMixin):
class EmailLog(db.Model, ModelMixin): class EmailLog(db.Model, ModelMixin):
user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=True)
contact_id = db.Column( contact_id = db.Column(
db.ForeignKey(Contact.id, ondelete="cascade"), nullable=False db.ForeignKey(Contact.id, ondelete="cascade"), nullable=False
) )

View file

@ -0,0 +1,56 @@
"""empty message
Revision ID: f4b8232fa17e
Revises: 0809266d08ca
Create Date: 2020-03-20 09:41:21.840221
"""
import sqlalchemy_utils
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "f4b8232fa17e"
down_revision = "0809266d08ca"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("contact", sa.Column("user_id", sa.Integer(), nullable=True))
op.create_foreign_key(
None, "contact", "users", ["user_id"], ["id"], ondelete="cascade"
)
op.add_column("email_log", sa.Column("user_id", sa.Integer(), nullable=True))
op.create_foreign_key(
None, "email_log", "users", ["user_id"], ["id"], ondelete="cascade"
)
op.add_column("file", sa.Column("user_id", sa.Integer(), nullable=True))
op.create_foreign_key(
None, "file", "users", ["user_id"], ["id"], ondelete="cascade"
)
op.drop_column("users", "can_use_pgp")
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"users",
sa.Column(
"can_use_pgp",
sa.BOOLEAN(),
server_default=sa.text("false"),
autoincrement=False,
nullable=False,
),
)
op.drop_constraint(None, "file", type_="foreignkey")
op.drop_column("file", "user_id")
op.drop_constraint(None, "email_log", type_="foreignkey")
op.drop_column("email_log", "user_id")
op.drop_constraint(None, "contact", type_="foreignkey")
op.drop_column("contact", "user_id")
# ### end Alembic commands ###