簡體   English   中英

將db轉儲到rails中的yml fixture的標准方法是什么?

[英]What is the standard way to dump db to yml fixtures in rails?

我已經看到一些插件和自定義rake任務將活動數據庫轉儲到燈具,但我不確定主流技術是什么。

基本上,我想要與rake相反:db:fixtures:load,以便我可以在部署時將基本數據庫信息(管理員用戶帳戶,一個)放入svn中。 我不想手動創建夾具,例如需要很長時間的樣本數據。

當我們部署時,我希望能夠運行

rake db:migrate
rake db:fixtures:load

並參加比賽。

在rails中執行此操作的最佳/首選方法是什么?

編輯:

所以似乎沒有標准的方法來執行db:fixtures:load的相反rake任務。

我不想使用遷移,因為我想要一個標准的方法來為我的所有項目執行此操作,並且我不喜歡在遷移中添加更多管理員帳戶的想法。 其次,我一直在重新思考使用燈具的想法。 我決定使用yaml_db,因為它使用rake任務:

rake db:data:dump
rake db:data:load

數據將在YAML文件中結束而不會破壞測試裝置(可能會有所不同,現在我更仔細地考慮這個問題)。 此外,如果像Heroku這樣的主要發行工具正在使用它,我不必擔心支持/長壽問題。

我想這最接近我會找到的“標准”。

感謝所有的好評。

這聽起來像你應該使用db / seeds.rb和相關的rake db:seed任務。 這些是專門為加載種子數據而設計的。 然后,您可以調用YamlDB來加載數據並調用db:data:dump_dir將所有fixture轉儲到臨時目錄,並根據需要將它們復制到種子數據目錄。

請注意,這不適用於轉儲燈具,因為YAML格式不同。 上面提到的ar_fixtures不適用於Rails 3,似乎不再受支持。 對於轉儲裝置,你可能想在lib / tasks / dump_fixtures.rake中嘗試這樣的東西:

namespace :db do
  namespace :fixtures do    
    desc 'Create YAML test fixtures from data in an existing database.  
    Defaults to development database.  Specify RAILS_ENV=production on command line to override.'
    task :dump => :environment do
      sql  = "SELECT * FROM %s"
      skip_tables = ["schema_migrations"]
      ActiveRecord::Base.establish_connection(Rails.env)
      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
        i = "000"
        File.open("#{Rails.root}/test/fixtures/#{table_name}.yml.new", 'w') do |file|
          data = ActiveRecord::Base.connection.select_all(sql % table_name)
          file.write data.inject({}) { |hash, record|
            hash["#{table_name}_#{i.succ!}"] = record
            hash
          }.to_yaml
        end
      end
    end
  end
end

我在這里找到了這個並為Rails3略微修改了它。

沒有標准的方法來做到這一點。 只有加載燈具的標准方式:

rake db:fixtures:load

但互聯網上有很多例子:

它不使用與db:fixtures完全相同的格式:load會期望,但ar_fixtures使轉儲和加載數據非常容易。

我認為,如果是標准的管理員信息,您最好將其放入遷移中。 理想情況下,燈具應僅用於測試。

對於其他任何人現在發現這一點,我稍微修改了@jpgeek的答案。 在ignore列表中包含schema_migration表,並按ID排序,因此我得到的輸出是table_name_001ID=1

namespace :db do
  namespace :fixtures do    
    desc 'Create YAML test fixtures from data in an existing database.  
    Defaults to development database.  Specify RAILS_ENV=production on command line to override.'
    task :dump => :environment do
      sql  = "SELECT * FROM %s ORDER BY ID"
      skip_tables = ["schema_info", "schema_migrations"]
      ActiveRecord::Base.establish_connection(Rails.env)
      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
        i = "000"
        File.open("#{Rails.root}/test/fixtures/#{table_name}.yml.new", 'w') do |file|
          data = ActiveRecord::Base.connection.select_all(sql % table_name)
          file.write data.inject({}) { |hash, record|
            hash["#{table_name}_#{i.succ!}"] = record
            hash
          }.to_yaml
        end
      end
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM