Patch #41954 » 0007-Support-table-filters.patch
| lib/tasks/extract_fixtures.rake | ||
|---|---|---|
| 38 | 38 |
dir = ENV['DIR'] || './tmp/fixtures' |
| 39 | 39 |
time_offset = ENV['TIME_OFFSET'] || '' |
| 40 | 40 |
skip_tables = ENV['SKIP_TABLES']&.split(',') || []
|
| 41 |
table_filters = ENV['TABLE_FILTERS']&.split(';')&.map {|tf| tf.split(":", 2)}&.to_h || {}
|
|
| 41 | 42 | |
| 42 | 43 |
FileUtils.mkdir_p(dir) |
| 43 | 44 |
if time_offset.present? && !time_offset.match?(/^([+-](0[0-9]|1[0-4]):[0-5][0-9])$/) |
| ... | ... | |
| 52 | 53 |
columns = ActiveRecord::Base.connection.columns(table_name) |
| 53 | 54 |
column_names = columns.map(&:name) |
| 54 | 55 |
order_columns = column_names.include?('id') ? 'id' : column_names.join(', ')
|
| 55 |
sql = "SELECT * FROM #{table_name} ORDER BY #{order_columns}"
|
|
| 56 |
where_clause = table_filters.has_key?(table_name) ? "WHERE #{table_filters[table_name]}" : ''
|
|
| 57 |
sql = "SELECT * FROM #{table_name} #{where_clause} ORDER BY #{order_columns}"
|
|
| 56 | 58 |
data = ActiveRecord::Base.connection.select_all(sql) |
| 57 | 59 |
file.write data.inject({}) { |hash, record|
|
| 58 | 60 |
# cast extracted values |