From b5c369f084cb92e9dd1f459dbe5f2c590d40c46a Mon Sep 17 00:00:00 2001 From: Son NK Date: Fri, 20 Mar 2020 10:13:25 +0100 Subject: [PATCH] make user_id non nullable on contact, email_log and file --- app/models.py | 8 +++-- .../versions/2020_032010_dbd80d290f04_.py | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/2020_032010_dbd80d290f04_.py diff --git a/app/models.py b/app/models.py index 88a56a37..6725fb66 100644 --- a/app/models.py +++ b/app/models.py @@ -80,7 +80,7 @@ class ModelMixin(object): class File(db.Model, ModelMixin): path = db.Column(db.String(128), unique=True, nullable=False) - user_id = db.Column(db.ForeignKey("users.id", ondelete="cascade"), nullable=True) + user_id = db.Column(db.ForeignKey("users.id", ondelete="cascade"), nullable=False) def get_url(self, expires_in=3600): return s3.get_url(self.path, expires_in) @@ -701,7 +701,7 @@ class Contact(db.Model, ModelMixin): db.UniqueConstraint("alias_id", "website_email", name="uq_contact"), ) - user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=True) + user_id = db.Column(db.ForeignKey(User.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 @@ -747,7 +747,7 @@ class Contact(db.Model, ModelMixin): class EmailLog(db.Model, ModelMixin): - user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=True) + user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=False) contact_id = db.Column( db.ForeignKey(Contact.id, ondelete="cascade"), nullable=False ) @@ -770,6 +770,8 @@ class EmailLog(db.Model, ModelMixin): refused_email = db.relationship("RefusedEmail") forward = db.relationship(Contact) + contact = db.relationship(Contact) + class Subscription(db.Model, ModelMixin): # Come from Paddle diff --git a/migrations/versions/2020_032010_dbd80d290f04_.py b/migrations/versions/2020_032010_dbd80d290f04_.py new file mode 100644 index 00000000..2d47593c --- /dev/null +++ b/migrations/versions/2020_032010_dbd80d290f04_.py @@ -0,0 +1,33 @@ +"""empty message + +Revision ID: dbd80d290f04 +Revises: f4b8232fa17e +Create Date: 2020-03-20 10:11:59.542933 + +""" +import sqlalchemy_utils +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "dbd80d290f04" +down_revision = "f4b8232fa17e" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column("contact", "user_id", existing_type=sa.INTEGER(), nullable=False) + op.alter_column("email_log", "user_id", existing_type=sa.INTEGER(), nullable=False) + op.alter_column("file", "user_id", existing_type=sa.INTEGER(), nullable=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.alter_column("file", "user_id", existing_type=sa.INTEGER(), nullable=True) + op.alter_column("email_log", "user_id", existing_type=sa.INTEGER(), nullable=True) + op.alter_column("contact", "user_id", existing_type=sa.INTEGER(), nullable=True) + # ### end Alembic commands ###