开发者

ActiveRecord::Migration - Referencing a table in another schema

开发者 https://www.devze.com 2023-04-08 15:55 出处:网络
I\'m using Ruby and PostgreSQL and have created 3 distinct DB schemas: billing (for billing related data), customer (for customer related data) and edocs (for electronic documents related data).

I'm using Ruby and PostgreSQL and have created 3 distinct DB schemas: billing (for billing related data), customer (for customer related data) and edocs (for electronic documents related data). I'm not using Rails so I have a stand-alone migration code like this:

#migrate.rb

if ARGV[0] =~ /VERSION=\d+/
  version = ARGV[0].split('=')[1].to_i
else
  version = nil
end

ActiveRecord::Base.default_timezone           = :utc
@logger                                       = Logger.new $stderr
ActiveRecord::Base.logger                     = @logger
ActiveSupport::LogSubscriber.colorize_logging = false
@config                                       =     YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))

ActiveRecord::Base.establish_connection(@config["edocs"])
ActiveRecord:开发者_如何学运维:Migrator.migrate(".", version)

I have already realized that I probably have to create a different directories to contain the migration for the different schemas, and changing connection info for each migrate.rb.

But I'm not sure how I'm going to make a table reference another table that is in another schema. For example:

class CreateBillingEventsTable < ActiveRecord::Migration

 def self.up
   create_table :billing_events do |t|
     t.references :customer, :null => false
     t.references :service_type, :null => false
     t.timestamps
   end
   change_table :billing_events do |t|
     t.index [:customer_id, :created_at]
   end
 end

 def self.down
   remove_index :billing_events, :column => [:customer_id, :created_at]
   drop_table :billing_events
 end

end

In the above example, "customer" and "billing_events" are in different schemas. How can I code that?

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号