Using Rails Migration on different database than standard “production” or “development”

前端 未结 20 1431
借酒劲吻你
借酒劲吻你 2020-11-29 18:18

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

20条回答
  •  时光说笑
    2020-11-29 19:11

    For example, I have a study_history model:

    rails g model study_history lesson:references user:references history_type:references
    
    1. Define mysql section in database.yml
    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
    
    1. Modify the StudyHistory model, add establish_connect, it will connect your mysql database player_records above (I added this database in mysql server first):
    class StudyHistory < ApplicationRecord
      establish_connection :player_records
      
      belongs_to :lesson
      belongs_to :user
      belongs_to :history_type
    end
    
    1. Use connection in the migration file to create table:
    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]]
    => #
    

提交回复
热议问题