0

I'm using rails 4.2 and trying to configure (in a already established application) the Audited Gem following this second database approach.

My config/database.yml file was as follows:

default: &default
  adapter: mysql2
  pool: 5
  timeout: 5000

development:
  <<: *default
  host:  <%= ENV["MYSQL_HOST"] %>
  username: <%= ENV["MYSQL_USER"] %>
  password: <%= ENV["MYSQL_PASSWORD"] %>
  database: <%= ENV["MYSQL_DATABASE"] %>

test:
  <<: *default
  host:  <%= ENV["MYSQL_HOST"] %>
  username: <%= ENV["MYSQL_USER"] %>
  password: <%= ENV["MYSQL_PASSWORD"] %>
  database: <%= ENV['TEST_ENV_DB'] %>

And I intend to make it work for another db, besides development, test or production. However the task rake db:create only creates my development and test database. Is this possible to accomplish in my rails version?

audition:
  <<: *default
  host:  <%= ENV["MYSQL_HOST"] %>
  username: <%= ENV["MYSQL_USER"] %>
  password: <%= ENV["MYSQL_PASSWORD"] %>
  database: <%= ENV["AUDITION_DATABASE"] %>

Note the new name for audition database

intmarinoreturn0
  • 300
  • 1
  • 16

3 Answers3

1

if you want to read/write to a seconds database in rails < 6

create a module

module AuditionConn
  def self.included(base)
    base.class_eval do
     if Rails.env == 'development'
       establish_connection "audition-development" # database.yml
      else
       establish_connection "audition-production" # database.yml
      end
    end
  end
end

then include it in any model you want to read/write from/to auditions database

class AuditionDBModel < ActiveRecord::Base
  include AuditionConn
end

migration for second database

def up
  AuditionDBModel.connection.create_table ... do |t|
    ...

  AuditionDBModel.connection.change_column ...
end
PGill
  • 3,373
  • 18
  • 21
  • Thanks, this worked for the migration. Do you have any idea in how to make the rake db:create/drop automate this step for the second db together? – intmarinoreturn0 Apr 29 '20 at 21:20
0

I think you want to create a new environment call audition, Right?!.

  • Clone an existing environment file for instance, config/environments/test.rb and rename it config/environments/audition.rb
  • Add a new configuration block in config/database.yml for your environment.
  • Update any other configuration file you might have under the config folder with your new environment, some gems need to config it.

  • Now you can start the server

rails server -e audition

Hany Moh.
  • 949
  • 1
  • 11
  • 11
  • No, I need to setup the audition database, to be used like the approach in the pointed link. Something to make it possible to be created with rake db:create alongside others databases. The same for rake db:migrate – intmarinoreturn0 Apr 24 '20 at 03:27
  • @intmarinoreturn0 try to use octopus gem https://github.com/thiagopradi/octopus – Hany Moh. Apr 24 '20 at 03:31
  • you can be using `replication` on octopus gem, all write queries will be sent to master and read queries to slaves – Hany Moh. Apr 24 '20 at 03:35
  • Does that mean It wont be possible to write on my second db? In this case the audition one. This is not the purpose of the audited gem – intmarinoreturn0 Apr 24 '20 at 03:38
0

I think this may help you:
create another model for audit:

class AuditModel < ActiveRecord::Base
  connects_to database: { writing: :audit_db, reading: :audit_db}
end

or

ActiveRecord::Base.establish_connection(
  adapter:  "mysql2",
  host:     "localhost",
  username: "myuser",
  password: "mypass",
  database: "somedatabase"
)

for details: https://guides.rubyonrails.org/active_record_multiple_databases.html https://edgeapi.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html