3

我想在我的 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 配置中覆盖默认数据库(“main”)的方法。

当我运行迁移 ( rake db:migrate) 时,在 main_development 数据库中创建了retailer_to_domains 表。如何覆盖此默认值以使迁移在我想要的位置创建表?

另外,我希望 RetailerToDomain 模型以类似的方式访问此表,建立连接方法如下所示:

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  

在此先感谢您的任何想法!

4

3 回答 3

4

看这个页面!

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

创建 rake 文件

# 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

数据库.yaml

your_engine_development:
  adapter: 
  database:
  encoding:
  username:
  password:
  host:
于 2014-03-24T12:49:13.340 回答
0
        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
于 2012-09-27T06:10:21.093 回答
0

在你的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

和所有其他环境相同的块。然后添加到您的迁移(rails 2.x-3.0 的示例,rails 3.1 remove 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
于 2013-02-28T15:49:18.147 回答