Rails迁移:Establishment_connection在错误的数据库中创建表 [英] Rails migration: establish_connection creates table in wrong database
问题描述
我想在数据库中创建一个新表,而不是在database.yml文件中定义的表.
I want to create a new table in a database other than the one defined in my database.yml file.
这是我的database.yml文件:
Here's my 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
我还有一个名为外围设备"的数据库.我想在该数据库内创建一个名为"retailer_to_domain"的表.
I have another database called "peripheral". I'd like to create a table inside that database called "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").
The migration file was generated by the command: rails generate model RetailerToDomain name:string domain:string
and then I added the def connection
method to override the default database ("main") from the database.yml configuration.
当我运行迁移(rake db:migrate
)时,将在main_development数据库中创建retailer_to_domains表.如何覆盖此默认值,以进行迁移以在所需的位置创建表?
When I run the migration (rake db:migrate
) the retailer_to_domains table gets created in the main_development database. How do I override this default to get the migration to create the table where I want it?
此外,我希望RetailerToDomain模型以类似于build_connection方法的方式访问此表,如下所示:
Also, I expect the RetailerToDomain model to access this table in a similar manner with the establish_connection method looking like this:
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
在此先感谢您的任何想法!
Thanks in advance for any ideas here!
推荐答案
观看此页面!
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
database.yaml
database.yaml
your_engine_development:
adapter:
database:
encoding:
username:
password:
host:
这篇关于Rails迁移:Establishment_connection在错误的数据库中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!