Skip to content

Add sequence migration helper

Max Orefice requested to merge morefice/add-drop-sequence-helper into master

Ref: #362984 (closed)

What does this MR do and why?

This MR adds a new migration helper method to drop unused sequences generated by postgres easily.

Why do we need this?

It was brought up in #362984 (closed) that we need to remove existing sequences which are not being used.

This MR simplifies the process to remove them.

Fake migration test

class DropSequenceTest < Gitlab::Database::Migration[2.0]
  def up
    drop_sequence(:ci_pipelines_config, :pipeline_id, :ci_pipelines_config_pipeline_id_seq)
  end

  def down
    add_sequence(:ci_pipelines_config, :pipeline_id, :ci_pipelines_config_pipeline_id_seq, 10)
  end
end
== 20220524102448 DropSequenceTest: migrating ================================
-- quote_table_name(:ci_pipelines_config)
   -> 0.0000s
-- quote_column_name(:pipeline_id)
   -> 0.0000s
-- quote_table_name(:ci_pipelines_config_pipeline_id_seq)
   -> 0.0000s
-- execute("ALTER TABLE \"ci_pipelines_config\" ALTER COLUMN \"pipeline_id\" DROP DEFAULT;\nDROP SEQUENCE IF EXISTS \"ci_pipelines_config_pipeline_id_seq\"\n")
   -> 0.0015s
== 20220524102448 DropSequenceTest: migrated (0.0016s) =======================


--------------- ROLLBACK ---------------------

== 20220524102448 DropSequenceTest: reverting ================================
-- quote_table_name(:ci_pipelines_config)
   -> 0.0000s
-- quote_column_name(:pipeline_id)
   -> 0.0000s
-- execute("CREATE SEQUENCE ci_pipelines_config_pipeline_id_seq START 10;\nALTER TABLE \"ci_pipelines_config\" ALTER COLUMN \"pipeline_id\" SET DEFAULT nextval('ci_pipelines_config_pipeline_id_seq')\n")
   -> 0.0035s
== 20220524102448 DropSequenceTest: reverted (0.0036s) =======================
-CREATE SEQUENCE ci_pipelines_config_pipeline_id_seq
-    START WITH 1
-    INCREMENT BY 1
-    NO MINVALUE
-    NO MAXVALUE
-    CACHE 1;
-
-ALTER SEQUENCE ci_pipelines_config_pipeline_id_seq OWNED BY ci_pipelines_config.pipeline_id;
-
-ALTER TABLE ONLY ci_pipelines_config ALTER COLUMN pipeline_id SET DEFAULT nextval('ci_pipelines_config_pipeline_id_seq'::regclass);

MR acceptance checklist

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

Edited by Max Orefice

Merge request reports

Loading