Skip to content

Commit

Permalink
CORE-2016: added unique indexes on plan rates and plan quota defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
slr71 committed Oct 29, 2024
1 parent 2ea589f commit 524185a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions migrations/000014_subscription_rate_changes.down.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions migrations/000014_subscription_rate_changes.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions migrations/000015_addon_rate_changes.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 524185a

Please sign in to comment.