EmpaticoOrg / scenic-mysql_adapter

MySQL adapter for thoughtbot/scenic
MIT License
30 stars 14 forks source link

Wrong SQL created if a table shares its name with the database #3

Open adamors opened 6 years ago

adamors commented 6 years ago

If I have a database called students and inside that database I have a table also called students then

private def view_definition(name)
  execute("SHOW CREATE VIEW #{quote_table_name(name)}")
    .first[1]
    .sub(/\A.*#{quote_table_name(name)} AS /i, '')
    .gsub(/#{quote_table_name(@connectable.connection.current_database)}\./, '')
  end

will replace the name of the table, leading to errors such as Mysql2::Error: Column 'id' in field list is ambiguous.

It should replace only the very first match, not all of them.

cainlevy commented 6 years ago

Ah! Yep. I'll circle back to this when I'm available if someone hasn't already taken care of it.

adamors commented 6 years ago

I've added a small PR, please let me know if I should add anything else.

adamors commented 6 years ago

BTW this behaviour of adding the database name as a prefix is not consistent, because it's a bug: https://bugs.mysql.com/bug.php?id=85176. Fixed here: https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-3.html