Upgrade to Pro — share decks privately, control downloads, hide ads and more …

seeds.rbを書かずに開発環境データを整備する

gogutan
October 27, 2023

 seeds.rbを書かずに開発環境データを整備する

gogutan

October 27, 2023
Tweet

More Decks by gogutan

Other Decks in Programming

Transcript

  1. db/seeds.rbͱ͸ # db/seeds.rb Book.create!( title: 'ֶ໰ͷ͢͢Ί', author: '෱୔་٢' ) %

    rails db:seed % rails c irb(main):001> Book.first => #<Book:0x000000010cb68110 id: 1, title: “ֶ໰ͷ͢͢Ί", author: "෱୔་٢", created_at: Tue, 17 Oct 2023 01:03:26.528993000 JST +09:00, updated_at: Tue, 17 Oct 2023 01:03:26.528993000 JST +09:00> ॳظσʔλΛ؆୯ʹ ༻ҙͰ͖ͯخ͍͠😊
  2. # db/fixtures/01_books.rb require 'csv' books = [] CSV.foreach('db/fixtures/books.csv').with_index do |row,

    i| next if i.zero? books << { id: row[0], title: row[1], author: row[2], created_at: row[3], updated_at: row[4] } end Book.insert_all books # db/fixtures/books.csv id,title,author,created_at,updated_at 1,ֶ໰ͷ͢͢Ί,෱୔་٢,2022-06-20 10:44,2022-06-20 10:44 2,ҏ౾ͷགྷΓࢠ,઒୺߁੒,2022-06-20 10:44,2022-06-20 10:44 CSV͔ΒSeed͍ͯ͠Δ ϞσϧΛൃݟ👀
  3. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ
  4. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ
  5. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ
  6. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ
  7. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ
  8. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ
  9. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ id,title,author,created_at,updated_at 1,ֶ໰ͷ͢͢Ί,෱୔་٢,2021/02/01 12:33,2021/02/01 12:33 2,ҏ౾ͷགྷΓࢠ,઒୺߁੒,2021/02/01 12:33,2021/02/01 12:33 …… ←
  10. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ id,title,author,created_at,updated_at 1,ֶ໰ͷ͢͢Ί,෱୔་٢,2021/02/01 12:33,2021/02/01 12:33 2,ҏ౾ͷགྷΓࢠ,઒୺߁੒,2021/02/01 12:33,2021/02/01 12:33 ……
  11. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ id,title,author,created_at,updated_at 1,ֶ໰ͷ͢͢Ί,෱୔་٢,2021/02/01 12:33,2021/02/01 12:33 2,ҏ౾ͷགྷΓࢠ,઒୺߁੒,2021/02/01 12:33,2021/02/01 12:33 ……
  12. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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ʹશϨίʔυग़ྗ id,title,author,created_at,updated_at 1,ֶ໰ͷ͢͢Ί,෱୔་٢,2021/02/01 12:33,2021/02/01 12:33 2,ҏ౾ͷགྷΓࢠ,઒୺߁੒,2021/02/01 12:33,2021/02/01 12:33 ……
  13. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    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 .reject{ |t| t.in? %w(schema_migrations ar_internal_metadata) } .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" end end CSVʹશϨίʔυग़ྗ id,title,author,created_at,updated_at 1,ֶ໰ͷ͢͢Ί,෱୔་٢,2021/02/01 12:33,2021/02/01 12:33 2,ҏ౾ͷགྷΓࢠ,઒୺߁੒,2021/02/01 12:33,2021/02/01 12:33 …… ←
  14. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  15. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  16. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  17. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  18. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  19. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  20. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  21. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  22. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') file_paths.each do |file_path|

    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͔ΒόϧΫΠϯαʔτ
  23. rails db:seed࣮ߦ % rails db:migrate:reset Dropped database …… Created database

    …… …… % rails db:seed "Finished inserting 2 records into books table!” όϧΫΠϯαʔτ׬ྃ💪
  24. rails db:seed࣮ߦ % rails db:migrate:reset Dropped database …… Created database

    …… …… % rails db:seed "Finished inserting 2 records into books table!” % rails s όϧΫΠϯαʔτ׬ྃ💪
  25. rails db:seed࣮ߦ % rails db:migrate:reset Dropped database …… Created database

    …… …… % rails db:seed "Finished inserting 2 records into books table!” % rails s όϧΫΠϯαʔτ׬ྃ💪
  26. ࣮ӡ༻ΞϓϦͰ ࣮ફ‼ % rails generate_csv_from_db "Finished generating db/fixtures/……(3 rows) from

    DB!” "Finished generating db/fixtures/……(107 rows) from DB!” "Finished generating db/fixtures/……(24 rows) from DB!” "Finished generating db/fixtures/……(8 rows) from DB!” "Finished generating db/fixtures/……(71 rows) from DB!” …… "Total row count: 3113"
  27. ࣮ӡ༻ΞϓϦͰ ࣮ફ‼ % rails generate_csv_from_db "Finished generating db/fixtures/……(3 rows) from

    DB!” "Finished generating db/fixtures/……(107 rows) from DB!” "Finished generating db/fixtures/……(24 rows) from DB!” "Finished generating db/fixtures/……(8 rows) from DB!” "Finished generating db/fixtures/……(71 rows) from DB!” …… "Total row count: 3113" CSVͷੜ੒͸׬ྃ👍
  28. ࣮ӡ༻ΞϓϦͰ ࣮ફ‼ % rails db:migrate:reset Dropped database …… Created database

    …… …… % rails db:seed rails aborted! ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "appointments" violates foreign key constraint "fk_rails_4fdc466d95" DETAIL: Key (branch_id)=(8) is not present in table "branches".
  29. ࣮ӡ༻ΞϓϦͰ ࣮ફ‼ % rails db:migrate:reset Dropped database …… Created database

    …… …… % rails db:seed rails aborted! ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: insert or update on table "appointments" violates foreign key constraint "fk_rails_4fdc466d95" DETAIL: Key (branch_id)=(8) is not present in table "branches". class Appointment < ApplicationRecord belongs_to :branch …… end ਌ςʔϒϧΛઌʹ Insert͢Δඞཁ͕͋Δ
  30. # db/seeds.rb require 'csv' file_paths = Dir.glob('db/fixtures/bulk_insert/*.csv') previous_path = ''

    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
  31. ࣮ӡ༻ΞϓϦͰ ࣮ફ‼ % rails db:migrate:reset Dropped database …… Created database

    …… …… % 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.”
  32. ࣮ӡ༻ΞϓϦͰ ࣮ફ‼ % rails db:migrate:reset Dropped database …… Created database

    …… …… % 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!”
  33. ࣮ӡ༻ΞϓϦͰ ࣮ફ‼ % rails db:migrate:reset Dropped database …… Created database

    …… …… % 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΋ແࣄ׬ྃ😉
  34. # db/seeds.rb require 'csv' dir = 'db/fixtures/bulk_insert/*.csv' order_txt = 'db/fixtures/bulk_insert/file_paths_order.txt'

    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
  35. # db/seeds.rb require 'csv' dir = 'db/fixtures/bulk_insert/*.csv' order_txt = 'db/fixtures/bulk_insert/file_paths_order.txt'

    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 ←
  36. # db/seeds.rb require 'csv' dir = 'db/fixtures/bulk_insert/*.csv' order_txt = 'db/fixtures/bulk_insert/file_paths_order.txt'

    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
  37. ղܾํ๏ ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name

    = model.table_name sql = "SELECT SETVAL ('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}))" ActiveRecord::Base.connection.execute(sql) p "Finished inserting #{csv.size} records into #{table_name} table!" inserted_file_paths << file_path end γʔέϯεΦϒδΣΫτΛ खಈͰमਖ਼͢Δ ໌ࣔతʹࢦఆͨ͜͠ͱͰɺ γʔέϯεΦϒδΣΫτ͕ਖ਼ৗʹߋ৽͞Εͣ ←
  38. ղܾํ๏ ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize model.insert_all! csv.map(&:to_hash) table_name

    = model.table_name sql = "SELECT SETVAL ('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}))" ActiveRecord::Base.connection.execute(sql) p "Finished inserting #{csv.size} records into #{table_name} table!" inserted_file_paths << file_path end γʔέϯεΦϒδΣΫτΛ खಈͰमਖ਼͢Δ ໌ࣔతʹࢦఆͨ͜͠ͱͰɺ γʔέϯεΦϒδΣΫτ͕ਖ਼ৗʹߋ৽͞Εͣ ← (PostgreSQLͷ৔߹)
  39. ΋͏Ұ౓ొ࿥ ແࣄʹ`id: 3`Ͱొ࿥׬ྃ🙌 % rails db:migrate:reset % rails db:seed %

    rails s % rails c irb(main):001> Appointment.last => #<Appointment:0x0000000110e99038 id: 3, ……
  40. # lib/tasks/generate_csv_from_db.rake require 'csv' desc 'for generating csv from db'

    task :generate_csv_from_db => :environment do |_task, args| def generate_csv(model_name) …… end total_count = 0 ActiveRecord::Base.connection .tables .reject{ |t| t.in? %w(schema_migrations ar_internal_metadata) } .each { |t| total_count += generate_csv(t.classify) } p "Total row count: #{total_count}" File.open("db/fixtures/bulk_insert/generated_date.txt", 'w') do |file| generated_date = Time.current.to_s file.write(generated_date) p "Saved generated_date: #{generated_date} to db/fixtures/bulk_insert/generated_date.txt!" end end CSVʹશϨίʔυग़ྗ ←
  41. # db/seeds.rb require 'csv' def offset_dates(hash, generated_before) hash[:starts_at] = hash[:starts_at].to_time

    + generated_before if hash[:starts_at].present? hash[:ends_at] = hash[:ends_at].to_time + generated_before if hash[:ends_at].present? hash end …… generated_date_txt = 'db/fixtures/bulk_insert/generated_date.txt' ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize generated_date = File.open(generated_date_txt, 'r').read generated_before = (Time.current.to_date - generated_date.to_date).to_i.days model.insert_all! csv.map(&:to_hash).map { |hash| offset_dates(hash, generated_before) } table_name = model.table_name sql = "SELECT SETVAL ('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}))" ActiveRecord::Base.connection.execute(sql) p "Finished inserting #{csv.size} records into #{table_name} table!" inserted_file_paths << file_path end ……
  42. # db/seeds.rb require 'csv' def offset_dates(hash, generated_before) hash[:starts_at] = hash[:starts_at].to_time

    + generated_before if hash[:starts_at].present? hash[:ends_at] = hash[:ends_at].to_time + generated_before if hash[:ends_at].present? hash end …… generated_date_txt = 'db/fixtures/bulk_insert/generated_date.txt' ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize generated_date = File.open(generated_date_txt, 'r').read generated_before = (Time.current.to_date - generated_date.to_date).to_i.days model.insert_all! csv.map(&:to_hash).map { |hash| offset_dates(hash, generated_before) } table_name = model.table_name sql = "SELECT SETVAL ('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}))" ActiveRecord::Base.connection.execute(sql) p "Finished inserting #{csv.size} records into #{table_name} table!" inserted_file_paths << file_path end ……
  43. # db/seeds.rb require 'csv' def offset_dates(hash, generated_before) hash[:starts_at] = hash[:starts_at].to_time

    + generated_before if hash[:starts_at].present? hash[:ends_at] = hash[:ends_at].to_time + generated_before if hash[:ends_at].present? hash end …… generated_date_txt = 'db/fixtures/bulk_insert/generated_date.txt' ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize generated_date = File.open(generated_date_txt, 'r').read generated_before = (Time.current.to_date - generated_date.to_date).to_i.days model.insert_all! csv.map(&:to_hash).map { |hash| offset_dates(hash, generated_before) } table_name = model.table_name sql = "SELECT SETVAL ('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}))" ActiveRecord::Base.connection.execute(sql) p "Finished inserting #{csv.size} records into #{table_name} table!" inserted_file_paths << file_path end …… 1 day ←
  44. # db/seeds.rb require 'csv' def offset_dates(hash, generated_before) hash[:starts_at] = hash[:starts_at].to_time

    + generated_before if hash[:starts_at].present? hash[:ends_at] = hash[:ends_at].to_time + generated_before if hash[:ends_at].present? hash end …… generated_date_txt = 'db/fixtures/bulk_insert/generated_date.txt' ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize generated_date = File.open(generated_date_txt, 'r').read generated_before = (Time.current.to_date - generated_date.to_date).to_i.days model.insert_all! csv.map(&:to_hash).map { |hash| offset_dates(hash, generated_before) } table_name = model.table_name sql = "SELECT SETVAL ('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}))" ActiveRecord::Base.connection.execute(sql) p "Finished inserting #{csv.size} records into #{table_name} table!" inserted_file_paths << file_path end …… 1 day ←
  45. # db/seeds.rb require 'csv' def offset_dates(hash, generated_before) hash[:starts_at] = hash[:starts_at].to_time

    + generated_before if hash[:starts_at].present? hash[:ends_at] = hash[:ends_at].to_time + generated_before if hash[:ends_at].present? hash end …… generated_date_txt = 'db/fixtures/bulk_insert/generated_date.txt' ActiveRecord::Base.transaction do model = File.basename(file_path, '.csv').constantize generated_date = File.open(generated_date_txt, 'r').read generated_before = (Time.current.to_date - generated_date.to_date).to_i.days model.insert_all! csv.map(&:to_hash).map { |hash| offset_dates(hash, generated_before) } table_name = model.table_name sql = "SELECT SETVAL ('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}))" ActiveRecord::Base.connection.execute(sql) p "Finished inserting #{csv.size} records into #{table_name} table!" inserted_file_paths << file_path end ……
  46. seeds.rbΛॻ͔ͳ͍ํ๏ͷ·ͱΊ • seeds.rbΛͨ͘͞Μॻ͍ͨ😂(ΤϯδχΞ͋Δ͋Δ) • γʔέϯεΦϒδΣΫτɺ೔෇ܥΧϥϜͷௐ੔ͳͲΠϨΪϡϥʔͳରԠ͕ 
 ͍͔ͭ͘ඞཁ(ಛʹ೔෇ͷௐ੔͸ɺΞϓϦͷ࢓༷ʹΑΔͱࢥΘΕΔ) • ຖճseeds.rbΛॻ͔ͳ͍͍ͯ͘ͷ͸շద🥳 


    ಋೖલ: ໿40/100Ϟσϧɺ߹ܭ50,000Ϩίʔυఔ౓ɺSeed࣮ߦʹ30ඵఔ౓ 
 ಋೖޙ: ໿90/100Ϟσϧɺ߹ܭ3,000Ϩίʔυఔ౓ɺSeed࣮ߦʹ5ඵఔ౓ 
 →࣮ࡍͷΞϓϦͷ࢖ΘΕํΛ૝ఆɺಈ࡞֬ೝ΍ෆ۩߹ݕূʹ໾ཱ͍ͬͯΔ