cgy*_*per 19 rake ruby-on-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这样的主要发行工具正在使用它,我不必担心支持/长寿问题.
我想这最接近我会找到的"标准".
感谢所有的好评.
jpg*_*eek 14
这听起来像你应该使用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
Bvu*_*Ic7 11
Heroku使用YamlDB Gem
http://www.github.com/ludicast/yaml_db/tree/master
| 归档时间: | 
 | 
| 查看次数: | 9611 次 | 
| 最近记录: |