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

Handling multiple schemas #50

Open
hlascelles opened this issue Nov 5, 2020 · 5 comments
Open

Handling multiple schemas #50

hlascelles opened this issue Nov 5, 2020 · 5 comments

Comments

@hlascelles
Copy link

We'd like to check multiple schemas in our DB. At present we are using the gem with great success in specs thus:

output = ActiveRecordDoctor::Tasks::UnindexedForeignKeys.new.run

However we can't see an easy way to move between schemas. Is that supported? Thanks!

@gregnavis
Copy link
Owner

@hlascelles, thank you for opening the issue. I'd like to clarify the meaning of "schema" here - do you mean something like a PostgreSQL schema? Or are you saying you're using two schema.rb at the same time?

@hlascelles
Copy link
Author

This is for using with a Postgres DB with multiple (DB) schemas, but one schema.rb.

eg:

# We can test this:
public.customers
# But not this?
reporting.orders

@gregnavis
Copy link
Owner

@hlascelles, thanks for clarification. How did you set up Active Record for multiple schemas?

@hlascelles
Copy link
Author

Almost everything is "normal" (eg the database.yaml), and then for a few models we change the table that ActiveRecord uses to look them up:

  class NormalModel < ApplicationRecord
    # This one is in `public`
  end

  class OrdersModel < ApplicationRecord
    # This one is in `audit`

    self.abstract_class = true

    class << self
      def inherited(subclass)
        super
        subclass.table_name = "audit.#{subclass.table_name}"
      end
    end
  end

That's it.

@krainboltgreene
Copy link
Contributor

So we have a similar setup, except each model exists on two different schemas: Live and Sandbox.

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

3 participants