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
01c7cb90
Commit
01c7cb90
authored
Oct 28, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor specs to separate concerns.
parent
c5fb4682
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
27 deletions
+64
-27
lib/gitlab/database/count.rb
lib/gitlab/database/count.rb
+1
-9
spec/lib/gitlab/database/count_spec.rb
spec/lib/gitlab/database/count_spec.rb
+63
-18
No files found.
lib/gitlab/database/count.rb
View file @
01c7cb90
...
@@ -25,7 +25,7 @@ module Gitlab
...
@@ -25,7 +25,7 @@ module Gitlab
#
#
# @param [Array]
# @param [Array]
# @return [Hash] of Model -> count mapping
# @return [Hash] of Model -> count mapping
def
self
.
approximate_counts
(
models
,
strategies
=
[
ReltuplesCountStrategy
,
ExactCountStrategy
])
def
self
.
approximate_counts
(
models
,
strategies
:
[
ReltuplesCountStrategy
,
ExactCountStrategy
])
strategies
.
each_with_object
({})
do
|
strategy
,
counts_by_model
|
strategies
.
each_with_object
({})
do
|
strategy
,
counts_by_model
|
if
strategy
.
enabled?
if
strategy
.
enabled?
models_with_missing_counts
=
models
-
counts_by_model
.
keys
models_with_missing_counts
=
models
-
counts_by_model
.
keys
...
@@ -38,14 +38,6 @@ module Gitlab
...
@@ -38,14 +38,6 @@ module Gitlab
end
end
end
end
# Returns a hash of the table names that have recently updated tuples.
#
# @param [Array] models to count
# @returns [Hash] Table name to count mapping (e.g. { 'projects' => 5, 'users' => 100 })
def
self
.
reltuples_from_recently_updated
(
models
)
ReltuplesCountStrategy
.
new
(
models
).
count
end
class
ExactCountStrategy
class
ExactCountStrategy
attr_reader
:models
attr_reader
:models
def
initialize
(
models
)
def
initialize
(
models
)
...
...
spec/lib/gitlab/database/count_spec.rb
View file @
01c7cb90
...
@@ -7,28 +7,29 @@ describe Gitlab::Database::Count do
...
@@ -7,28 +7,29 @@ describe Gitlab::Database::Count do
end
end
let
(
:models
)
{
[
Project
,
Identity
]
}
let
(
:models
)
{
[
Project
,
Identity
]
}
let
(
:reltuples_strategy
)
{
double
(
'reltuples_strategy'
,
count:
{})
}
let
(
:exact_strategy
)
{
double
(
'exact_strategy'
,
count:
{})
}
describe
'.approximate_counts'
do
before
do
context
'with MySQL'
do
allow
(
Gitlab
::
Database
::
Count
::
ReltuplesCountStrategy
).
to
receive
(
:new
).
with
(
models
).
and_return
(
reltuples_strategy
)
context
'when reltuples have not been updated'
do
end
it
'counts all models the normal way'
do
expect
(
Gitlab
::
Database
).
to
receive
(
:postgresql?
).
and_return
(
false
)
expect
(
Project
).
to
receive
(
:count
).
and_call_original
context
'.approximate_counts'
do
expect
(
Identity
).
to
receive
(
:count
).
and_call_original
context
'selecting strategies'
do
let
(
:strategies
)
{
[
double
(
's1'
,
:enabled?
=>
true
),
double
(
's2'
,
:enabled?
=>
false
)]
}
expect
(
described_class
.
approximate_counts
(
models
)).
to
eq
({
Project
=>
3
,
Identity
=>
1
})
it
'uses only enabled strategies'
do
end
expect
(
strategies
[
0
]).
to
receive
(
:new
).
and_return
(
double
(
'strategy1'
,
count:
{}))
expect
(
strategies
[
1
]).
not_to
receive
(
:new
)
described_class
.
approximate_counts
(
models
,
strategies:
strategies
)
end
end
end
end
context
'with PostgreSQL'
,
:postgresql
do
context
'fallbacks'
do
let
(
:reltuples_strategy
)
{
double
(
'reltuples_strategy'
,
count:
{})
}
before
do
allow
(
Gitlab
::
Database
::
Count
::
ReltuplesCountStrategy
).
to
receive
(
:new
).
with
(
models
).
and_return
(
reltuples_strategy
)
end
end
context
'with PostgreSQL'
,
:postgresql
do
describe
'when reltuples have not been updated'
do
describe
'when reltuples have not been updated'
do
it
'counts all models the normal way'
do
it
'counts all models the normal way'
do
expect
(
Project
).
to
receive
(
:count
).
and_call_original
expect
(
Project
).
to
receive
(
:count
).
and_call_original
...
@@ -77,12 +78,56 @@ describe Gitlab::Database::Count do
...
@@ -77,12 +78,56 @@ describe Gitlab::Database::Count do
describe
Gitlab
::
Database
::
Count
::
ExactCountStrategy
do
describe
Gitlab
::
Database
::
Count
::
ExactCountStrategy
do
subject
{
described_class
.
new
(
models
).
count
}
subject
{
described_class
.
new
(
models
).
count
}
describe
'#count'
do
it
'counts all models'
do
it
'counts all models'
do
models
.
each
{
|
model
|
expect
(
model
).
to
receive
(
:count
).
and_call_original
}
models
.
each
{
|
model
|
expect
(
model
).
to
receive
(
:count
).
and_call_original
}
expect
(
subject
).
to
eq
({
Project
=>
3
,
Identity
=>
1
})
expect
(
subject
).
to
eq
({
Project
=>
3
,
Identity
=>
1
})
end
end
end
end
describe
'.enabled?'
do
it
'is enabled for PostgreSQL'
do
allow
(
Gitlab
::
Database
).
to
receive
(
:postgresql?
).
and_return
(
true
)
expect
(
described_class
.
enabled?
).
to
be_truthy
end
it
'is enabled for MySQL'
do
allow
(
Gitlab
::
Database
).
to
receive
(
:postgresql?
).
and_return
(
false
)
expect
(
described_class
.
enabled?
).
to
be_truthy
end
end
end
describe
Gitlab
::
Database
::
Count
::
ReltuplesCountStrategy
do
subject
{
described_class
.
new
(
models
).
count
}
describe
'#count'
do
context
'when reltuples is not up to date'
do
it
'returns an empty hash'
do
models
.
each
{
|
model
|
expect
(
model
).
not_to
receive
(
:count
)
}
expect
(
subject
).
to
eq
({})
end
end
end
describe
'.enabled?'
do
it
'is enabled for PostgreSQL'
do
allow
(
Gitlab
::
Database
).
to
receive
(
:postgresql?
).
and_return
(
true
)
expect
(
described_class
.
enabled?
).
to
be_truthy
end
it
'is disabled for MySQL'
do
allow
(
Gitlab
::
Database
).
to
receive
(
:postgresql?
).
and_return
(
false
)
expect
(
described_class
.
enabled?
).
to
be_falsey
end
end
end
end
end
end
end
end
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