Commit 87ad62ba authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '333508-integrations-type-new-trigger-null-safe' into 'master'

NULL safe integrations type maintainance trigger

See merge request gitlab-org/gitlab!80537
parents e068823e 2e114fce
# frozen_string_literal: true
class UpdateIntegrationsTriggerTypeNewOnInsertNullSafe < Gitlab::Database::Migration[1.0]
include Gitlab::Database::SchemaHelpers
FUNCTION_NAME = 'integrations_set_type_new'
def up
# Update `type_new` dynamically based on `type`, if `type_new` is null
# and `type` dynamically based on `type_new`, if `type` is null.
#
# The old class names are in the format `AbcService`, and the new ones `Integrations::Abc`.
create_trigger_function(FUNCTION_NAME, replace: true) do
<<~SQL.squish
UPDATE integrations
SET type_new = COALESCE(NEW.type_new, regexp_replace(NEW.type, '\\A(.+)Service\\Z', 'Integrations::\\1'))
, type = COALESCE(NEW.type, regexp_replace(NEW.type_new, '\\AIntegrations::(.+)\\Z', '\\1Service'))
WHERE integrations.id = NEW.id;
RETURN NULL;
SQL
end
end
def down
# Update `type_new` dynamically based on `type`.
#
# The old class names are in the format `AbcService`, and the new ones `Integrations::Abc`.
create_trigger_function(FUNCTION_NAME, replace: true) do
<<~SQL
UPDATE integrations SET type_new = regexp_replace(NEW.type, '\\A(.+)Service\\Z', 'Integrations::\\1')
WHERE integrations.id = NEW.id;
RETURN NULL;
SQL
end
end
end
add5ee0b8e090ba740ce738f66ea4f741485a0c84728fecfa5b0488564d55536
\ No newline at end of file
...@@ -93,10 +93,7 @@ CREATE FUNCTION integrations_set_type_new() RETURNS trigger ...@@ -93,10 +93,7 @@ CREATE FUNCTION integrations_set_type_new() RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
BEGIN BEGIN
UPDATE integrations SET type_new = regexp_replace(NEW.type, '\A(.+)Service\Z', 'Integrations::\1') UPDATE integrations SET type_new = COALESCE(NEW.type_new, regexp_replace(NEW.type, '\A(.+)Service\Z', 'Integrations::\1')) , type = COALESCE(NEW.type, regexp_replace(NEW.type_new, '\AIntegrations::(.+)\Z', '\1Service')) WHERE integrations.id = NEW.id; RETURN NULL;
WHERE integrations.id = NEW.id;
RETURN NULL;
END END
$$; $$;
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe UpdateIntegrationsTriggerTypeNewOnInsertNullSafe, :migration do
let(:integrations) { table(:integrations) }
before do
migrate!
end
it 'leaves defined values alone' do
record = integrations.create!(type: 'XService', type_new: 'Integrations::Y')
expect(integrations.find(record.id)).to have_attributes(type: 'XService', type_new: 'Integrations::Y')
end
it 'keeps type_new synchronized with type' do
record = integrations.create!(type: 'AbcService', type_new: nil)
expect(integrations.find(record.id)).to have_attributes(
type: 'AbcService',
type_new: 'Integrations::Abc'
)
end
it 'keeps type synchronized with type_new' do
record = integrations.create!(type: nil, type_new: 'Integrations::Abc')
expect(integrations.find(record.id)).to have_attributes(
type: 'AbcService',
type_new: 'Integrations::Abc'
)
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