Commit 44319fd5 authored by Marius Bobin's avatar Marius Bobin Committed by Andreas Brandl

Remove empty Github service templates from db

When creating a new Github integration, it looks for a service
record of the same type, but with the template flag set to true,
and clone its attributes before presenting the record to the user.

`https://gitlab.com/gitlab-org/gitlab/merge_requests/15737/diffs`
added `static_context` as a default attribute on for
this service but it clashes with the empty template on .com.

This commit removes Github templates that have `nil` or `{}`
as properties, allowing new services to set the `static_context`
default value from the code.
parent 134974f5
---
title: Remove empty Github service templates from database
merge_request: 18868
author:
type: fixed
# frozen_string_literal: true
## It's expected to delete one record on GitLab.com
#
class RemoveEmptyGithubServiceTemplates < ActiveRecord::Migration[5.2]
DOWNTIME = false
class Service < ActiveRecord::Base
self.table_name = 'services'
self.inheritance_column = :_type_disabled
serialize :properties, JSON
end
def up
relationship.where(properties: {}).delete_all
end
def down
relationship.find_or_create_by!(properties: {})
end
private
def relationship
RemoveEmptyGithubServiceTemplates::Service.where(template: true, type: 'GithubService')
end
end
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20191021101942_remove_empty_github_service_templates.rb')
describe RemoveEmptyGithubServiceTemplates, :migration do
subject(:migration) { described_class.new }
let(:services) do
table(:services).tap do |klass|
klass.class_eval do
serialize :properties, JSON
end
end
end
before do
services.delete_all
create_service(properties: nil)
create_service(properties: {})
create_service(properties: { some: :value })
create_service(properties: {}, template: false)
create_service(properties: {}, type: 'SomeType')
end
def all_service_properties
services.where(template: true, type: 'GithubService').pluck(:properties)
end
it 'correctly migrates up and down service templates' do
reversible_migration do |migration|
migration.before -> do
expect(services.count).to eq(5)
expect(all_service_properties)
.to match(a_collection_containing_exactly(nil, {}, { 'some' => 'value' }))
end
migration.after -> do
expect(services.count).to eq(4)
expect(all_service_properties)
.to match(a_collection_containing_exactly(nil, { 'some' => 'value' }))
end
end
end
def create_service(params)
data = { template: true, type: 'GithubService' }
data.merge!(params)
services.create!(data)
end
end
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