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 f26ecc4
Show file tree
Hide file tree
Showing 2 changed files with 12 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

0 comments on commit f26ecc4

Please sign in to comment.