-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to update existing subscription caches
- Loading branch information
1 parent
56a266d
commit c039942
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
api/organisations/migrations/0058_update_audit_and_history_limits_in_sub_cache.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Generated by Django 4.2.15 on 2024-10-29 11:11 | ||
import logging | ||
|
||
from django.apps.registry import Apps | ||
from django.db import migrations | ||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
|
||
from organisations.subscriptions.constants import SubscriptionPlanFamily | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def update_limits(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None: | ||
subscription_model = apps.get_model("organisations", "Subscription") | ||
organisation_subscription_information_cache_model = apps.get_model("organisations", "OrganisationSubscriptionInformationCache") | ||
|
||
all_paid_subscriptions = subscription_model.objects.select_related( | ||
"organisation", "organisation__subscription_information_cache" | ||
).exclude(plan="free") | ||
|
||
cache_models_to_update = [] | ||
|
||
for subscription in all_paid_subscriptions: | ||
subscription_family = SubscriptionPlanFamily.get_by_plan_id(subscription.plan) | ||
if subscription_family == SubscriptionPlanFamily.START_UP: | ||
continue | ||
|
||
osic = getattr(subscription.organisation, "subscription_information_cache", None) | ||
if osic is None: | ||
continue | ||
|
||
if subscription_family == SubscriptionPlanFamily.SCALE_UP: | ||
osic.audit_log_visibility_days = None | ||
osic.feature_history_visibility_days = 14 | ||
elif subscription_family == SubscriptionPlanFamily.ENTERPRISE: | ||
osic.audit_log_visibility_days = None | ||
osic.feature_history_visibility_days = None | ||
|
||
cache_models_to_update.append(osic) | ||
|
||
organisation_subscription_information_cache_model.objects.bulk_update( | ||
cache_models_to_update, | ||
fields=["audit_log_visibility_days", "feature_history_visibility_days"] | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("organisations", "0057_limit_audit_and_version_history"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_limits, reverse_code=migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters