Commit 32f09dc4 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Filter non-existing Project IDs while populating `has_vulnerabilities`

parent 2e20097d
......@@ -9,10 +9,12 @@ module Gitlab
self.table_name = 'project_settings'
UPSERT_SQL = <<~SQL
WITH upsert_data (project_id, has_vulnerabilities, created_at, updated_at) AS (
SELECT projects.id, true, current_timestamp, current_timestamp FROM projects WHERE projects.id IN (%{project_ids})
)
INSERT INTO project_settings
(project_id, has_vulnerabilities, created_at, updated_at)
VALUES
%{values}
(SELECT * FROM upsert_data)
ON CONFLICT (project_id)
DO UPDATE SET
has_vulnerabilities = true,
......@@ -20,10 +22,7 @@ module Gitlab
SQL
def self.upsert_for(project_ids)
timestamp = connection.quote(Time.now)
values = project_ids.map { |project_id| "(#{project_id}, true, #{timestamp}, #{timestamp})" }.join(', ')
connection.execute(UPSERT_SQL % { values: values })
connection.execute(UPSERT_SQL % { project_ids: project_ids.join(', ') })
end
end
......
......@@ -39,6 +39,13 @@ RSpec.describe Gitlab::BackgroundMigration::PopulateHasVulnerabilities, schema:
count: 2)
end
context 'when non-existing project_id is given' do
it 'populates only for the existing projects' do
expect { subject.perform(project_1.id, 0, project_3.id) }.to change { project_settings.count }.from(1).to(2)
.and change { project_settings.where(has_vulnerabilities: true).count }.from(0).to(2)
end
end
context 'when an error happens' do
before do
allow(described_class::ProjectSetting).to receive(:upsert_for).and_raise('foo')
......
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