Skip to content

Error when listing columns for model backed by different DB #149

Description

@owst

We have a couple of models that are backed by a different DB to the majority of models in our app. A rough example is something like this:

class MainDBThings < ApplicationRecord
end
class ExternalDBThings < ApplicationRecord
  establish_connection :external_db
end

It seems that a couple of places in active_record_doctor assume that all tables can be accessed from a connection to a single DB (ActiveRecord::Base.connection); when the "different DB" table names are accessed, a "table missing" error is raised because the connection used is to the wrong DB.

For example, this line:

uses connection, which raises an error along the lines of:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "ExternalDBThings" does not exist

A possible fix might be to use models.first.columns instead of connection.columns(table) so that the per-model connection is used?

Similarly, this line has the same issue:

connection.columns(model.table_name).each do |column|

I'm happy to have a stab at a PR for this, but would like some guidance as to any thoughts or suggested approaches before I start. I expect the fix will be to not use connection without reference to a particular model instance.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementAn enhancement other than a new detector.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions