Created
September 30, 2008 15:06
-
-
Save tinogomes/13845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def extract_fixtures(dirname) | |
sql = "SELECT * FROM %s" | |
skip_tables = ["schema_info"] | |
ActiveRecord::Base.establish_connection | |
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| | |
extract_fixture_from table_name, "#{dirname}/#{table_name}.yml" | |
end | |
end | |
def extract_fixture_from(table_name, filename) | |
sql = "SELECT * FROM %s" | |
ActiveRecord::Base.establish_connection | |
File.open(filename, 'w') do |file| | |
i='000' | |
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 | |
namespace :db do | |
namespace :fixtures do | |
namespace :spec do | |
desc 'Create YAML spec fixtures from data in an existing database. | |
Defaults to development database. Set RAILS_ENV to override.' | |
task :extract_all => :environment do | |
extract_fixtures("#{RAILS_ROOT}/spec/fixtures") | |
end | |
end | |
namespace :test do | |
desc 'Create YAML test fixtures from data in an existing database. | |
Defaults to development database. Set RAILS_ENV to override.' | |
task :extract_all => :environment do | |
extract_fixtures("#{RAILS_ROOT}/test/fixtures") | |
end | |
end | |
desc 'Create YAML file from data in an existing database. | |
Set TABLE_NAME and FILENAME environments.' | |
task :extract_from => :environment do | |
extract_fixture_from ENV['TABLE_NAME'], ENV['FILENAME'] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment