1

我在本地运行这个迁移文件并遇到问题,因为部分是 MySQL,因为我使用的是 Sqlite3,所以它不起作用。我想确保 SQL 只在生产中运行,而不是在开发中。我知道我需要用此评论来包装其中的一部分,以便我可以成功运行此迁移:

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
4

1 回答 1

2

在您的self.up方法中,您可以检查environment如下:

  if Rails.env.production? 
     #your mysql code which runs only in production env
  end
于 2013-02-09T03:52:07.087 回答