diff options
Diffstat (limited to 'db/migrate/20220211214605_update_integrations_trigger_type_new_on_insert_null_safe.rb')
-rw-r--r-- | db/migrate/20220211214605_update_integrations_trigger_type_new_on_insert_null_safe.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/db/migrate/20220211214605_update_integrations_trigger_type_new_on_insert_null_safe.rb b/db/migrate/20220211214605_update_integrations_trigger_type_new_on_insert_null_safe.rb new file mode 100644 index 00000000000..7a60ea48f40 --- /dev/null +++ b/db/migrate/20220211214605_update_integrations_trigger_type_new_on_insert_null_safe.rb @@ -0,0 +1,36 @@ +# 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 |