From 524185a0ac9361e5b36ee1a20aa951b2d4ee4932 Mon Sep 17 00:00:00 2001 From: Sarah Roberts Date: Tue, 29 Oct 2024 13:40:40 -0700 Subject: [PATCH] CORE-2016: added unique indexes on plan rates and plan quota defaults --- migrations/000014_subscription_rate_changes.down.sql | 3 +++ migrations/000014_subscription_rate_changes.up.sql | 9 +++++++++ migrations/000015_addon_rate_changes.up.sql | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/migrations/000014_subscription_rate_changes.down.sql b/migrations/000014_subscription_rate_changes.down.sql index e0bf05d..1885559 100644 --- a/migrations/000014_subscription_rate_changes.down.sql +++ b/migrations/000014_subscription_rate_changes.down.sql @@ -25,6 +25,9 @@ WHERE keepers.plan_id = pqd.plan_id AND keepers.resource_type_id = pqd.resource_type_id AND keepers.most_recent_date != pqd.effective_date; +-- Remove the plan_quota_defaults unique key on plan_id, resource_type_id, and effective_date. +DROP INDEX IF EXISTS plan_quota_defaults_resource_type_plan_effective_date_index; + -- Add the plan_quota_defaults unique key on plan_id and resource_type_id. CREATE UNIQUE INDEX IF NOT EXISTS plan_quota_defaults_resource_type_plan_index ON plan_quota_defaults (resource_type_id, plan_id); diff --git a/migrations/000014_subscription_rate_changes.up.sql b/migrations/000014_subscription_rate_changes.up.sql index 7dd513a..2370d07 100644 --- a/migrations/000014_subscription_rate_changes.up.sql +++ b/migrations/000014_subscription_rate_changes.up.sql @@ -16,6 +16,10 @@ CREATE TABLE IF NOT EXISTS plan_rates ( PRIMARY KEY (id) ); +-- There can only be one rate for each subscription plan that can become effective at a specific time. +CREATE UNIQUE INDEX IF NOT EXISTS plan_rates_plan_effective_date_index + ON plan_rates (plan_id, effective_date); + -- Populate the plan_rates table with initial values. This could cause a problem if this isn't the exact set of plans, -- but the changes of having a different set of plans in any DE deployment at this point in time is small because the -- subscription admin pages don't currently have a feature to allow administrators to add or remove subscription plans. @@ -34,6 +38,11 @@ ALTER TABLE IF EXISTS plan_quota_defaults ADD COLUMN effective_date timestamp WI UPDATE plan_quota_defaults SET effective_date = '2022-01-01'; ALTER TABLE IF EXISTS plan_quota_defaults ALTER COLUMN effective_date SET NOT NULL; +-- Ensure that no two plan quota default values for the same plan have both the same resource type and effective date. +CREATE UNIQUE INDEX IF NOT EXISTS plan_quota_defaults_resource_type_plan_effective_date_index + ON plan_quota_defaults (resource_type_id, effective_date, plan_id); + + -- Add the plan rate ID column to the subscriptions table. ALTER TABLE IF EXISTS subscriptions ADD COLUMN IF NOT EXISTS plan_rate_id uuid REFERENCES plan_rates (id) ON DELETE CASCADE; diff --git a/migrations/000015_addon_rate_changes.up.sql b/migrations/000015_addon_rate_changes.up.sql index b404f10..5666dfc 100644 --- a/migrations/000015_addon_rate_changes.up.sql +++ b/migrations/000015_addon_rate_changes.up.sql @@ -23,6 +23,10 @@ INSERT INTO addon_rates (id, addon_id, effective_date, rate) VALUES ('d612d958-82ad-11ef-b4b2-5a8d7f4f1112', 'c21dd61f-aa41-40ad-8005-859679ceed9c', '2022-01-01', 125.00) ON CONFLICT (id) DO NOTHING; +-- There can only be one rate for each subscription addon that can become effective at a specific time. +CREATE UNIQUE INDEX IF NOT EXISTS addon_rates_effective_date_addon_index + ON addon_rates(addon_id, effective_date); + -- Add the addon_rate_id column to the subscription_addons table. ALTER TABLE IF EXISTS subscription_addons ADD COLUMN IF NOT EXISTS addon_rate_id uuid REFERENCES addon_rates(id) ON DELETE CASCADE;