Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query using {start_time} {end_time} variables in system with Postgres and SQL Server generates SQL Server strings for Postgres #391

Closed
HotFusionMan opened this issue Apr 14, 2022 · 3 comments

Comments

@HotFusionMan
Copy link

HotFusionMan commented Apr 14, 2022

We have both a Postgres database and a (legacy) SQL Server database that Blazer is able to query. When using the {start_time} and {end_time} variables in a query that select from one of the Postgres database's tables, those time variables are interpolated into the query as strings using SQL Server's quoting style that looks like N'the string contents' instead of 'the string contents' as Postgres uses.

Steps to reproduce:

  1. blazer.yml file contains:
data_sources:
  main:
    url: <%= BLAZER_POSTGRES_DATABASE_URL %>
  sql_server:
      url: <%= BLAZER_SQL_SERVER_DATABASE_URL %>

where BLAZER_POSTGRES_DATABASE_URL points to a Postgres database and BLAZER_SQL_SERVER_DATABASE_URL points at a SQL Server database.
2. In Blazer's GUI, create and save a query against the Postgres database that uses the {start_time} and {end_time} variables, e.g.,

SELECT COUNT(*) FROM some_table_in_Postgres WHERE created_at >= {start_time} AND created_at < {end_time}
  1. Run the query. Notice the error and the N'' quoting of the datetime values that were interpolated

I tried gem "blazer", github: "ankane/blazer" in the Gemfile and generated Gemfile.lock and saw no change in behavior.

In addition to omitting the leading "N", I've found that I had to cast the generated datetime strings in order for Postgres to successfully run the query, e.g.,

SELECT COUNT(*) FROM some_table_in_Postgres WHERE created_at >= '2022-03-16T00:00:00Z'::timestamp AND created_at < '2022-04-14T23:59:59Z'::timestamp
@HotFusionMan
Copy link
Author

I think I see why it behaves this way. The last executable line of Blazer::BaseController#process_vars calls ActiveRecord::Base.connection.quote(value), but that may not be the appropriate connection for the database the query is targeting.

@HotFusionMan
Copy link
Author

I've hacked that line to be the following, and it works, but I doubt it's production quality:

            if ["start_time", "end_time"].include?(var)
              statement.gsub!("{#{var}}", "#{Blazer.data_sources[data_source].__send__(:adapter_instance).connection_model.connection.quote(value)}::timestamp") if Blazer.data_sources[data_source].settings["url"].start_with?("postgres://")
            else
              statement.gsub!("{#{var}}", Blazer.data_sources[data_source].__send__(:adapter_instance).connection_model.connection.quote(value))
            end

@ankane ankane closed this as completed in f49fbfe Apr 20, 2022
@ankane
Copy link
Owner

ankane commented Apr 20, 2022

Hey @HotFusionMan, thanks for reporting! More info on the issue: #392

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants