Use NOT VALID to enforce a not null constraint on file_store

When adding a constraint to a table, we can optionally include
NOT VALID. This tells Postgres that it doesn’t need to enforce
the constraint on existing records in the table. At least not
immediately. This constraint will be enforced for any updates
and subsequent insertions. Thus, we can immediately enforce
the constraint while giving ourselves time to clean up any
existing records that conflict with the constraint.
parent 1a30d4aa
# frozen_string_literal: true
class AddNotNullConstraintOnFileStoreToLfsObjects < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
CONSTRAINT_NAME = 'lfs_objects_file_store_not_null'
DOWNTIME = false
def up
with_lock_retries do
execute <<~SQL
ALTER TABLE lfs_objects ADD CONSTRAINT #{CONSTRAINT_NAME} CHECK (file_store IS NOT NULL) NOT VALID;
SQL
end
end
def down
with_lock_retries do
execute <<~SQL
ALTER TABLE lfs_objects DROP CONSTRAINT IF EXISTS #{CONSTRAINT_NAME};
SQL
end
end
end
# frozen_string_literal: true
class AddNotNullConstraintOnFileStoreToCiJobArtifacts < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
CONSTRAINT_NAME = 'ci_job_artifacts_file_store_not_null'
DOWNTIME = false
def up
with_lock_retries do
execute <<~SQL
ALTER TABLE ci_job_artifacts ADD CONSTRAINT #{CONSTRAINT_NAME} CHECK (file_store IS NOT NULL) NOT VALID;
SQL
end
end
def down
with_lock_retries do
execute <<~SQL
ALTER TABLE ci_job_artifacts DROP CONSTRAINT IF EXISTS #{CONSTRAINT_NAME};
SQL
end
end
end
# frozen_string_literal: true
class AddNotNullConstraintOnFileStoreToUploads < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
CONSTRAINT_NAME = 'uploads_store_not_null'
DOWNTIME = false
def up
with_lock_retries do
execute <<~SQL
ALTER TABLE uploads ADD CONSTRAINT #{CONSTRAINT_NAME} CHECK (store IS NOT NULL) NOT VALID;
SQL
end
end
def down
with_lock_retries do
execute <<~SQL
ALTER TABLE uploads DROP CONSTRAINT IF EXISTS #{CONSTRAINT_NAME};
SQL
end
end
end
......@@ -7697,6 +7697,9 @@ ALTER TABLE ONLY public.ci_daily_report_results
ALTER TABLE ONLY public.ci_group_variables
ADD CONSTRAINT ci_group_variables_pkey PRIMARY KEY (id);
ALTER TABLE public.ci_job_artifacts
ADD CONSTRAINT ci_job_artifacts_file_store_not_null CHECK ((file_store IS NOT NULL)) NOT VALID;
ALTER TABLE ONLY public.ci_job_artifacts
ADD CONSTRAINT ci_job_artifacts_pkey PRIMARY KEY (id);
......@@ -8054,6 +8057,9 @@ ALTER TABLE ONLY public.ldap_group_links
ALTER TABLE ONLY public.lfs_file_locks
ADD CONSTRAINT lfs_file_locks_pkey PRIMARY KEY (id);
ALTER TABLE public.lfs_objects
ADD CONSTRAINT lfs_objects_file_store_not_null CHECK ((file_store IS NOT NULL)) NOT VALID;
ALTER TABLE ONLY public.lfs_objects
ADD CONSTRAINT lfs_objects_pkey PRIMARY KEY (id);
......@@ -8447,6 +8453,9 @@ ALTER TABLE ONLY public.u2f_registrations
ALTER TABLE ONLY public.uploads
ADD CONSTRAINT uploads_pkey PRIMARY KEY (id);
ALTER TABLE public.uploads
ADD CONSTRAINT uploads_store_not_null CHECK ((store IS NOT NULL)) NOT VALID;
ALTER TABLE ONLY public.user_agent_details
ADD CONSTRAINT user_agent_details_pkey PRIMARY KEY (id);
......@@ -13127,6 +13136,9 @@ COPY "schema_migrations" (version) FROM STDIN;
20200403185127
20200403185422
20200406135648
20200406165950
20200406171857
20200406172135
20200406192059
20200407094005
20200407094923
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment