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

fix: Add proper unique constraints and remove soft deletion from models #7098

Merged
merged 14 commits into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions app/api/custom/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,7 @@ def complete_order(order_id):
)
form_fields = (
db.session.query(CustomForms)
.filter_by(
event_id=order.event_id, form='attendee', is_included=True, deleted_at=None
)
.filter_by(event_id=order.event_id, form='attendee', is_included=True)
.all()
)
for attendee, updated_attendee in zip(attendees, updated_attendees):
Expand Down
2 changes: 1 addition & 1 deletion app/api/event_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def create_event_copy(identifier):
event_id=event.id, deleted_at=None
).all()
tracks = Track.query.filter_by(event_id=event.id, deleted_at=None).all()
custom_forms = CustomForms.query.filter_by(event_id=event.id, deleted_at=None).all()
custom_forms = CustomForms.query.filter_by(event_id=event.id).all()
discount_codes = DiscountCode.query.filter_by(
event_id=event.id, deleted_at=None
).all()
Expand Down
2 changes: 1 addition & 1 deletion app/api/helpers/custom_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_schema(form_fields):

def validate_custom_form_constraints(form, obj):
form_fields = CustomForms.query.filter_by(
form=form, event_id=obj.event_id, is_included=True, deleted_at=None,
form=form, event_id=obj.event_id, is_included=True,
).all()
required_form_fields = filter(lambda field: field.is_required, form_fields)
missing_required_fields = []
Expand Down
5 changes: 2 additions & 3 deletions app/api/schema/custom_forms.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from marshmallow import validate as validate
from marshmallow_jsonapi import fields
from marshmallow_jsonapi.flask import Relationship
from marshmallow_jsonapi.flask import Relationship, Schema

from app.api.helpers.utilities import dasherize
from app.api.schema.base import SoftDeletionSchema
from utils.common import use_defaults


@use_defaults()
class CustomFormSchema(SoftDeletionSchema):
class CustomFormSchema(Schema):
"""
API Schema for Custom Forms database model
"""
Expand Down
5 changes: 2 additions & 3 deletions app/api/schema/event_sub_topics.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from marshmallow_jsonapi import fields
from marshmallow_jsonapi.flask import Relationship
from marshmallow_jsonapi.flask import Relationship, Schema

from app.api.helpers.utilities import dasherize
from app.api.schema.base import SoftDeletionSchema


class EventSubTopicSchema(SoftDeletionSchema):
class EventSubTopicSchema(Schema):
"""
Api Schema for event sub topic model
"""
Expand Down
3 changes: 1 addition & 2 deletions app/models/custom_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from app.api.helpers.utilities import to_snake_case
from app.models import db
from app.models.base import SoftDeletionModel

SESSION_FORM = {
"title": {"include": 1, "require": 1},
Expand Down Expand Up @@ -135,7 +134,7 @@
}


class CustomForms(SoftDeletionModel):
class CustomForms(db.Model):
"""custom form model class"""

__tablename__ = 'custom_forms'
Expand Down
2 changes: 1 addition & 1 deletion app/models/discount_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class DiscountCode(SoftDeletionModel):
__tablename__ = "discount_codes"
__table_args__ = (
UniqueConstraint('event_id', 'code', name='uq_event_discount_code'),
UniqueConstraint('event_id', 'code', 'deleted_at', name='uq_event_discount_code'),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

)

id = db.Column(db.Integer, primary_key=True)
Expand Down
3 changes: 1 addition & 2 deletions app/models/event_sub_topic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

from app.api.helpers.db import get_new_slug
from app.models import db
from app.models.base import SoftDeletionModel


class EventSubTopic(SoftDeletionModel):
class EventSubTopic(db.Model):
"""Event sub topic object table"""

__tablename__ = 'event_sub_topics'
Expand Down
8 changes: 6 additions & 2 deletions app/models/permission.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from app.models import db

from app.models.base import SoftDeletionModel

class Permission(db.Model):

class Permission(SoftDeletionModel):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well. Don't add more soft deletion. It has already complicated matters a lot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

"""Role-Service Permissions
"""

__tablename__ = 'permissions'
__table_args__ = (
db.UniqueConstraint('role_id', 'service_id', name='role_service_uc'),
db.UniqueConstraint(
'role_id', 'service_id', 'deleted_at', name='role_service_uc'
),
)

