Commit 241242af authored by Andreas Brandl's avatar Andreas Brandl

Merge branch 'remove-empty-github-service-templates' into 'master'

Remove empty Github service templates from db

See merge request gitlab-org/gitlab!18868
parents 134974f5 44319fd5
---
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