Commit 5df1c240 authored by Maxime Orefice's avatar Maxime Orefice

Add not null constraint to PipelineArtifact file

This MR makes sure we don't allow nil record to be
persisted in the databse. Otherwise we would not be able
to generate the right data without a valid file.
parent c94a823d
......@@ -16,7 +16,7 @@ module Ci
belongs_to :project, class_name: "Project", inverse_of: :pipeline_artifacts
belongs_to :pipeline, class_name: "Ci::Pipeline", inverse_of: :pipeline_artifacts
validates :pipeline, :project, :file_format, presence: true
validates :pipeline, :project, :file_format, :file, presence: true
validates :file_store, presence: true, inclusion: { in: FILE_STORE_SUPPORTED }
validates :size, presence: true, numericality: { less_than_or_equal_to: FILE_SIZE_LIMIT }
validates :file_type, presence: true, uniqueness: { scope: [:pipeline_id] }
......
---
title: 'Add not null constraint for file to ci_pipeline_artifacts'
merge_request: 39118
author:
type: fixed
# frozen_string_literal: true
class AddNotNullConstraintToCiPipelineArtifactFile < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_not_null_constraint :ci_pipeline_artifacts, :file, validate: true
end
def down
remove_not_null_constraint :ci_pipeline_artifacts, :file
end
end
b5400a5e292f793986b102c7ddd0df8c7c5a7704b5121086a87792f7cdd0a2aa
\ No newline at end of file
......@@ -10059,7 +10059,8 @@ CREATE TABLE public.ci_pipeline_artifacts (
file_format smallint NOT NULL,
file text,
expire_at timestamp with time zone,
CONSTRAINT check_191b5850ec CHECK ((char_length(file) <= 255))
CONSTRAINT check_191b5850ec CHECK ((char_length(file) <= 255)),
CONSTRAINT check_abeeb71caf CHECK ((file IS NOT NULL))
);
CREATE SEQUENCE public.ci_pipeline_artifacts_id_seq
......
......@@ -8,5 +8,10 @@ FactoryBot.define do
file_format { :raw }
file_store { Ci::PipelineArtifact::FILE_STORE_SUPPORTED.first }
size { 1.megabytes }
after(:build) do |artifact, _evaluator|
artifact.file = fixture_file_upload(
Rails.root.join('spec/fixtures/pipeline_artifacts/code_coverage.json'), 'application/json')
end
end
end
{
"files": {
"demo.rb": {
"1": 1,
"2": 1,
"3": 1
}
}
}
......@@ -18,6 +18,7 @@ RSpec.describe Ci::PipelineArtifact, type: :model do
it { is_expected.to validate_presence_of(:file_type) }
it { is_expected.to validate_presence_of(:file_format) }
it { is_expected.to validate_presence_of(:size) }
it { is_expected.to validate_presence_of(:file) }
it { is_expected.to validate_uniqueness_of(:file_type).scoped_to([:pipeline_id]).ignoring_case_sensitivity }
context 'when attributes are valid' do
......
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