2013-02-09 63 views
1

我在本地運行這個遷移文件,並且遇到了問題,因爲部分是因爲我使用Sqlite3而無法運行的MySQL。我想確保SQL僅在生產中運行,而不是在開發中運行。我明白我需要包裝的這部分與此評論這樣我就可以成功運行此遷移:Rails將遷移文件註釋掉以便開發?

unless Rails.env == "development". 

我如何添加這下面的腳本?

class Places < ActiveRecord::Migration 
     def self.up 
     create_table :countries do |t| 
      t.string :name, :limit => 50, :null => false 
      t.string :fips104, :limit => 2, :null => false 
      t.string :iso2, :limit => 2, :null => false 
      t.string :iso3, :limit => 3, :null => false 
      t.string :ison, :limit => 4, :null => false 
      t.string :internet, :limit => 2, :null => false 
      t.string :capital, :limit => 25 
      t.string :map_reference, :limit => 50 
      t.string :nationality_singular, :limit => 35 
      t.string :nationaiity_plural, :limit => 35 
      t.string :currency, :limit => 30 
      t.string :currency_code, :limit => 3 
      t.integer :population 
      t.string :title, :limit => 50 
      t.string :comment, :limit => 255 

      t.timestamps 
     end 

     create_table :regions do |t| 
      t.references :country, :null => false 
      t.string :name, :limit => 45, :null => false 
      t.string :code, :limit => 8, :null => false 
      t.string :adm1code, :limit => 4, :null => false 

      t.timestamps 
     end 

     create_table :cities do |t| 
      t.references :country, :null => false 
      t.references :region, :null => false 
      t.string :name, :limit => 45, :null => false 
      t.float :latitude, :null => false 
      t.float :longitude, :null => false 
      t.string :timezone, :limit => 10, :null => false 
      t.integer :dma_id 
      t.string :county, :limit => 25 
      t.string :code, :limit => 4 

      t.timestamps 
     end 
     add_index :cities, :name 


     execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Countries.txt' INTO TABLE countries 
      FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;" 

     execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Regions.txt' INTO TABLE regions 
      FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;" 

     execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Cities.txt' INTO TABLE cities 
      FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;" 


     end 

     def self.down 
     drop_table :countries 
     drop_table :regions 
     drop_table :cities 
     end 
    end 
+0

你爲什麼要這麼做? – codeit 2013-02-09 03:49:28

+0

該腳本正在使用mysql語法,它在開發中不適用於Sqlite3。因此,我想將進口腳本註釋掉以便直接將csv文件導入數據庫。 – 2013-02-09 03:51:07

+0

哪個db是你在測試中使用?你需要測試嗎? – codeit 2013-02-09 03:53:19

回答

2

在你self.up方法可以檢查environment象下面這樣:

if Rails.env.production? 
    #your mysql code which runs only in production env 
    end