Mercurial > hg > soundsoftware-site
comparison lib/tasks/.svn/text-base/extract_fixtures.rake.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | cbce1fd3b1b7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 desc 'Create YAML test fixtures from data in an existing database. | |
2 Defaults to development database. Set RAILS_ENV to override.' | |
3 | |
4 task :extract_fixtures => :environment do | |
5 sql = "SELECT * FROM %s" | |
6 skip_tables = ["schema_info"] | |
7 ActiveRecord::Base.establish_connection | |
8 (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| | |
9 i = "000" | |
10 File.open("#{RAILS_ROOT}/#{table_name}.yml", 'w' ) do |file| | |
11 data = ActiveRecord::Base.connection.select_all(sql % table_name) | |
12 file.write data.inject({}) { |hash, record| | |
13 | |
14 # cast extracted values | |
15 ActiveRecord::Base.connection.columns(table_name).each { |col| | |
16 record[col.name] = col.type_cast(record[col.name]) if record[col.name] | |
17 } | |
18 | |
19 hash["#{table_name}_#{i.succ!}"] = record | |
20 hash | |
21 }.to_yaml | |
22 end | |
23 end | |
24 end |