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
c1a951cf
Commit
c1a951cf
authored
Mar 12, 2021
by
Mikolaj Wawrzyniak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Joins relation parsers
parent
f933d954
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
114 additions
and
0 deletions
+114
-0
lib/gitlab/usage/metrics/names_suggestions/relation_parsers/joins.rb
...usage/metrics/names_suggestions/relation_parsers/joins.rb
+74
-0
spec/lib/gitlab/usage/metrics/names_suggestions/relation_parsers/joins_spec.rb
.../metrics/names_suggestions/relation_parsers/joins_spec.rb
+40
-0
No files found.
lib/gitlab/usage/metrics/names_suggestions/relation_parsers/joins.rb
0 → 100644
View file @
c1a951cf
# frozen_string_literal: true
module
Gitlab
module
Usage
module
Metrics
module
NamesSuggestions
module
RelationParsers
class
Joins
<
::
Arel
::
Visitors
::
PostgreSQL
def
accept
(
object
)
object
.
source
.
right
.
map
do
|
join
|
visit
(
join
,
collector
)
end
end
private
# rubocop:disable Naming/MethodName
def
visit_Arel_Nodes_StringJoin
(
object
,
collector
)
result
=
visit
(
object
.
left
,
collector
)
source
,
constraints
=
result
.
value
.
split
(
'ON'
)
{
source:
source
.
split
(
'JOIN'
).
last
&
.
strip
,
constraints:
constraints
&
.
strip
}.
compact
end
def
visit_Arel_Nodes_FullOuterJoin
(
object
,
_
)
parse_join
(
object
)
end
def
visit_Arel_Nodes_OuterJoin
(
object
,
_
)
parse_join
(
object
)
end
def
visit_Arel_Nodes_RightOuterJoin
(
object
,
_
)
parse_join
(
object
)
end
def
visit_Arel_Nodes_InnerJoin
(
object
,
_
)
{
source:
visit
(
object
.
left
,
collector
).
value
,
constraints:
object
.
right
?
visit
(
object
.
right
.
expr
,
collector
).
value
:
nil
}.
compact
end
# rubocop:enable Naming/MethodName
def
parse_join
(
object
)
{
source:
visit
(
object
.
left
,
collector
).
value
,
constraints:
visit
(
object
.
right
.
expr
,
collector
).
value
}
end
def
quote
(
value
)
"
#{
value
}
"
end
def
quote_table_name
(
name
)
"
#{
name
}
"
end
def
quote_column_name
(
name
)
"
#{
name
}
"
end
def
collector
Arel
::
Collectors
::
SubstituteBinds
.
new
(
@connection
,
Arel
::
Collectors
::
SQLString
.
new
)
end
end
end
end
end
end
end
spec/lib/gitlab/usage/metrics/names_suggestions/relation_parsers/joins_spec.rb
0 → 100644
View file @
c1a951cf
# frozen_string_literal: true
require
'spec_helper'
RSpec
.
describe
Gitlab
::
Usage
::
Metrics
::
NamesSuggestions
::
RelationParsers
::
Joins
do
describe
'#accept'
do
let
(
:collector
)
{
Arel
::
Collectors
::
SubstituteBinds
.
new
(
ActiveRecord
::
Base
.
connection
,
Arel
::
Collectors
::
SQLString
.
new
)
}
context
'with join added via string'
do
it
'collects join parts'
do
arel
=
Issue
.
joins
(
'LEFT JOIN projects ON projects.id = issue.project_id'
)
arel
=
arel
.
arel
result
=
described_class
.
new
(
ApplicationRecord
.
connection
).
accept
(
arel
)
expect
(
result
).
to
match_array
[{
source:
"projects"
,
constraints:
"projects.id = issue.project_id"
}]
end
end
context
'with join added via arel node'
do
it
'collects join parts'
do
source_table
=
Arel
::
Table
.
new
(
'records'
)
joined_table
=
Arel
::
Table
.
new
(
'joins'
)
second_level_joined_table
=
Arel
::
Table
.
new
(
'second_level_joins'
)
arel
=
source_table
.
from
.
project
(
source_table
[
'id'
].
count
)
.
join
(
joined_table
,
Arel
::
Nodes
::
OuterJoin
)
.
on
(
source_table
[
:id
].
eq
(
joined_table
[
:records_id
]))
.
join
(
second_level_joined_table
,
Arel
::
Nodes
::
OuterJoin
)
.
on
(
joined_table
[
:id
].
eq
(
second_level_joined_table
[
:joins_id
]))
result
=
described_class
.
new
(
ApplicationRecord
.
connection
).
accept
(
arel
)
expect
(
result
).
to
match_array
[{
source:
"joins"
,
constraints:
"records.id = joins.records_id"
},
{
source:
"second_level_joins"
,
constraints:
"joins.id = second_level_joins.joins_id"
}]
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