Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
13
Help us understand the problem. What is going on with this article?
@reikubonaga

Railsでデータベースを分けてmigrationを実行する方法(+modelとdatabase.yamlの書き方)

More than 5 years have passed since last update.

概要

  • 今回の要件は、master slaveを作るのでなく、テーブルを分けたい
  • 専用のrakeタスクを作成した
  • railsのissueでやり取りがあったけど、古いしバグもありいろいろ直すのが大変だった。。

rake

  • lib/tasks以下に配置
  • your_engineには好きな名前で置き換える
  • rake your_engine:db:migrateで実行
  • (establish_connectionをmigrationの中で実行すると、migrationのversion管理がうまく動かなくなる。)
database.rake
engine = "your_engine_name"

# Run custom task before original task
task 'db:migrate' => "#{engine}:db:migrate"
task 'db:schema:dump' => "#{engine}:db:schema:dump"
task 'db:schema:load' => "#{engine}:db:schema:load"

engine_namespace = namespace engine.to_sym do
  namespace :db do
    @engine = engine
    desc "Migrates the #{engine} database"
    task :migrate => :environment do
      p "#{engine} db migrate"
      with_engine_connection do
        ActiveRecord::Migrator.migrate("#{File.dirname(__FILE__)}/../../db/#{@engine}_migrate", ENV['VERSION'].try(:to_i))
      end
    end

    desc "Dump #{engine} schema"
    task :'schema:dump' => :environment do
      require 'active_record/schema_dumper'

      with_engine_connection do
        File.open(File.expand_path("../../../db/#{@engine}_schema.rb", __FILE__), 'w') do |file|
          ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, file
        end
      end
      engine_namespace['db:schema:dump'].reenable
    end

    desc "Load #{engine} schema"
    task :'schema:load' => :environment do
      with_engine_connection do
        load File.expand_path("../../../db/#{@engine}_schema.rb", __FILE__)
      end
    end

    # Hack to temporarily connect AR::Base to custom database engine.
    def with_engine_connection
      original = ActiveRecord::Base.remove_connection
      ActiveRecord::Base.establish_connection "#{@engine}_#{Rails.env}".to_sym
      yield
    ensure
      ActiveRecord::Base.establish_connection original
    end
  end
end

ついでにモデルとdatabase.ymlの設定

Model

class YourModel < ActiveRecord::Base
    establish_connection "your_engine_#{Rails.env}".to_sym
end

database.yml

your_engine_development:
  adapter: 
  database:
  encoding:
  username:
  password:
  host:

参考

13
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
reikubonaga
MC DigitalのCTO。Ex-Wantedly Visitの開発チームのリーダ。 チームのメンバーやインターンに伝えたいことをついでにqiitaにも書いていこうと思っています。 MC Digitalはエンジニア絶賛募集中です!
wantedly
「シゴトでココロオドル」ためのビジネスSNS「Wantedly」の開発・運営をしています。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
13
Help us understand the problem. What is going on with this article?