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.
parent a1215556
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment