Replace dots with an underscore when creating an alias for the CTE
When the Arel table to use as the alias contains a schema in your name, e.g., "gitlab_secondary"."namespaces" it produces an invalid query.
This commit is contained in:
parent
a1215556ec
commit
9100ca188c
2 changed files with 10 additions and 1 deletions
|
@ -48,7 +48,7 @@ module Gitlab
|
|||
#
|
||||
# alias_table - The Arel table to use as the alias.
|
||||
def alias_to(alias_table)
|
||||
Arel::Nodes::As.new(table, alias_table)
|
||||
Arel::Nodes::As.new(table, Arel::Table.new(alias_table.name.tr('.', '_')))
|
||||
end
|
||||
|
||||
# Applies the CTE to the given relation, returning a new one that will
|
||||
|
|
|
@ -31,6 +31,15 @@ describe Gitlab::SQL::RecursiveCTE, :postgresql do
|
|||
|
||||
expect(cte.alias_to(table).to_sql).to eq("#{source_name} AS #{alias_name}")
|
||||
end
|
||||
|
||||
it 'replaces dots with an underscore' do
|
||||
table = Arel::Table.new('gitlab.kittens')
|
||||
|
||||
source_name = ActiveRecord::Base.connection.quote_table_name(:cte_name)
|
||||
alias_name = ActiveRecord::Base.connection.quote_table_name(:gitlab_kittens)
|
||||
|
||||
expect(cte.alias_to(table).to_sql).to eq("#{source_name} AS #{alias_name}")
|
||||
end
|
||||
end
|
||||
|
||||
describe '#apply_to' do
|
||||
|
|
Loading…
Reference in a new issue