task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) model = model_name.constantize results = model.pluck(*model.column_names) return 0 if results.size.zero? data = CSV.generate do |csv| csv << model.column_names results.each { |result| csv << result } end file_path = "db/fixtures/bulk_insert/#{model_name}.csv" File.open(file_path, 'w') do |file| file.write(data) p "Finished generating #{file_path}(#{results.size} rows) from DB!” end return results.size end total_count = 0 ActiveRecord::Base.connection .tables .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ
task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) model = model_name.constantize results = model.pluck(*model.column_names) return 0 if results.size.zero? data = CSV.generate do |csv| csv << model.column_names results.each { |result| csv << result } end file_path = "db/fixtures/bulk_insert/#{model_name}.csv" File.open(file_path, 'w') do |file| file.write(data) p "Finished generating #{file_path}(#{results.size} rows) from DB!” end return results.size end total_count = 0 ActiveRecord::Base.connection .tables .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ
task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) model = model_name.constantize results = model.pluck(*model.column_names) return 0 if results.size.zero? data = CSV.generate do |csv| csv << model.column_names results.each { |result| csv << result } end file_path = "db/fixtures/bulk_insert/#{model_name}.csv" File.open(file_path, 'w') do |file| file.write(data) p "Finished generating #{file_path}(#{results.size} rows) from DB!” end return results.size end total_count = 0 ActiveRecord::Base.connection .tables .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ
task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) model = model_name.constantize results = model.pluck(*model.column_names) return 0 if results.size.zero? data = CSV.generate do |csv| csv << model.column_names results.each { |result| csv << result } end file_path = "db/fixtures/bulk_insert/#{model_name}.csv" File.open(file_path, 'w') do |file| file.write(data) p "Finished generating #{file_path}(#{results.size} rows) from DB!” end return results.size end total_count = 0 ActiveRecord::Base.connection .tables .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ
task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) model = model_name.constantize results = model.pluck(*model.column_names) return 0 if results.size.zero? data = CSV.generate do |csv| csv << model.column_names results.each { |result| csv << result } end file_path = "db/fixtures/bulk_insert/#{model_name}.csv" File.open(file_path, 'w') do |file| file.write(data) p "Finished generating #{file_path}(#{results.size} rows) from DB!” end return results.size end total_count = 0 ActiveRecord::Base.connection .tables .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ
task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) model = model_name.constantize results = model.pluck(*model.column_names) return 0 if results.size.zero? data = CSV.generate do |csv| csv << model.column_names results.each { |result| csv << result } end file_path = "db/fixtures/bulk_insert/#{model_name}.csv" File.open(file_path, 'w') do |file| file.write(data) p "Finished generating #{file_path}(#{results.size} rows) from DB!” end return results.size end total_count = 0 ActiveRecord::Base.connection .tables .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
csv = CSV.table(file_path) ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end end CSV͔ΒόϧΫΠϯαʔτ
until file_paths.empty? do file_path = file_paths.shift csv = CSV.table(file_path) next if csv.size.zero? begin ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name = model.table_name p "Finished inserting #{csv.size} records into #{table_name} table!" end rescue ActiveRecord::InvalidForeignKey => e ActiveRecord::Base.connection.execute 'ROLLBACK' if file_path != previous_path p "Inserting #{file_path} failed due to #{e.message}." p "Therefore, it is pushed to the end of file_paths to avoid the error." file_paths << file_path else p "Inserting #{file_path} failed twice in a row. **Inserting #{file_path} was SKIPPED.**" end end previous_path = file_path end
…… …… % rails db:seed "Inserting db/fixtures/bulk_insert/Appointment.csv failed due to PG::ForeignKeyViolation: ERROR: insert or update on table \"appointments\" violates foreign key constraint \"fk_rails_4fdc466d95\"\nDETAIL: Key (branch_id)=(8) is not present in table \"branches\".\n." "Therefore, it is pushed to the end of file_paths to avoid the error.”
…… …… % rails db:seed "Inserting db/fixtures/bulk_insert/Appointment.csv failed due to PG::ForeignKeyViolation: ERROR: insert or update on table \"appointments\" violates foreign key constraint \"fk_rails_4fdc466d95\"\nDETAIL: Key (branch_id)=(8) is not present in table \"branches\".\n." "Therefore, it is pushed to the end of file_paths to avoid the error.” …… "Finished inserting 11 records into branches table!”
…… …… % rails db:seed "Inserting db/fixtures/bulk_insert/Appointment.csv failed due to PG::ForeignKeyViolation: ERROR: insert or update on table \"appointments\" violates foreign key constraint \"fk_rails_4fdc466d95\"\nDETAIL: Key (branch_id)=(8) is not present in table \"branches\".\n." "Therefore, it is pushed to the end of file_paths to avoid the error.” …… "Finished inserting 11 records into branches table!” …… "Finished inserting 2 records into appointments table!” Insertແࣄྃ😉
def correct_order_exist?(order_txt, dir) File.exist?(order_txt) && File.open(order_txt, 'r').read.split("\n").sort == Dir.glob(dir).sort end file_paths = correct_order_exist?(order_txt, dir) ? File.open(order_txt, 'r').read.split("\n") : Dir.glob(dir) previous_path = '' inserted_file_paths = [] until file_paths.empty? do file_path = file_paths.shift csv = CSV.table(file_path) next if csv.size.zero? begin ActiveRecord::Base.transaction do …… inserted_file_paths << file_path end …… end File.open(order_txt, 'w') do |file| file.write(inserted_file_paths.join("\n")) p "Finished generating #{order_txt} for future seeding!" end
def correct_order_exist?(order_txt, dir) File.exist?(order_txt) && File.open(order_txt, 'r').read.split("\n").sort == Dir.glob(dir).sort end file_paths = correct_order_exist?(order_txt, dir) ? File.open(order_txt, 'r').read.split("\n") : Dir.glob(dir) previous_path = '' inserted_file_paths = [] until file_paths.empty? do file_path = file_paths.shift csv = CSV.table(file_path) next if csv.size.zero? begin ActiveRecord::Base.transaction do …… inserted_file_paths << file_path end …… end File.open(order_txt, 'w') do |file| file.write(inserted_file_paths.join("\n")) p "Finished generating #{order_txt} for future seeding!" end ←
def correct_order_exist?(order_txt, dir) File.exist?(order_txt) && File.open(order_txt, 'r').read.split("\n").sort == Dir.glob(dir).sort end file_paths = correct_order_exist?(order_txt, dir) ? File.open(order_txt, 'r').read.split("\n") : Dir.glob(dir) previous_path = '' inserted_file_paths = [] until file_paths.empty? do file_path = file_paths.shift csv = CSV.table(file_path) next if csv.size.zero? begin ActiveRecord::Base.transaction do …… inserted_file_paths << file_path end …… end File.open(order_txt, 'w') do |file| file.write(inserted_file_paths.join("\n")) p "Finished generating #{order_txt} for future seeding!" end