Merge branch '343134-create-open-source-plan' into 'master'

Add opensource plan to database

See merge request gitlab-org/gitlab!73925
This commit is contained in:
Krasimir Angelov 2021-11-10 00:06:04 +00:00
commit 8bfdf3082c
3 changed files with 126 additions and 0 deletions

View file

@ -0,0 +1,39 @@
# frozen_string_literal: true
class AddOpenSourcePlan < Gitlab::Database::Migration[1.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?
opensource = Plan.create!(name: 'opensource', title: 'Open Source Program')
create_plan_limits('ultimate', opensource)
end
def down
return unless Gitlab.dev_env_or_com?
Plan.where(name: 'opensource').delete_all
end
end

View file

@ -0,0 +1 @@
e1f9d87287048010e9816fd5b4a9a2d30b64d2ad150226852f6679b950031914

View file

@ -0,0 +1,86 @@
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe AddOpenSourcePlan, :migration do
describe '#up' do
before do
allow(Gitlab).to receive(:dev_env_or_com?).and_return true
end
it 'creates 1 entry within the plans table' do
expect { migrate! }.to change { AddOpenSourcePlan::Plan.count }.by 1
expect(AddOpenSourcePlan::Plan.last.name).to eql('opensource')
end
it 'creates 1 entry for plan limits' do
expect { migrate! }.to change { AddOpenSourcePlan::PlanLimits.count }.by 1
end
context 'when the plan limits for gold and silver exists' do
before do
table(:plans).create!(id: 1, name: 'ultimate', title: 'Ultimate')
table(:plan_limits).create!(id: 1, plan_id: 1, storage_size_limit: 2000)
end
it 'duplicates the gold and silvers plan limits entries' do
migrate!
opensource_limits = AddOpenSourcePlan::Plan.find_by(name: 'opensource').limits
expect(opensource_limits.storage_size_limit).to be 2000
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 { AddOpenSourcePlan::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 opensource entry' do
migrate!
expect { described_class.new.down }.to change { AddOpenSourcePlan::Plan.count }.by(-1)
expect(AddOpenSourcePlan::Plan.find_by(name: 'opensource')).to be_nil
other_plan = AddOpenSourcePlan::Plan.find_by(name: 'other')
expect(other_plan).to be_persisted
expect(AddOpenSourcePlan::PlanLimits.count).to eq(1)
expect(AddOpenSourcePlan::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: 'opensource', title: 'Open Source Program')
table(:plan_limits).create!(id: 1, plan_id: 1)
end
it 'does not delete plans and plan limits and returns' do
migrate!
expect { described_class.new.down }.not_to change { AddOpenSourcePlan::Plan.count }
expect(AddOpenSourcePlan::PlanLimits.count).to eq(2)
end
end
end
end