Commit 1efd2241 authored by Etienne Baqué's avatar Etienne Baqué Committed by Mayra Cabrera

Add migration to update subscription plans

parent a7111b39
---
title: Add migration to update plans on new post-EoA subscriptions
merge_request: 55625
author:
type: changed
# frozen_string_literal: true
class UpdateGitlabSubscriptionsStartAtPostEoa < ActiveRecord::Migration[6.0]
UPDATE_BATCH_SIZE = 100
disable_ddl_transaction!
class Plan < ActiveRecord::Base
self.table_name = 'plans'
self.inheritance_column = :_type_disabled
end
class GitlabSubscription < ActiveRecord::Base
include EachBatch
self.table_name = 'gitlab_subscriptions'
self.inheritance_column = :_type_disabled
EOA_ROLLOUT_DATE = '2021-01-26'
scope :with_plan, -> (from_plan) do
where("start_date >= ? AND hosted_plan_id = ?", EOA_ROLLOUT_DATE, from_plan.id)
end
end
def up
return unless Gitlab.com?
silver_plan = Plan.find_by(name: 'silver')
gold_plan = Plan.find_by(name: 'gold')
premium_plan = Plan.find_by(name: 'premium')
ultimate_plan = Plan.find_by(name: 'ultimate')
# Silver to Premium
update_hosted_plan_for_subscription(from_plan: silver_plan, to_plan: premium_plan)
# Gold to Ultimate
update_hosted_plan_for_subscription(from_plan: gold_plan, to_plan: ultimate_plan)
end
def down
# no-op
end
private
def update_hosted_plan_for_subscription(from_plan:, to_plan:)
return unless from_plan && to_plan
GitlabSubscription.with_plan(from_plan).each_batch(of: UPDATE_BATCH_SIZE) do |batch|
batch.update_all(hosted_plan_id: to_plan.id)
end
end
end
cef2421a6885cb8b28d34388af6c79c4be1564dfd5fae2efcb35622d511eb8c0
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210303121224_update_gitlab_subscriptions_start_at_post_eoa.rb')
RSpec.describe UpdateGitlabSubscriptionsStartAtPostEoa do
let(:migration) { described_class.new }
let(:eoa_rollout_date) { described_class::GitlabSubscription::EOA_ROLLOUT_DATE.to_date }
let(:gitlab_subscriptions_table) { table(:gitlab_subscriptions) }
let(:plans_table) { table(:plans) }
let(:namespaces_table) { table(:namespaces) }
let!(:namespace_one) { namespaces_table.create!(name: 'namespace1', path: 'path1') }
let!(:namespace_two) { namespaces_table.create!(name: 'namespace2', path: 'path2') }
let!(:free_plan) { plans_table.create!(name: 'free') }
let!(:silver_plan) { plans_table.create!(name: 'silver') }
let!(:gold_plan) { plans_table.create!(name: 'gold') }
let!(:premium_plan) { plans_table.create!(name: 'premium') }
let!(:ultimate_plan) { plans_table.create!(name: 'ultimate') }
describe '#up' do
context 'when not on GitLab.com' do
before do
allow(Gitlab).to receive(:com?).and_return(false)
end
it 'does not do any update' do
expect(migration).not_to receive(:update_hosted_plan_for_subscription)
migration.up
end
end
context 'when on Gitlab.com' do
before do
allow(Gitlab).to receive(:com?).and_return(true)
end
it 'updates the silver and gold post-EoA subscriptions' do
silver_sub = gitlab_subscriptions_table.create!(
start_date: eoa_rollout_date + 2.days,
hosted_plan_id: silver_plan.id,
namespace_id: namespace_two.id
)
gold_sub = gitlab_subscriptions_table.create!(
start_date: eoa_rollout_date + 5.days,
hosted_plan_id: gold_plan.id,
namespace_id: namespace_one.id
)
migration.up
expect(silver_sub.reload.hosted_plan_id).to eq(premium_plan.id)
expect(gold_sub.reload.hosted_plan_id).to eq(ultimate_plan.id)
end
context 'when a subscription occurred before eoa date' do
it 'is not being updated' do
silver_sub = gitlab_subscriptions_table.create!(
start_date: eoa_rollout_date - 3.days,
hosted_plan_id: silver_plan.id,
namespace_id: namespace_two.id
)
migration.up
expect(silver_sub.reload.hosted_plan_id).to eq(silver_plan.id)
end
end
context 'when a subscription other than gold and silver was bought after eoa date' do
it 'is not being updated' do
free_sub = gitlab_subscriptions_table.create!(
start_date: eoa_rollout_date + 2.days,
hosted_plan_id: free_plan.id,
namespace_id: namespace_one.id
)
migration.up
expect(free_sub.reload.hosted_plan_id).to eq(free_plan.id)
end
end
end
end
end
......@@ -7,9 +7,11 @@ RSpec.describe Subscriptions::NewPlanPresenter do
using RSpec::Parameterized::TableSyntax
where(:legacy_name, :new_title) do
'bronze' | 'Bronze'
'silver' | 'Premium (Formerly Silver)'
'gold' | 'Ultimate (Formerly Gold)'
'bronze' | 'Bronze'
'silver' | 'Premium (Formerly Silver)'
'gold' | 'Ultimate (Formerly Gold)'
'premium' | 'Premium'
'ultimate' | 'Ultimate'
end
with_them do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment