Skip to content

Add 'instance' column to services table

Arturo Herrero requested to merge 204801-add-instance-to-services into master

What does this MR do?

As part of Mass-integration at a Group and Instance Level &2137, we are moving service templates to become instance-level service configuration &2430.

Following our plan #204770 (closed), we are going to support both features at the same time.

This merge request sets the foundations to enable that feature, creating the instance column in the services table. This is needed to support instance-level integrations alongside service templates.

There are a few things related to the new attribute mimicking the service templates behavior:

  • instance attribute is a safe attribute to be updated
  • instance services are not imported

We will continue adjusting the backend behavior as part of #199388 (closed).

We also want to make sure that we always have the correct state in the database. This merge request is also adding the following validations #208925 (closed):

  • Validates presence of project_id if not instance
  • Validates absence of project_id if instance
  • Validates only one instance-level service per type
  • Validates service is instance or template

Migration script execution

== 20200310132654 AddInstanceToServices: migrating ============================
-- transaction_open?()
   -> 0.0000s
-- execute("SET statement_timeout TO 0")
   -> 0.0009s
-- transaction()
-- add_column(:services, :instance, :boolean, {:default=>nil})
   -> 0.0024s
-- change_column_default(:services, :instance, false)
   -> 0.0032s
   -> 0.0067s
-- columns(:services)
   -> 0.0015s
-- transaction_open?()
   -> 0.0000s
-- exec_query("SELECT COUNT(*) AS count FROM \"services\"")
   -> 0.0015s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" ORDER BY \"services\".\"id\" ASC LIMIT 1")
   -> 0.0013s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 1 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0008s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 1 AND \"services\".\"id\" < 2")
   -> 0.0009s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 2 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0007s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 2 AND \"services\".\"id\" < 3")
   -> 0.0007s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 3 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0006s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 3 AND \"services\".\"id\" < 4")
   -> 0.0007s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 4 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0007s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 4 AND \"services\".\"id\" < 5")
   -> 0.0008s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 5 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0006s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 5 AND \"services\".\"id\" < 6")
   -> 0.0007s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 6 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0007s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 6 AND \"services\".\"id\" < 7")
   -> 0.0008s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 7 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0006s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 7 AND \"services\".\"id\" < 8")
   -> 0.0007s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 8 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0006s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 8 AND \"services\".\"id\" < 9")
   -> 0.0007s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 9 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0009s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 9 AND \"services\".\"id\" < 10")
   -> 0.0009s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 10 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0007s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 10 AND \"services\".\"id\" < 11")
   -> 0.0008s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 11 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0006s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 11 AND \"services\".\"id\" < 12")
   -> 0.0007s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 12 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0007s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 12 AND \"services\".\"id\" < 13")
   -> 0.0008s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 13 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0007s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 13 AND \"services\".\"id\" < 14")
   -> 0.0008s
-- exec_query("SELECT \"services\".\"id\" FROM \"services\" WHERE \"services\".\"id\" >= 14 ORDER BY \"services\".\"id\" ASC LIMIT 1 OFFSET 1")
   -> 0.0007s
-- execute("UPDATE \"services\" SET \"instance\" = FALSE WHERE \"services\".\"id\" >= 14")
   -> 0.0008s
-- change_column_null(:services, :instance, false)
   -> 0.0009s
-- execute("RESET ALL")
   -> 0.0005s
== 20200310132654 AddInstanceToServices: migrated (0.0493s) ===================

== 20200310135823 AddIndexToServiceUniqueInstancePerType: migrating ===========
-- transaction_open?()
   -> 0.0000s
-- index_exists?(:services, [:type, :instance], {:unique=>true, :where=>"instance IS TRUE", :algorithm=>:concurrently})
   -> 0.0032s
-- execute("SET statement_timeout TO 0")
   -> 0.0005s
-- add_index(:services, [:type, :instance], {:unique=>true, :where=>"instance IS TRUE", :algorithm=>:concurrently})
   -> 0.0033s
-- execute("RESET ALL")
   -> 0.0005s
== 20200310135823 AddIndexToServiceUniqueInstancePerType: migrated (0.0077s) ==

Closes #204801 (closed).

Edited by 🤖 GitLab Bot 🤖

Merge request reports

Loading