Skip to content

Use dynamic mapping in trigger to sync `integrations.type_new`

What does this MR do?

In !66541 (merged) we ended up going with a static mapping for this trigger, and figured any new integrations could just start using the correct class name in type directly.

But our plan is to drop the type column at some point and replace it with type_new, so we still need to sync the class name to type_new for new integrations as well.

So the trigger is changed to transform all *Service values into Integrations::*, and copy any other values unmodified.

Follow-up to #333506 (closed) / !66541 (merged), in response to !67941 (comment 653170448) where we add a new integration.

Migration output

$ rails db:migrate:up VERSION=20210818175949
== 20210818175949 UpdateIntegrationsTriggerTypeNewOnInsert: migrating =========
-- execute("CREATE OR REPLACE FUNCTION integrations_set_type_new()\nRETURNS TRIGGER AS\n$$\nBEGIN\nUPDATE integrations SET type_new = regexp_replace(NEW.type, '\\A(.+)Service\\Z', 'Integrations::\\1')\nWHERE integrations.id = NEW.id;\nRETURN NULL;\n\nEND\n$$ LANGUAGE PLPGSQL\n")
   -> 0.0033s
== 20210818175949 UpdateIntegrationsTriggerTypeNewOnInsert: migrated (0.0034s)

$ rails db:migrate:down VERSION=20210818175949
== 20210818175949 UpdateIntegrationsTriggerTypeNewOnInsert: reverting =========
-- execute("CREATE OR REPLACE FUNCTION integrations_set_type_new()\nRETURNS TRIGGER AS\n$$\nBEGIN\nWITH mapping(old_type, new_type) AS (VALUES\n  ('AsanaService',                   'Integrations::Asana'),\n  ('AssemblaService',                'Integrations::Assembla'),\n  ('BambooService',                  'Integrations::Bamboo'),\n  ('BugzillaService',                'Integrations::Bugzilla'),\n  ('BuildkiteService',               'Integrations::Buildkite'),\n  ('CampfireService',                'Integrations::Campfire'),\n  ('ConfluenceService',              'Integrations::Confluence'),\n  ('CustomIssueTrackerService',      'Integrations::CustomIssueTracker'),\n  ('DatadogService',                 'Integrations::Datadog'),\n  ('DiscordService',                 'Integrations::Discord'),\n  ('DroneCiService',                 'Integrations::DroneCi'),\n  ('EmailsOnPushService',            'Integrations::EmailsOnPush'),\n  ('EwmService',                     'Integrations::Ewm'),\n  ('ExternalWikiService',            'Integrations::ExternalWiki'),\n  ('FlowdockService',                'Integrations::Flowdock'),\n  ('HangoutsChatService',            'Integrations::HangoutsChat'),\n  ('IrkerService',                   'Integrations::Irker'),\n  ('JenkinsService',                 'Integrations::Jenkins'),\n  ('JiraService',                    'Integrations::Jira'),\n  ('MattermostService',              'Integrations::Mattermost'),\n  ('MattermostSlashCommandsService', 'Integrations::MattermostSlashCommands'),\n  ('MicrosoftTeamsService',          'Integrations::MicrosoftTeams'),\n  ('MockCiService',                  'Integrations::MockCi'),\n  ('MockMonitoringService',          'Integrations::MockMonitoring'),\n  ('PackagistService',               'Integrations::Packagist'),\n  ('PipelinesEmailService',          'Integrations::PipelinesEmail'),\n  ('PivotaltrackerService',          'Integrations::Pivotaltracker'),\n  ('PrometheusService',              'Integrations::Prometheus'),\n  ('PushoverService',                'Integrations::Pushover'),\n  ('RedmineService',                 'Integrations::Redmine'),\n  ('SlackService',                   'Integrations::Slack'),\n  ('SlackSlashCommandsService',      'Integrations::SlackSlashCommands'),\n  ('TeamcityService',                'Integrations::Teamcity'),\n  ('UnifyCircuitService',            'Integrations::UnifyCircuit'),\n  ('YoutrackService',                'Integrations::Youtrack'),\n  ('WebexTeamsService',              'Integrations::WebexTeams'),\n\n  -- EE-only integrations\n  ('GithubService',                  'Integrations::Github'),\n  ('GitlabSlackApplicationService',  'Integrations::GitlabSlackApplication')\n)\n\nUPDATE integrations SET type_new = mapping.new_type\nFROM mapping\nWHERE integrations.id = NEW.id\n  AND mapping.old_type = NEW.type;\nRETURN NULL;\n\nEND\n$$ LANGUAGE PLPGSQL\n")
   -> 0.0025s
== 20210818175949 UpdateIntegrationsTriggerTypeNewOnInsert: reverted (0.0026s)

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Related to #333506 (closed)

Edited by Markus Koller

Merge request reports

Loading