id = db.Column(db.Integer, primary_key=True)
Expand Down
4 changes: 3 additions & 1 deletion app/models/role_invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def generate_hash():
class RoleInvite(SoftDeletionModel):
__tablename__ = 'role_invites'
__table_args__ = (
UniqueConstraint('email', 'role_id', 'event_id', name='email_role_event_uc'),
UniqueConstraint(
'email', 'role_id', 'event_id', 'deleted_at', name='email_role_event_uc',
),
)

id = db.Column(db.Integer, primary_key=True)
Expand Down
4 changes: 0 additions & 4 deletions docs/api/blueprint/custom_forms.apib
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ Create a new Custom Form with event_id.
"field-identifier": "name",
"is-included": false,
"is-fixed": false,
"deleted-at": null,
"type": "text"
},
"type": "custom-form",
Expand Down Expand Up @@ -123,7 +122,6 @@ Get a single custom form.
"field-identifier": "name",
"is-fixed": false,
"is-included": false,
"deleted-at": null,
"type": "text"
},
"type": "custom-form",
Expand Down Expand Up @@ -187,7 +185,6 @@ Update a single custom form with `id`.
"form": "speaker",
"field-identifier": "name",
"is-fixed": false,
"deleted-at": null,
"is-included": false,
"type": "text"
},
Expand Down Expand Up @@ -271,7 +268,6 @@ Get a list of Custom Forms for an event.
"field-identifier": "name",
"is-included": false,
"is-fixed": false,
"deleted-at": null,
"type": "text"
},
"type": "custom-form",
Expand Down
33 changes: 33 additions & 0 deletions migrations/versions/rev-2020-06-30-20:07:55-f177c14710f1_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""empty message

Revision ID: f177c14710f1
Revises: 64439b77fa6d
Create Date: 2020-06-30 20:07:55.320285

"""

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'f177c14710f1'
down_revision = '64439b77fa6d'


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.execute('DELETE FROM custom_forms WHERE deleted_at IS NOT NULL')
op.execute('DELETE FROM event_sub_topics WHERE deleted_at IS NOT NULL')
Copy link
Member

@iamareebjamal iamareebjamal Jul 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add equivalent statements in downgrade as well

Sorry, you can't

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will be the equivalent?


op.drop_column('custom_forms', 'deleted_at')
op.drop_column('event_sub_topics', 'deleted_at')
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('event_sub_topics', sa.Column('deleted_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True))
op.add_column('custom_forms', sa.Column('deleted_at', postgresql.TIMESTAMP(timezone=True), autoincrement=False, nullable=True))
# ### end Alembic commands ###
40 changes: 40 additions & 0 deletions migrations/versions/rev-2020-07-03-01:29:29-4e9450c16147_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""empty message

Revision ID: 4e9450c16147
Revises: f177c14710f1
Create Date: 2020-07-03 01:29:29.959740

"""

from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils


# revision identifiers, used by Alembic.
revision = '4e9450c16147'
down_revision = 'f177c14710f1'


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('uq_event_discount_code', 'discount_codes', type_='unique')
op.create_unique_constraint('uq_event_discount_code', 'discount_codes', ['event_id', 'code', 'deleted_at'])
op.add_column('permissions', sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True))
op.drop_constraint('role_service_uc', 'permissions', type_='unique')
op.create_unique_constraint('role_service_uc', 'permissions', ['role_id', 'service_id', 'deleted_at'])
op.drop_constraint('email_role_event_uc', 'role_invites', type_='unique')
op.create_unique_constraint('email_role_event_uc', 'role_invites', ['email', 'role_id', 'event_id', 'deleted_at'])
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('email_role_event_uc', 'role_invites', type_='unique')
op.create_unique_constraint('email_role_event_uc', 'role_invites', ['email', 'role_id', 'event_id'])
op.drop_constraint('role_service_uc', 'permissions', type_='unique')
op.create_unique_constraint('role_service_uc', 'permissions', ['role_id', 'service_id'])
op.drop_column('permissions', 'deleted_at')
op.drop_constraint('uq_event_discount_code', 'discount_codes', type_='unique')
op.create_unique_constraint('uq_event_discount_code', 'discount_codes', ['event_id', 'code'])
# ### end Alembic commands ###