scenic icon indicating copy to clipboard operation
scenic copied to clipboard

Multiple databases, main DB is assumed even though it's specified in the model. Is there a better way?

Open epetre opened this issue 1 year ago • 1 comments

One of our models is connecting to a warehouse DB since it's for reporting and not to serve the app features. That model is based on a materialized view.

When refreshing, we currently can't use Scenic.database to connect to the database because it defaults to the main DB (it seems). It's not a major issue since we can simply use the connection from the model, but I am wondering if scenic supports this and I am missing something or if it doesn't.

  # In one of my models
  
  def self.refresh
     Scenic.database.refresh_materialized_view(table_name, 
                                               concurrently: false, 
                                               cascade: false)
  end

Instead we have to do this workaround which works fine, but I wonder if there is a better way?

def self.refresh
  connection.execute("REFRESH MATERIALIZED VIEW #{table_name};")
end

epetre avatar Dec 22 '23 17:12 epetre

This is probably something we need to look into as Scenic dates back to before multiple database support was properly added to Rails. What you have is fine, but I think in our own fix for this we'd want to keep the SQL abstracted from this point. I wonder if it would make more sense for us to inject the model's connection.

derekprior avatar Jan 17 '24 18:01 derekprior