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

Support multiple databases #521

Conversation

michael-groble
Copy link

We would like to store the audits table in a separate database. With Rails 6 or ankane/multiverse, I can do that by setting up an abstract base record for all models I want to store in a secondary database:

class SecondaryRecord < ActiveRecord::Base
  self.abstract_class = true
  connects_to database: { writing: :secondary, reading: :secondary_replica }
end

and would then want to set up the audit model to live there:

class CustomAudit < SecondaryRecord
end

But since that CustomAudit record does not inherit from Audited::Audit, this won't really work.

This pull request creates an Audited::Auditable concern which lets me store audits in the secondary database like so:

class CustomAudit < SecondaryRecord
  include Audited::Auditable
end

@brand-it
Copy link

NotImplementedError:
  Audit is an abstract class and cannot be instantiated.
  # /Users/blareau/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-6.0.2/lib/active_record/inheritance.rb:52:in `new'

@michael-groble Did you get the error above and if so how did you work around the issue?

@michael-groble
Copy link
Author

I see failures in travis, @brand-it, but things run cleanly for me locally (on macos), e.g.:

bundle exec rake
bundle exec appraisal rails50 rake
bundle exec appraisal rails51 rake
bundle exec appraisal rails52 rake
bundle exec appraisal rails60 rake

(and the travis errors don't seem to match that abstract class error)

@LukeGuanY
Copy link

Hi @michael-groble, I also have the same need that writing the audit logs to the second database. But I am having trouble with how to tell the CustomAudit to write the audit logs to the second database. Could you please tell me how to point audited to the second DB in the CustomAudit file?

@michael-groble
Copy link
Author

@LukeGuanY , that really depends on how you connect to multiple databases in the first place. I give an example above of rails 6. You can also set up a base that connects to a specific entry in you database configuration. For example, say your database config has a separate set of configuration for "warehouse" type tables, e.g.

production:
  ...
  database: app_production
  warehouse:
    ...
    database: warehouse_production

then you can have a base record (rails 5 this time)

class WarehouseRecord < ApplicationRecord
  self.abstract_class = true
  establish_connection configurations[Rails.env]['warehouse'] if configurations[Rails.env]['warehouse']
end

class CustomAudit < WarehouseRecord
  include Audited::Auditable
end

@LukeGuanY
Copy link

@michael-groble Thanks for the reply. I really appreciate your method. I am sharing my configuration here so that people can also learn it.

I use the multiple database feature on Rail 6

Database.yml:

development:
  primary:
    database: the_existing_db
    ...
  audit:
    database: audit_db
    migrations_paths: db/audit_migrate #you need to create a new folder called "audit_migrate" under db directory

Make an audit initializer at config/initializers/audited.rb

# Make Audited send its data to a different database
Audited::Audit.class_eval do
  establish_connection :audit
end

@amkisko
Copy link

amkisko commented Jan 12, 2023

Probably related to this one #641

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

Successfully merging this pull request may close these issues.

4 participants