module Carto module QueryRewriter def rewrite_query(query, old_username, new_user, renamed_tables) new_query = query new_query = rewrite_query_for_new_user(query, old_username, new_user) if old_username != new_user.database_schema new_query = rewrite_query_for_renamed_tables(new_query, renamed_tables) if renamed_tables.present? if test_query(new_user, new_query) new_query else CartoDB::Logger.debug(message: 'Did not rewrite query', old_query: query, new_query: new_query, old_username: old_username, user: new_user, renamed_tables: renamed_tables) query end end def qualify_query(query, table_name, username) query.gsub(/(?