Skip to content
Open
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
7 changes: 7 additions & 0 deletions docker-app/qfieldcloud/subscription/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib.postgres.fields import DateTimeRangeField
from django.db.models import Func


class TsTzRange(Func):
function = "TSTZRANGE"
output_field = DateTimeRangeField()
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import uuid

import django.contrib.postgres.constraints
import django.contrib.postgres.fields.ranges
import django.db.migrations.state
import django.db.models.deletion
import migrate_sql.operations
from django.conf import settings
from django.contrib.postgres.operations import BtreeGistExtension
from django.db import migrations, models
from django.db.models import Q
from django.utils import timezone

import qfieldcloud.subscription.functions

now = timezone.now()


Expand Down Expand Up @@ -193,10 +196,23 @@ class Migration(migrations.Migration):
],
),
migrations.RunPython(populate_subscriptions_model, populate_account_plan_field),
migrate_sql.operations.CreateSQL(
name="subscription_subscription_prevent_overlaps_idx",
sql="\n ALTER TABLE subscription_subscription\n ADD CONSTRAINT subscription_subscription_prevent_overlaps\n EXCLUDE USING gist (\n account_id WITH =,\n tstzrange(active_since, active_until) WITH &&\n )\n WHERE (active_since IS NOT NULL)\n ",
reverse_sql="\n ALTER TABLE subscription_subscription DROP CONSTRAINT subscription_subscription_prevent_overlaps\n ",
migrations.AddConstraint(
model_name="subscription",
constraint=django.contrib.postgres.constraints.ExclusionConstraint(
condition=models.Q(("active_since__isnull", False)),
expressions=[
("account_id", "="),
(
qfieldcloud.subscription.functions.TsTzRange(
"active_since",
"active_until",
django.contrib.postgres.fields.ranges.RangeBoundary(),
),
"&&",
),
],
name="subscription_subscription_prevent_overlaps",
),
),
#################
# Packages
Expand Down Expand Up @@ -277,10 +293,23 @@ class Migration(migrations.Migration):
null=False,
),
),
migrate_sql.operations.CreateSQL(
name="subscription_package_prevent_overlaps_idx",
sql="\n ALTER TABLE subscription_package\n ADD CONSTRAINT subscription_package_prevent_overlaps\n EXCLUDE USING gist (\n subscription_id WITH =,\n tstzrange(active_since, active_until) WITH &&\n )\n WHERE (active_since IS NOT NULL)\n ",
reverse_sql="\n ALTER TABLE subscription_package DROP CONSTRAINT subscription_package_prevent_overlaps\n ",
migrations.AddConstraint(
model_name="package",
constraint=django.contrib.postgres.constraints.ExclusionConstraint(
condition=models.Q(("active_since__isnull", False)),
expressions=[
("subscription_id", "="),
(
qfieldcloud.subscription.functions.TsTzRange(
"active_since",
"active_until",
django.contrib.postgres.fields.ranges.RangeBoundary(),
),
"&&",
),
],
name="subscription_package_prevent_overlaps",
),
),
####################
# Add auditing fields to plans and packages
Expand Down
35 changes: 34 additions & 1 deletion docker-app/qfieldcloud/subscription/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from deprecated import deprecated
from django.apps import apps
from django.conf import settings
from django.contrib.postgres.constraints import ExclusionConstraint
from django.contrib.postgres.fields import RangeBoundary, RangeOperators
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models, transaction
Expand All @@ -20,6 +22,7 @@
from qfieldcloud.core.models import Organization, Person, User, UserAccount

from .exceptions import NotPremiumPlanException
from .functions import TsTzRange

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -374,6 +377,22 @@ class Package(models.Model):
),
)

class Meta:
constraints = [
ExclusionConstraint(
name="subscription_package_prevent_overlaps",
index_type="GIST",
expressions=[
("subscription_id", RangeOperators.EQUAL),
(
TsTzRange("active_since", "active_until", RangeBoundary()),
RangeOperators.OVERLAPS,
),
],
condition=Q(active_since__isnull=False),
),
]


# TODO add check constraint makes sure there are no two active additional packages at the same time,
# because we assume that once you change your quantity, the old Package instance has an end_date
Expand Down Expand Up @@ -953,7 +972,21 @@ def __str__(self):


class Subscription(AbstractSubscription):
pass
class Meta:
constraints = [
ExclusionConstraint(
name="subscription_subscription_prevent_overlaps",
index_type="GIST",
expressions=[
("account_id", RangeOperators.EQUAL),
(
TsTzRange("active_since", "active_until", RangeBoundary()),
RangeOperators.OVERLAPS,
),
],
condition=Q(active_since__isnull=False),
),
]


class CurrentSubscription(AbstractSubscription):
Expand Down
30 changes: 0 additions & 30 deletions docker-app/qfieldcloud/subscription/sql_config.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,6 @@
from migrate_sql.config import SQLItem

sql_items = [
SQLItem(
"subscription_subscription_prevent_overlaps_idx",
r"""
ALTER TABLE subscription_subscription
ADD CONSTRAINT subscription_subscription_prevent_overlaps
EXCLUDE USING gist (
account_id WITH =,
tstzrange(active_since, active_until) WITH &&
)
WHERE (active_since IS NOT NULL)
""",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

DDL statement generated by the ORM:

ALTER TABLE "subscription_subscription"
ADD CONSTRAINT "subscription_subscription_prevent_overlaps"
EXCLUDE USING GIST (
     "account_id" WITH =,
     (TSTZRANGE("active_since", "active_until", '[)')) WITH &&
)
WHERE ("active_since" IS NOT NULL)

r"""
ALTER TABLE subscription_subscription DROP CONSTRAINT subscription_subscription_prevent_overlaps
""",
),
SQLItem(
"subscription_package_prevent_overlaps_idx",
r"""
ALTER TABLE subscription_package
ADD CONSTRAINT subscription_package_prevent_overlaps
EXCLUDE USING gist (
subscription_id WITH =,
tstzrange(active_since, active_until) WITH &&
)
WHERE (active_since IS NOT NULL)
""",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

DDL statement generated by the ORM:

ALTER TABLE "subscription_package"
ADD CONSTRAINT "subscription_package_prevent_overlaps"
EXCLUDE USING GIST (
    "subscription_id" WITH =,
    (TSTZRANGE("active_since", "active_until", '[)')) WITH &&
)
WHERE ("active_since" IS NOT NULL)

r"""
ALTER TABLE subscription_package DROP CONSTRAINT subscription_package_prevent_overlaps
""",
),
SQLItem(
"current_subscriptions_vw",
r"""
Expand Down
Loading