diff --git a/changelogs/unreleased/322043-insert-plan-trial.yml b/changelogs/unreleased/322043-insert-plan-trial.yml new file mode 100644 index 0000000000000000000000000000000000000000..a65860a5f852b0de755b453a3da0023364a1c4c6 --- /dev/null +++ b/changelogs/unreleased/322043-insert-plan-trial.yml @@ -0,0 +1,5 @@ +--- +title: Add a migration to insert trail plans within SAAS for Ultimate and Premium plans +merge_request: 57814 +author: +type: added diff --git a/db/post_migrate/20210329102724_add_new_trail_plans.rb b/db/post_migrate/20210329102724_add_new_trail_plans.rb new file mode 100644 index 0000000000000000000000000000000000000000..b142f6385f78cb099eb50d6c38edec9d378199a8 --- /dev/null +++ b/db/post_migrate/20210329102724_add_new_trail_plans.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +class AddNewTrailPlans < ActiveRecord::Migration[6.0] + class Plan < ActiveRecord::Base + self.inheritance_column = :_type_disabled + + has_one :limits, class_name: 'PlanLimits' + + def actual_limits + self.limits || self.build_limits + end + end + + class PlanLimits < ActiveRecord::Base + self.inheritance_column = :_type_disabled + + belongs_to :plan + end + + def create_plan_limits(plan_limit_name, plan) + plan_limit = Plan.find_or_initialize_by(name: plan_limit_name).actual_limits.dup + plan_limit.plan = plan + plan_limit.save! + end + + def up + return unless Gitlab.dev_env_or_com? + + ultimate_trial = Plan.create!(name: 'ultimate_trial', title: 'Ultimate Trial') + premium_trial = Plan.create!(name: 'premium_trial', title: 'Premium Trial') + + create_plan_limits('gold', ultimate_trial) + create_plan_limits('silver', premium_trial) + end + + def down + return unless Gitlab.dev_env_or_com? + + Plan.where(name: %w(ultimate_trial premium_trial)).delete_all + end +end diff --git a/db/schema_migrations/20210329102724 b/db/schema_migrations/20210329102724 new file mode 100644 index 0000000000000000000000000000000000000000..b2fdccf2bb878db93d78a80c14b15433149c650f --- /dev/null +++ b/db/schema_migrations/20210329102724 @@ -0,0 +1 @@ +b40c702ea6b2120da6fe11b213064a7a124dbc86bfb2d6785bfd2274c44f1e22 \ No newline at end of file diff --git a/spec/migrations/add_new_trail_plans_spec.rb b/spec/migrations/add_new_trail_plans_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..8ba6da11ad10df3ab5d2af1541a9a5ce2f1985b2 --- /dev/null +++ b/spec/migrations/add_new_trail_plans_spec.rb @@ -0,0 +1,95 @@ +# frozen_string_literal: true + +require 'spec_helper' + +require_migration! + +RSpec.describe AddNewTrailPlans, :migration do + describe '#up' do + before do + allow(Gitlab).to receive(:dev_env_or_com?).and_return true + end + + it 'creates 2 entries within the plans table' do + expect { migrate! }.to change { AddNewTrailPlans::Plan.count }.by 2 + expect(AddNewTrailPlans::Plan.last(2).pluck(:name)).to match_array(%w(ultimate_trial premium_trial)) + end + + it 'creates 2 entries for plan limits' do + expect { migrate! }.to change { AddNewTrailPlans::PlanLimits.count }.by 2 + end + + context 'when the plan limits for gold and silver exists' do + before do + table(:plans).create!(id: 1, name: 'gold', title: 'Gold') + table(:plan_limits).create!(id: 1, plan_id: 1, storage_size_limit: 2000) + table(:plans).create!(id: 2, name: 'silver', title: 'Silver') + table(:plan_limits).create!(id: 2, plan_id: 2, storage_size_limit: 1000) + end + + it 'duplicates the gold and silvers plan limits entries' do + migrate! + + ultimate_plan_limits = AddNewTrailPlans::Plan.find_by(name: 'ultimate_trial').limits + expect(ultimate_plan_limits.storage_size_limit).to be 2000 + + premium_plan_limits = AddNewTrailPlans::Plan.find_by(name: 'premium_trial').limits + expect(premium_plan_limits.storage_size_limit).to be 1000 + end + end + + context 'when the instance is not SaaS' do + before do + allow(Gitlab).to receive(:dev_env_or_com?).and_return false + end + + it 'does not create plans and plan limits and returns' do + expect { migrate! }.not_to change { AddNewTrailPlans::Plan.count } + expect { migrate! }.not_to change { AddNewTrailPlans::Plan.count } + end + end + end + + describe '#down' do + before do + table(:plans).create!(id: 3, name: 'other') + table(:plan_limits).create!(plan_id: 3) + end + + context 'when the instance is SaaS' do + before do + allow(Gitlab).to receive(:dev_env_or_com?).and_return true + end + + it 'removes the newly added ultimate and premium trial entries' do + migrate! + + expect { described_class.new.down }.to change { AddNewTrailPlans::Plan.count }.by(-2) + expect(AddNewTrailPlans::Plan.find_by(name: 'premium_trial')).to be_nil + expect(AddNewTrailPlans::Plan.find_by(name: 'ultimate_trial')).to be_nil + + other_plan = AddNewTrailPlans::Plan.find_by(name: 'other') + expect(other_plan).to be_persisted + expect(AddNewTrailPlans::PlanLimits.count).to eq(1) + expect(AddNewTrailPlans::PlanLimits.first.plan_id).to eq(other_plan.id) + end + end + + context 'when the instance is not SaaS' do + before do + allow(Gitlab).to receive(:dev_env_or_com?).and_return false + table(:plans).create!(id: 1, name: 'ultimate_trial', title: 'Ultimate Trial') + table(:plans).create!(id: 2, name: 'premium_trial', title: 'Premium Trial') + table(:plan_limits).create!(id: 1, plan_id: 1) + table(:plan_limits).create!(id: 2, plan_id: 2) + end + + it 'does not delete plans and plan limits and returns' do + migrate! + + expect { described_class.new.down }.not_to change { AddNewTrailPlans::Plan.count } + expect(AddNewTrailPlans::PlanLimits.count).to eq(3) + end + end + end +end