Commit ce429417 authored by Ahmad Sherif's avatar Ahmad Sherif

Check for table/column existence before querying rows count

parent b20fb9f5
Pipeline #75370 passed with stage
in 2 minutes and 10 seconds
PATH
remote: .
specs:
gitlab-monitor (2.3.0)
gitlab-monitor (2.5.0)
connection_pool (~> 2.2.1)
pg (~> 0.18.4)
quantile (~> 0.2.0)
......
......@@ -13,7 +13,8 @@ module GitLab
joins: "INNER JOIN project_mirror_data ON project_mirror_data.project_id = projects.id",
where: "projects.mirror = 't' AND " \
"projects.import_status NOT IN ('scheduled', 'started') AND " \
"next_execution_timestamp <= NOW()"
"next_execution_timestamp <= NOW()",
check: "SELECT 1 FROM information_schema.tables WHERE table_name='project_mirror_data'"
},
soft_deleted_projects: { select: :projects, where: "pending_delete=true" },
orphaned_projects: {
......@@ -27,8 +28,10 @@ module GitLab
def run
results = Hash.new(0)
QUERIES.each do |key, query|
results[key] = execute(query)
QUERIES.each do |key, query_hash|
next if query_hash[:check] && !successful_check?(query_hash[:check])
results[key] = count_from_query_hash(query_hash)
end
results
......@@ -36,12 +39,26 @@ module GitLab
private
def count_from_query_hash(query_hash)
result = execute(construct_query(query_hash))
return 0 unless result
result[0]["count"]
end
def successful_check?(query)
result = execute("SELECT EXISTS (#{query})")
return unless result
result[0]["exists"] == "t"
end
def execute(query)
with_connection_pool do |conn|
conn.exec(construct_query(query))[0]["count"]
conn.exec(query)
end
rescue PG::UndefinedTable, PG::UndefinedColumn
0
nil
end
# Not private so I can test it without meta programming tricks
......
module GitLab
module Monitor
VERSION = "2.4.0".freeze
VERSION = "2.5.0".freeze
end
end
......@@ -11,7 +11,7 @@ describe GitLab::Monitor::Database::RowCountCollector do
end
it "executes the query" do
allow(collector).to receive(:execute).with(query[:project_1]).and_return(3)
allow(collector).to receive(:count_from_query_hash).with(query[:project_1]).and_return(3)
expect(collector.run).to eq(project_1: 3)
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