@@ -1,22 +1,22 | |||||
1 | desc 'Create YAML test fixtures from data in an existing database. |
|
1 | desc 'Create YAML test fixtures from data in an existing database. | |
2 | Defaults to development database. Set RAILS_ENV to override.' |
|
2 | Defaults to development database. Set RAILS_ENV to override.' | |
3 |
|
3 | |||
4 | task :extract_fixtures => :environment do |
|
4 | task :extract_fixtures => :environment do | |
5 | sql = "SELECT * FROM %s" |
|
5 | sql = "SELECT * FROM %s" | |
6 | skip_tables = ["schema_info"] |
|
6 | skip_tables = ["schema_info"] | |
7 | ActiveRecord::Base.establish_connection |
|
7 | ActiveRecord::Base.establish_connection | |
8 | (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| |
|
8 | (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| | |
9 | i = "000" |
|
9 | i = "000" | |
10 | File.open("#{Rails.root}/#{table_name}.yml", 'w' ) do |file| |
|
10 | File.open("#{Rails.root}/#{table_name}.yml", 'w' ) do |file| | |
11 | data = ActiveRecord::Base.connection.select_all(sql % table_name) |
|
11 | data = ActiveRecord::Base.connection.select_all(sql % table_name) | |
12 | file.write data.inject({}) { |hash, record| |
|
12 | file.write data.inject({}) { |hash, record| | |
13 | # cast extracted values |
|
13 | # cast extracted values | |
14 | ActiveRecord::Base.connection.columns(table_name).each { |col| |
|
14 | ActiveRecord::Base.connection.columns(table_name).each { |col| | |
15 | record[col.name] = col.type_cast(record[col.name]) if record[col.name] |
|
15 | record[col.name] = col.type_cast(record[col.name]) if record[col.name] | |
16 | } |
|
16 | } | |
17 | hash["#{table_name}_#{i.succ!}"] = record |
|
17 | hash["#{table_name}_#{i.succ!}"] = record | |
18 | hash |
|
18 | hash | |
19 | }.to_yaml |
|
19 | }.to_yaml | |
20 | end |
|
20 | end | |
21 | end |
|
21 | end | |
22 | end |
|
22 | end |
General Comments 0
You need to be logged in to leave comments.
Login now