Rails的迁移:establish_connection在错误的数据库中创建表(Rails mig

2019-09-21 08:06发布

我想比我的database.yml文件中定义的另一个数据库中创建一个新表。

这是我的database.yml文件:

development:                                                                                                                                                                    
  adapter: mysql2                                                                                                                                                               
  encoding: utf8                                                                                                                                                                
  reconnect: false                                                                                                                                                              
  database: main_development                                                                                                                                                    
  pool: 5                                                                                                                                                                       
  username: root                                                                                                                                                                
  password:                                                                                                                                                                     
  socket: /var/run/mysqld/mysqld.sock                                                                                                                                           

test:                                                                                                                                                                           
  adapter: mysql2                                                                                                                                                               
  encoding: utf8                                                                                                                                                                
  reconnect: false                                                                                                                                                              
  database: main_test                                                                                                                                                    
  pool: 5                                                                                                                                                                       
  username: root                                                                                                                                                                
  password:                                                                                                                                                                     
  socket: /var/run/mysqld/mysqld.sock  

production:                                                                                                                                                                           
  adapter: mysql2                                                                                                                                                               
  encoding: utf8                                                                                                                                                                
  reconnect: false                                                                                                                                                              
  database: main_prod                                                                                                                                                    
  pool: 5                                                                                                                                                                       
  username: root                                                                                                                                                                
  password:                                                                                                                                                                     
  socket: /var/run/mysqld/mysqld.sock  

我有所谓的“外围”另一个数据库。 我想创建一个数据库名为“retailer_to_domain”里面的表。

这里是我的移民文件:

class CreateRetailerToDomains < ActiveRecord::Migration                                                                                                                         

      def connection                                                                                                                                                                
        ActiveRecord::Base.establish_connection(                                                                                                                                    
        :adapter => "mysql2",                                                                                                                                                       
        :encoding => "utf8",                                                                                                                                                        
        :reconnect => false,                                                                                                                                                        
        :database => "peripheral",                                                                                                                                                 
        :pool => 5,                                                                                                                                                                 
        :username => "root",                                                                                                                                                        
        :password => "",                                                                                                                                                            
        :socket => "/var/run/mysqld/mysqld.sock").connection                                                                                                                        
      end                                                                                                                                                                           

      def change                                                                                                                                                                    
        ActiveRecord::Base.connection.create_table :retailer_to_domains do |t|                                                                                            
          t.string :name                                                                                                                                                            
          t.string :domain                                                                                                                                                          

          t.timestamps                                                                                                                                                              
        end                                                                                                                                                                         
      end                                                                                                                                                                           
    end           

由命令生成的迁移文件: rails generate model RetailerToDomain name:string domain:string ,然后我加入def connection方法从database.yml中配置覆盖默认数据库(“主”)。

当我运行迁移( rake db:migrate )的retailer_to_domains表得到的main_development数据库中创建。 如何重写此默认获得迁移来创建表,我想呢?

此外,我预计RetailerToDomain模型来访问与establish_connection方法看起来像这样类似的方式该表:

class RetailerToDomain < ActiveRecord::Base                                                                                                                                     

 establish_connection(                                                                                                                                    
            :adapter => "mysql2",                                                                                                                                                       
            :encoding => "utf8",                                                                                                                                                        
            :reconnect => false,                                                                                                                                                        
            :database => "peripheral",                                                                                                                                                 
            :pool => 5,                                                                                                                                                                 
            :username => "root",                                                                                                                                                        
            :password => "",                                                                                                                                                            
            :socket => "/var/run/mysqld/mysqld.sock")

self.table=retailer_to_domain

validates_presence_of :name, :domain

end  

在此先感谢这里的任何想法!

Answer 1:

看这个页面!

https://github.com/rails/rails/issues/3497

创建耙文件

# Augment the main migration to migrate your engine, too.
task 'db:migrate', 'your_engine:db:migrate'

# Augment to dump/load the engine schema, too
task 'db:schema:dump', 'your_engine:db:schema:dump'
task 'db:schema:load', 'your_engine:db:schema:load'

namespace :your_engine do
  namespace :db do
    desc 'Migrates the your_engine database'
    task :migrate => :environment do
      p "your_engine db migrate"
      with_engine_connection do
        ActiveRecord::Migrator.migrate("#{File.dirname(__FILE__)}/../../db/migrate/your_engine", ENV['VERSION'].try(:to_i))
      end
      Rake::Task['your_engine:db:schema:dump'].invoke
    end

    task :'schema:dump' => :environment do
      require 'active_record/schema_dumper'

      with_engine_connection do
        File.open(File.expand_path('../../../db/your_engine_schema.rb', __FILE__), 'w') do |file|
          ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, file
        end
      end
    end

    task :'schema:load' => :environment do
      with_engine_connection do
        load File.expand_path('../../../db/your_engine_schema.rb', __FILE__)
      end
    end
  end
end

# Hack to temporarily connect AR::Base to your_engine.
def with_engine_connection
  original = ActiveRecord::Base.remove_connection
  ActiveRecord::Base.establish_connection "your_engine_#{Rails.env}".to_sym
  yield
ensure
  ActiveRecord::Base.establish_connection original
end

模型

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

database.yaml

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


Answer 2:

        Add another database connection seeting to database.yml file.

        database.yml file

            development:                                                                                                                                                                    
              adapter: mysql2                                                                                                                                                               
              encoding: utf8                                                                                                                                                                
              reconnect: false                                                                                                                                                              
              database: main_development                                                                                                                                                    
              pool: 5                                                                                                                                                                       
              username: root                                                                                                                                                                
              password:                                                                                                                                                                     
              socket: /var/run/mysqld/mysqld.sock                                                                                                                                           

            test:                                                                                                                                                                           
              adapter: mysql2                                                                                                                                                               
              encoding: utf8                                                                                                                                                                
              reconnect: false                                                                                                                                                              
              database: main_test                                                                                                                                                    
              pool: 5                                                                                                                                                                       
              username: root                                                                                                                                                                
              password:                                                                                                                                                                     
              socket: /var/run/mysqld/mysqld.sock  

            production:                                                                                                                                                                           
              adapter: mysql2                                                                                                                                                               
              encoding: utf8                                                                                                                                                                
              reconnect: false                                                                                                                                                              
              database: main_prod                                                                                                                                                    
              pool: 5                                                                                                                                                                       
              username: root                                                                                                                                                                
              password:                                                                                                                                                                     
              socket: /var/run/mysqld/mysqld.sock 

    another_database:
                adapter: "mysql2",                                                                                                                                                       
                encoding: "utf8",                                                                                                                                                        
                reconnect: false,                                                                                                                                                        
                database: "peripheral",                                                                                                                                                 
                pool: 5,                                                                                                                                                                 
                username: "root",                                                                                                                                                        
                password: "",                                                                                                                                                            
                socket: "/var/run/mysqld/mysqld.sock"



Now In Migration file specify:
ActiveRecord::Base.establish_connection(:another_database)


class CreateRetailerToDomains < ActiveRecord::Migration
ActiveRecord::Base.establish_connection(:another_database)
 def change                                                                                                                                                                    
     create_table :retailer_to_domains do |t|                                                                                            
          t.string :name                                                                                                                                                            
          t.string :domain                                                                                                                                                          

          t.timestamps                                                                                                                                                              
        end                                                                                                                                                                         
      end                                                                                                                                                                           
    end  



 It will get create table inside that database only.


Model

class RetailerToDomain < ActiveRecord::Base
  ActiveRecord::Base.establish_connection(:another_database)
end


**To check the active database connection**
ActiveRecord::Base.connection.current_database


Answer 3:

在你config/database.yml

new_connect_development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: new_database_development
  pool: 5
  username: root
  password:
  socket: /tmp/mysql.sock

而对于所有其他环境中相同的块。 然后添加到您的迁移(例如钢轨2.X-3.0,为Rails 3.1删除self ):

  def self.connection
    ActiveRecord::Base.establish_connection("new_connect_#{RAILS_ENV}").connection
  end

并添加以下行到你的模型,在新的数据库表中的相应:

class ModelName < ActiveRecord::Base
  establish_connection "new_connect_#{RAILS_ENV}"
end


文章来源: Rails migration: establish_connection creates table in wrong database