Skip to content

Commit

Permalink
Support table filters
Browse files Browse the repository at this point in the history
  • Loading branch information
sanak committed Dec 13, 2024
1 parent 2947955 commit 21ed2fc
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/tasks/extract_fixtures.rake
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ task :extract_fixtures => :environment do
dir = ENV['DIR'] || './tmp/fixtures'
time_offset = ENV['TIME_OFFSET'] || ''
skip_tables = ENV['SKIP_TABLES']&.split(',') || []
table_filters = ENV['TABLE_FILTERS']&.split(';')&.map {|tf| tf.split(":", 2)}&.to_h || {}

FileUtils.mkdir_p(dir)
if time_offset.present? && !time_offset.match?(/^([+-](0[0-9]|1[0-4]):[0-5][0-9])$/)
Expand All @@ -52,7 +53,8 @@ task :extract_fixtures => :environment do
columns = ActiveRecord::Base.connection.columns(table_name)
column_names = columns.map(&:name)
order_columns = column_names.include?('id') ? 'id' : column_names.join(', ')
sql = "SELECT * FROM #{table_name} ORDER BY #{order_columns}"
where_clause = table_filters.has_key?(table_name) ? "WHERE #{table_filters[table_name]}" : ''
sql = "SELECT * FROM #{table_name} #{where_clause} ORDER BY #{order_columns}"
data = ActiveRecord::Base.connection.select_all(sql)
file.write data.inject({}) { |hash, record|
# cast extracted values
Expand Down

0 comments on commit 21ed2fc

Please sign in to comment.