Patch #41954 » 0002-Sort-records-for-stability.patch
lib/tasks/extract_fixtures.rake | ||
---|---|---|
22 | 22 |
dir = ENV['DIR'] || './tmp/fixtures' |
23 | 23 |
FileUtils.mkdir_p(dir) |
24 | 24 | |
25 |
sql = "SELECT * FROM %s" |
|
26 | 25 |
skip_tables = ["schema_migrations", "ar_internal_metadata"] |
27 | 26 |
ActiveRecord::Base.establish_connection |
28 | 27 |
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| |
29 | 28 |
i = "000" |
30 | 29 |
File.open(File.join(dir, "#{table_name}.yml"), 'w') do |file| |
31 |
data = ActiveRecord::Base.connection.select_all(sql % table_name) |
|
30 |
columns = ActiveRecord::Base.connection.columns(table_name) |
|
31 |
column_names = columns.map(&:name) |
|
32 |
order_columns = column_names.include?('id') ? 'id' : column_names.join(', ') |
|
33 |
sql = "SELECT * FROM #{table_name} ORDER BY #{order_columns}" |
|
34 |
data = ActiveRecord::Base.connection.select_all(sql) |
|
32 | 35 |
file.write data.inject({}) { |hash, record| |
33 | 36 |
# cast extracted values |
34 |
ActiveRecord::Base.connection.columns(table_name).each { |col|
|
|
37 |
columns.each { |col|
|
|
35 | 38 |
record[col.name] = ActiveRecord::Type.lookup(col.type).deserialize(record[col.name]) if record[col.name] |
36 | 39 |
} |
37 | 40 |
hash["#{table_name}_#{i.succ!}"] = record |