Skip to content

Instantly share code, notes, and snippets.

@nordringrayhide
Created September 13, 2010 04:05
Show Gist options
  • Save nordringrayhide/576787 to your computer and use it in GitHub Desktop.
Save nordringrayhide/576787 to your computer and use it in GitHub Desktop.
require "ya2yaml"
desc 'Create YAML test fixtures from data in an existing database.'
task 'db:fixtures:dump' => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info", "schema_migrations"]
ActiveRecord::Base.establish_connection
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
i = "0000000"
File.open("#{RAILS_ROOT}/db/bootstrap/fixtures/#{table_name}.yml", '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
}.ya2yaml
end
end
end
desc "Load fixtures data into the database"
task :'db:fixtures:migrate' => :environment do
require 'active_record/fixtures'
ActiveRecord::Base.establish_connection
fixture_dir = "#{RAILS_ROOT}/db/bootstrap/fixtures"
tables = Dir["#{fixture_dir}/*.yml"]
tables.collect! {|t| File.basename(t, '.yml')}
Fixtures.create_fixtures(fixture_dir, tables)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment