Просмотр исходного кода

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

Son NK 5 лет назад
Родитель
Сommit
cd19997424
2 измененных файлов с 60 добавлено и 1 удалено
  1. 4 1
      app/models.py
  2. 56 0
      migrations/versions/2020_032009_f4b8232fa17e_.py

+ 4 - 1
app/models.py

@@ -80,6 +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)
 
     def get_url(self, expires_in=3600):
         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
     )
 
-    profile_picture = db.relationship(File)
+    profile_picture = db.relationship(File, foreign_keys=[profile_picture_id])
 
     @classmethod
     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"),
     )
 
+    user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=True)
     alias_id = db.Column(db.ForeignKey(Alias.id, ondelete="cascade"), nullable=False)
 
     # 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):
+    user_id = db.Column(db.ForeignKey(User.id, ondelete="cascade"), nullable=True)
     contact_id = db.Column(
         db.ForeignKey(Contact.id, ondelete="cascade"), nullable=False
     )

+ 56 - 0
migrations/versions/2020_032009_f4b8232fa17e_.py

@@ -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 ###