From 658a6eae388ed08e51c0cb36693e79ce60abb5c7 Mon Sep 17 00:00:00 2001 From: Ko Nagase Date: Fri, 13 Dec 2024 11:15:33 +0900 Subject: [PATCH 8/8] Use record id for fixture key id if exists --- lib/tasks/extract_fixtures.rake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/tasks/extract_fixtures.rake b/lib/tasks/extract_fixtures.rake index 28ef3fdfd..6d3a4f65b 100644 --- a/lib/tasks/extract_fixtures.rake +++ b/lib/tasks/extract_fixtures.rake @@ -52,7 +52,8 @@ task :extract_fixtures => :environment do File.open(File.join(dir, "#{table_name}.yml"), 'w') do |file| columns = ActiveRecord::Base.connection.columns(table_name) column_names = columns.map(&:name) - order_columns = column_names.include?('id') ? 'id' : column_names.join(', ') + has_id_column = column_names.include?('id') + order_columns = has_id_column ? 'id' : column_names.join(', ') 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) @@ -70,7 +71,8 @@ task :extract_fixtures => :environment do end end end - hash["#{table_name}_#{i.succ!}"] = record + key_id = has_id_column ? sprintf('%03d', record['id']) : i.succ! + hash["#{table_name}_#{key_id}"] = record hash }.to_yaml end -- 2.47.1