Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
7cc864ec718c
Commit
77abe9ac
authored
Oct 29, 2018
by
Andreas Brandl
Browse files
Fix rubocop offenses.
parent
a898e0821a08
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/gitlab/database/count.rb
View file @
7cc864ec
...
...
@@ -40,7 +40,7 @@ def self.approximate_counts(models, strategies: [TablesampleCountStrategy, Reltu
if
strategy
.
enabled?
models_with_missing_counts
=
models
-
counts_by_model
.
keys
re
turn
counts_by_model
if
models_with_missing_counts
.
empty?
b
re
ak
if
models_with_missing_counts
.
empty?
counts
=
strategy
.
new
(
models_with_missing_counts
).
count
...
...
@@ -139,6 +139,7 @@ def postgresql_estimate_query(table_names, check_statistics: true)
LEFT JOIN pg_stat_user_tables ON pg_class.relname = pg_stat_user_tables.relname
WHERE pg_class.relname IN (
#{
table_names
.
map
{
|
table
|
"'
#{
table
}
'"
}
.join(',')})
SQL
if
check_statistics
base_query
+
"AND (last_vacuum >
#{
time
}
OR last_autovacuum >
#{
time
}
OR last_analyze >
#{
time
}
OR last_autoanalyze >
#{
time
}
)"
else
...
...
@@ -177,6 +178,7 @@ def self.enabled?
end
private
def
perform_count
(
model
,
estimate
)
# If we estimate 0, we may not have statistics at all. Don't use them.
return
nil
unless
estimate
&&
estimate
>
0
...
...
@@ -193,10 +195,10 @@ def perform_count(model, estimate)
def
tablesample_count
(
model
,
estimate
)
portion
=
(
TABLESAMPLE_ROW_TARGET
.
to_f
/
estimate
).
round
(
4
)
inverse
=
1
/
portion
inverse
=
1
/
portion
query
=
<<~
SQL
SELECT (COUNT(*)*
#{
inverse
}
)::integer AS count
FROM
#{
model
.
table_name
}
TABLESAMPLE SYSTEM (
#{
portion
*
100
}
)
FROM
#{
model
.
table_name
}
TABLESAMPLE SYSTEM (
#{
portion
*
100
}
)
SQL
rows
=
ActiveRecord
::
Base
.
connection
.
select_all
(
query
)
...
...
spec/lib/gitlab/database/count_spec.rb
View file @
7cc864ec
...
...
@@ -10,7 +10,7 @@
context
'.approximate_counts'
do
context
'selecting strategies'
do
let
(
:strategies
)
{
[
double
(
's1'
,
:
enabled?
=>
true
),
double
(
's2'
,
:
enabled?
=>
false
)]
}
let
(
:strategies
)
{
[
double
(
's1'
,
enabled?
:
true
),
double
(
's2'
,
enabled?
:
false
)]
}
it
'uses only enabled strategies'
do
expect
(
strategies
[
0
]).
to
receive
(
:new
).
and_return
(
double
(
'strategy1'
,
count:
{}))
...
...
@@ -25,8 +25,8 @@
let
(
:strategies
)
do
[
double
(
's1'
,
:
enabled?
=>
true
,
new:
first_strategy
),
double
(
's2'
,
:
enabled?
=>
true
,
new:
second_strategy
)
double
(
's1'
,
enabled?
:
true
,
new:
first_strategy
),
double
(
's2'
,
enabled?
:
true
,
new:
second_strategy
)
]
end
...
...
@@ -62,7 +62,7 @@
describe
'#count'
do
it
'counts all models'
do
models
.
each
{
|
model
|
expect
(
model
).
to
receive
(
:count
).
and_call_original
}
expect
(
model
s
).
to
all
(
receive
(
:count
).
and_call_original
)
expect
(
subject
).
to
eq
({
Project
=>
3
,
Identity
=>
1
})
end
...
...
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