Skip to content

Use loose foreign key for deployment_clusters.cluster_id

What does this MR do and why?

Replaces the foreign key constraint for deployment_clusters.cluster_id with a loose foreign key.

Some clusters have a very large number of associated deployments, which can prevent the cluster from being deleted as the statement timeout is reached while executing foreign key actions. A loose foreign key defers the foreign key action, allowing the cluster to be deleted without error.

Migration output

main: == 20220609195803 RemoveDeploymentClustersClusterIdFk: migrating ==============
main: -- transaction_open?()
main:    -> 0.0000s
main: -- foreign_keys(:deployment_clusters)
main:    -> 0.0024s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("LOCK TABLE clusters, deployment_clusters IN ACCESS EXCLUSIVE MODE")
main:    -> 0.0003s
main: -- remove_foreign_key(:deployment_clusters, :clusters, {:name=>"fk_rails_4e6243e120"})
main:    -> 0.0024s
main: == 20220609195803 RemoveDeploymentClustersClusterIdFk: migrated (0.0068s) =====
main: == 20220609195803 RemoveDeploymentClustersClusterIdFk: reverting ==============
main: -- transaction_open?()
main:    -> 0.0000s
main: -- foreign_keys(:deployment_clusters)
main:    -> 0.0044s
main: -- transaction_open?()
main:    -> 0.0000s
main: -- execute("ALTER TABLE deployment_clusters\nADD CONSTRAINT fk_rails_4e6243e120\nFOREIGN KEY (cluster_id)\nREFERENCES clusters (id)\nON DELETE CASCADE\nNOT VALID;\n")
main:    -> 0.0026s
main: -- execute("SET statement_timeout TO 0")
main:    -> 0.0001s
main: -- execute("ALTER TABLE deployment_clusters VALIDATE CONSTRAINT fk_rails_4e6243e120;")
main:    -> 0.0028s
main: -- execute("RESET statement_timeout")
main:    -> 0.0002s
main: == 20220609195803 RemoveDeploymentClustersClusterIdFk: reverted (0.0161s) =====

How to set up and validate locally

  1. Create a legacy certificate-based cluster
  2. Run a pipeline with the following job definition to create a deployment:
      deploy:
        stage: deploy
        environment: test
        script:
          - echo 'deploy'
  3. Delete the cluster
  4. Verify the job trace from the pipeline is viewable without error, and that the associated cluster_deployment record is (asynchronously) removed.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Related to #364575 (closed)

Edited by Tiger Watson

Merge request reports