diff --git a/corporate/migrations/0018_customer_cloud_xor_self_hosted.py b/corporate/migrations/0018_customer_cloud_xor_self_hosted.py new file mode 100644 index 0000000000..788be10ee0 --- /dev/null +++ b/corporate/migrations/0018_customer_cloud_xor_self_hosted.py @@ -0,0 +1,24 @@ +# Generated by Django 4.2.1 on 2023-05-30 03:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ( + "corporate", + "0017_rename_exempt_from_from_license_number_check_customer_exempt_from_license_number_check", + ), + ] + + operations = [ + migrations.AddConstraint( + model_name="customer", + constraint=models.CheckConstraint( + check=models.Q( + ("realm__isnull", False), ("remote_server__isnull", False), _connector="XOR" + ), + name="cloud_xor_self_hosted", + ), + ), + ] diff --git a/corporate/models.py b/corporate/models.py index b40807a465..14bba604b6 100644 --- a/corporate/models.py +++ b/corporate/models.py @@ -3,7 +3,7 @@ from typing import Any, Dict, Optional, Union from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import models -from django.db.models import CASCADE +from django.db.models import CASCADE, Q from zerver.models import Realm, UserProfile from zilencer.models import RemoteZulipServer @@ -28,23 +28,17 @@ class Customer(models.Model): # they purchased. exempt_from_license_number_check = models.BooleanField(default=False) + class Meta: + constraints = [ + models.CheckConstraint( + check=Q(realm__isnull=False) ^ Q(remote_server__isnull=False), + name="cloud_xor_self_hosted", + ) + ] + def __str__(self) -> str: return f"{self.realm!r} {self.stripe_customer_id}" - @property - def is_self_hosted(self) -> bool: - is_self_hosted = self.remote_server is not None - if is_self_hosted: - assert self.realm is None - return is_self_hosted - - @property - def is_cloud(self) -> bool: - is_cloud = self.realm is not None - if is_cloud: - assert self.remote_server is None - return is_cloud - def get_customer_by_realm(realm: Realm) -> Optional[Customer]: return Customer.objects.filter(realm=realm).first()