Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Schema Changes
- Dropped ``TagValue.project_id`` foreign key constraint
- Dropped ``GroupTagKey.project_id`` foreign key constraint
- Dropped ``GroupTagKey.group_id`` foreign key constraint
- Added ``Email`` model

Version 8.19
------------
Expand Down
22 changes: 20 additions & 2 deletions src/sentry/db/models/fields/citext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import six

from django.conf import settings
from django.db import models
from django.db import connections, models
from django.db.models.signals import pre_syncdb


__all__ = ('CITextField', 'CICharField', 'CIEmailField')


class CIText(object):
def get_db_type(self, connection):
def db_type(self, connection):
engine = connection.settings_dict['ENGINE']
if 'postgres' in engine:
return 'citext'
Expand Down Expand Up @@ -47,3 +49,19 @@ class CIEmailField(CIText, models.EmailField):
add_introspection_rules([], ["^sentry\.db\.models\.fields\.citext\.CITextField"])
add_introspection_rules([], ["^sentry\.db\.models\.fields\.citext\.CICharField"])
add_introspection_rules([], ["^sentry\.db\.models\.fields\.citext\.CIEmailField"])


def create_citext_extension(db, **kwargs):
from sentry.utils.db import is_postgres

# We always need the citext extension installed for Postgres,
# and for tests, it's not always guaranteed that we will have
# run full migrations which installed it.
if is_postgres(db):
cursor = connections[db].cursor()
try:
cursor.execute('CREATE EXTENSION IF NOT EXISTS citext')
except Exception:
pass

pre_syncdb.connect(create_citext_extension)
25 changes: 25 additions & 0 deletions src/sentry/models/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import absolute_import

from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from sentry.db.models import Model, sane_repr
from sentry.db.models import CIEmailField


class Email(Model):
"""
Email represents a unique email. Email settings (unsubscribe state) should be associated here.
UserEmail represents whether a given user account has access to that email.
"""
__core__ = True

email = CIEmailField(_('email address'), unique=True)
Copy link
Member

Choose a reason for hiding this comment

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

can we add a date_added field here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

date_added = models.DateTimeField(default=timezone.now)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we don't need an index on this now, we'll probably need to add one later if we try to do anything with this table regarding cleanup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm going to punt on this because I'm unsure when we'd need to do such a cleanup.


class Meta:
app_label = 'sentry'
db_table = 'sentry_email'

__repr__ = sane_repr('email')
26 changes: 26 additions & 0 deletions src/sentry/receivers/email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import absolute_import

from django.db import IntegrityError, transaction
from django.db.models.signals import post_delete, post_save

from sentry.models import Email, UserEmail


def create_email(instance, created, **kwargs):
if created:
try:
with transaction.atomic():
Email.objects.create(email=instance.email)
except IntegrityError:
pass


def delete_email(instance, **kwargs):
if UserEmail.objects.filter(email=instance.email).exists():
Copy link
Contributor

Choose a reason for hiding this comment

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

I want to note here, that this might be slightly inaccurate due to this not being a case insensitive lookup. You should do email__iexact. Because Email is case insensitive but UserEmail is, you'll get false positives.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point: #6070

return

Email.objects.filter(email=instance.email).delete()


post_save.connect(create_email, sender=UserEmail, dispatch_uid="create_email", weak=False)
post_delete.connect(delete_email, sender=UserEmail, dispatch_uid="delete_email", weak=False)
Loading