Commit 0a5a7f13 authored by Zamir Martins's avatar Zamir Martins Committed by Andreas Brandl

Add default value to vulnerabilities_allowed column

parent 4524225c
# frozen_string_literal: true
class AddDefaultProjectApprovalRulesVulnAllowed < ActiveRecord::Migration[6.1]
include Gitlab::Database::MigrationHelpers
disable_ddl_transaction!
DEFAULT_VALUE = 0
def up
change_column_default :approval_project_rules, :vulnerabilities_allowed, DEFAULT_VALUE
update_column_in_batches(:approval_project_rules, :vulnerabilities_allowed, DEFAULT_VALUE) do |table, query|
query.where(table[:vulnerabilities_allowed].eq(nil))
end
change_column_null :approval_project_rules, :vulnerabilities_allowed, false
end
def down
change_column_default :approval_project_rules, :vulnerabilities_allowed, nil
change_column_null :approval_project_rules, :vulnerabilities_allowed, true
end
end
8d247218468ad383d1a8a2dc67d5e7e67ddad2a33a38203a41e49c4c018adc7e
\ No newline at end of file
...@@ -9746,7 +9746,7 @@ CREATE TABLE approval_project_rules ( ...@@ -9746,7 +9746,7 @@ CREATE TABLE approval_project_rules (
name character varying NOT NULL, name character varying NOT NULL,
rule_type smallint DEFAULT 0 NOT NULL, rule_type smallint DEFAULT 0 NOT NULL,
scanners text[], scanners text[],
vulnerabilities_allowed smallint, vulnerabilities_allowed smallint DEFAULT 0 NOT NULL,
severity_levels text[] DEFAULT '{}'::text[] NOT NULL severity_levels text[] DEFAULT '{}'::text[] NOT NULL
); );
# frozen_string_literal: true
require 'spec_helper'
require_migration!
RSpec.describe AddDefaultProjectApprovalRulesVulnAllowed do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:namespace) { namespaces.create!(name: 'namespace', path: 'namespace') }
let(:project) { projects.create!(name: 'project', path: 'project', namespace_id: namespace.id) }
let(:approval_project_rules) { table(:approval_project_rules) }
it 'updates records when vulnerabilities_allowed is nil' do
records_to_migrate = 10
records_to_migrate.times do |i|
approval_project_rules.create!(name: "rule #{i}", project_id: project.id)
end
expect { migrate! }
.to change { approval_project_rules.where(vulnerabilities_allowed: nil).count }
.from(records_to_migrate)
.to(0)
end
it 'defaults vulnerabilities_allowed to 0' do
approval_project_rule = approval_project_rules.create!(name: "new rule", project_id: project.id)
expect(approval_project_rule.vulnerabilities_allowed).to be_nil
migrate!
expect(approval_project_rule.reload.vulnerabilities_allowed).to eq(0)
end
end
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