Patch #41954 » 0001-Update-extract_fixtures-task-with-configurable-dir.patch
lib/tasks/extract_fixtures.rake | ||
---|---|---|
19 | 19 |
Defaults to development database. Set RAILS_ENV to override.' |
20 | 20 | |
21 | 21 |
task :extract_fixtures => :environment do |
22 |
dir = ENV['DIR'] || './tmp/fixtures' |
|
23 |
FileUtils.mkdir_p(dir) |
|
24 | ||
22 | 25 |
sql = "SELECT * FROM %s" |
23 |
skip_tables = ["schema_info"]
|
|
26 |
skip_tables = ["schema_migrations", "ar_internal_metadata"]
|
|
24 | 27 |
ActiveRecord::Base.establish_connection |
25 | 28 |
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| |
26 | 29 |
i = "000" |
27 |
File.open("#{Rails.root}/#{table_name}.yml", 'w' ) do |file|
|
|
30 |
File.open(File.join(dir, "#{table_name}.yml"), 'w') do |file|
|
|
28 | 31 |
data = ActiveRecord::Base.connection.select_all(sql % table_name) |
29 | 32 |
file.write data.inject({}) { |hash, record| |
30 | 33 |
# cast extracted values |
31 | 34 |
ActiveRecord::Base.connection.columns(table_name).each { |col| |
32 |
record[col.name] = col.type_cast(record[col.name]) if record[col.name]
|
|
35 |
record[col.name] = ActiveRecord::Type.lookup(col.type).deserialize(record[col.name]) if record[col.name]
|
|
33 | 36 |
} |
34 | 37 |
hash["#{table_name}_#{i.succ!}"] = record |
35 | 38 |
hash |