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:
Douglas Barbosa Alexandre 2019-02-11 17:36:57 -02:00
parent a1215556ec
commit 9100ca188c
No known key found for this signature in database
GPG Key ID: F1E98EF6393565A0
2 changed files with 10 additions and 1 deletions

View File

@ -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

View File

@ -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