Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
f621dca9
Commit
f621dca9
authored
Feb 22, 2021
by
Alper Akgun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hardened "add" arithmetic for usage data
Deals with negative numbers and SQL exports
parent
ce664d2f
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
43 additions
and
0 deletions
+43
-0
changelogs/unreleased/322099-sql-definition-for-template_repositories.yml
...eased/322099-sql-definition-for-template_repositories.yml
+5
-0
lib/gitlab/usage_data_queries.rb
lib/gitlab/usage_data_queries.rb
+4
-0
lib/gitlab/utils/usage_data.rb
lib/gitlab/utils/usage_data.rb
+8
-0
spec/lib/gitlab/usage_data_queries_spec.rb
spec/lib/gitlab/usage_data_queries_spec.rb
+8
-0
spec/lib/gitlab/utils/usage_data_spec.rb
spec/lib/gitlab/utils/usage_data_spec.rb
+18
-0
No files found.
changelogs/unreleased/322099-sql-definition-for-template_repositories.yml
0 → 100644
View file @
f621dca9
---
title
:
Hardened "add" arithmetic for usage data
merge_request
:
54794
author
:
type
:
performance
lib/gitlab/usage_data_queries.rb
View file @
f621dca9
...
...
@@ -32,6 +32,10 @@ module Gitlab
raw_sql
(
relation
,
column
,
:distinct
)
end
def
add
(
*
args
)
'SELECT '
+
args
.
map
{
|
arg
|
"(
#{
arg
}
)"
}.
join
(
' + '
)
end
private
def
raw_sql
(
relation
,
column
,
distinct
=
nil
)
...
...
lib/gitlab/utils/usage_data.rb
View file @
f621dca9
...
...
@@ -86,6 +86,14 @@ module Gitlab
FALLBACK
end
def
add
(
*
args
)
return
-
1
if
args
.
any?
(
&
:negative?
)
args
.
sum
rescue
StandardError
FALLBACK
end
def
alt_usage_data
(
value
=
nil
,
fallback:
FALLBACK
,
&
block
)
if
block_given?
yield
...
...
spec/lib/gitlab/usage_data_queries_spec.rb
View file @
f621dca9
...
...
@@ -38,4 +38,12 @@ RSpec.describe Gitlab::UsageDataQueries do
expect
(
described_class
.
sum
(
Issue
,
:weight
)).
to
eq
(
'SELECT SUM("issues"."weight") FROM "issues"'
)
end
end
describe
'.add'
do
it
'returns the combined raw SQL with an inner query'
do
expect
(
described_class
.
add
(
'SELECT COUNT("users"."id") FROM "users"'
,
'SELECT COUNT("issues"."id") FROM "issues"'
))
.
to
eq
(
'SELECT (SELECT COUNT("users"."id") FROM "users") + (SELECT COUNT("issues"."id") FROM "issues")'
)
end
end
end
spec/lib/gitlab/utils/usage_data_spec.rb
View file @
f621dca9
...
...
@@ -183,6 +183,24 @@ RSpec.describe Gitlab::Utils::UsageData do
end
end
describe
'#add'
do
it
'adds given values'
do
expect
(
described_class
.
add
(
1
,
3
)).
to
eq
(
4
)
end
it
'adds given values'
do
expect
(
described_class
.
add
).
to
eq
(
0
)
end
it
'returns the fallback value when adding fails'
do
expect
(
described_class
.
add
(
nil
,
3
)).
to
eq
(
-
1
)
end
it
'returns the fallback value one of the arguments is negative'
do
expect
(
described_class
.
add
(
-
1
,
1
)).
to
eq
(
-
1
)
end
end
describe
'#alt_usage_data'
do
it
'returns the fallback when it gets an error'
do
expect
(
described_class
.
alt_usage_data
{
raise
StandardError
}
).
to
eq
(
-
1
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment