I have a rails project running that defines the standard production:, :development and :test DB-connections in config/database.yml
In addition I have a quiz_developm
You could use this version, which also supports rake db:rollback
:
class ChangeQuiz < ActiveRecord::Migration
def connection
ActiveRecord::Base.establish_connection("quiz_#{Rails.env}").connection
end
def reset_connection
ActiveRecord::Base.establish_connection(Rails.env)
end
def up
# make changes
reset_connection
end
def self.down
# reverse changes
reset_connection
end
end
You should define the other databases/environments in /config/environments.
After that you can use the following command to migrate that specific environment.
rake db:migrate RAILS_ENV=customenvironment
class Article < ActiveRecord::Base
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "root",
:database => "test"
)
end
And:
class Artic < Aritcle
self.table_name = 'test'
def self.get_test_name()
query = "select name from testing"
tst = connection.select_all(query) #select_all is important!
tst[0].fetch('name')
end
end
You can call Artic.get_test_name in order to execute.
Have you tried using quiz_development as a RAILS_ENV (instead of trying to get it to use "quiz_#{RAILS_ENV}"
)?
RAILS_ENV=quiz_development rake db:migrate
I got this to work with the following code.
class AddInProgressToRefHighLevelStatuses < ActiveRecord::Migration
def connection
@connection = ActiveRecord::Base.establish_connection("sdmstore_#{Rails.env}").connection
end
def change
add_column :ref_high_level_statuses, :is_in_progress, :boolean, :default => true
@connection = ActiveRecord::Base.establish_connection("#{Rails.env}").connection
end
end
It was necessary to set the connection back to get it to write the migration to the schema_migrations table so rake would not try to re-run the migration the next time. This assumes that you want the schema_migrations table in the default database configuration to keep track of the migrations checked into version control for the corresponding project.
I was unable to get the down migration to work.
For example, I have a study_history model:
rails g model study_history lesson:references user:references history_type:references
player_records:
adapter: mysql2
encoding: utf8
host: 1.2.3.4
username: root
password:
timeout: 5000
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 20 } %>
database: player_records
class StudyHistory < ApplicationRecord
establish_connection :player_records
belongs_to :lesson
belongs_to :user
belongs_to :history_type
end
class CreateStudyHistories < ActiveRecord::Migration[6.0]
def change
StudyHistory.connection.create_table :study_histories do |t|
t.references :lesson, null: false
t.references :user, null: false
t.references :history_type, null: false
t.timestamps
end
end
end
now, you can run
rails db:migrate
That's it, I tested in rails 6, it works like a charm, you can get your data from different databases combined( local sqlite3 and remote mysql).
irb(main):029:0> StudyHistory.first.lesson
(42.5ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_Z
ERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
StudyHistory Load (30.0ms) SELECT `study_histories`.* FROM `study_histories` ORDER BY `study_histories`.`id` ASC LIMIT 1
(0.0ms)
SELECT sqlite_version(*)
Lesson Load (0.1ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> #<Lesson id: 1, title: "people", cn: nil, description: nil, version: nil, course_id: 1, created_at: "2020-03-01 23:57
:02", updated_at: "2020-05-08 09:57:40", level: "aa", ready: false, pictureurl: "/pictures/kiss^boy and girl^boy^girl.jp
g">