Reset Yourself Free

Reset Yourself Free

When was the last time you ran rake db:reset locally?

My guess is not recently. Nor do you think of doing it frequently. And I want to persuade you that deleting your precious local environment this way is a Very Good Thing Indeed.

Because, friends, db:reset will not only delete your database, but it will seed it too.

And by spending quality time with your seeds file, I believe you’ll make your entire development team more productive.

You’ll give your project the opportunity to grow its own shared development environment – a beautiful, idealistic place where all devs can talk through the same problem in the same context.

Here's a supporting post 👇🏽
https://christhesoul.com/2019/05/06/seeds-rails-and-you.html

3cce90569a90033ff3da3ae65b1c91cb?s=128

Chris Waters

May 01, 2019
Tweet

Transcript

  1. Reset yourself free Railsconf 2019

  2. Full disclosure

  3. My first Railsconf

  4. My first ever talk "

  5. @christhesoul #

  6. Suffolk $ ⛱

  7. None
  8. !8

  9. Hotels, Campsites, B&Bs, and Cottages

  10. None
  11. Monolith

  12. 300+ models 40,000+ commits 72 contributors

  13. Contractors

  14. None
  15. Rotations

  16. “Chris is the Oracle!”

  17. None
  18. None
  19. None
  20. Useless Local Environment

  21. Precious Local Environment

  22. Lynchpin
 Bottleneck

  23. Walter H. Deubner

  24. None
  25. Lightweight and repeatable

  26. rake db:reset

  27. What happens when I run rake db:reset?

  28. # activerecord/lib/active_record/railties/databases.rake desc 'Drops and recreates the db from db/schema.rb

    and loads seeds.' task reset: [ "db:drop", "db:setup" ] desc "Creates the database, loads the schema, and inits with seed data" task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
  29. # activerecord/lib/active_record/railties/databases.rake desc 'Drops and recreates the db from db/schema.rb

    and loads seeds.' task reset: [ "db:drop", "db:setup" ] desc "Creates the database, loads the schema, and inits with seed data" task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
  30. # activerecord/lib/active_record/railties/databases.rake desc 'Drops and recreates the db from db/schema.rb

    and loads seeds.' task reset: [ "db:drop", "db:setup" ] desc "Creates the database, loads the schema, and inits with seed data" task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
  31. What happens when I run rake db:seed?

  32. # databases.rake desc "Loads the seed data from db/seeds.rb" task

    seed: :load_config do db_namespace["abort_if_pending_migrations"].invoke ActiveRecord::Tasks::DatabaseTasks.load_seed end
  33. # databases.rake desc "Loads the seed data from db/seeds.rb" task

    seed: :load_config do db_namespace["abort_if_pending_migrations"].invoke ActiveRecord::Tasks::DatabaseTasks.load_seed end
  34. # activerecord/lib/active_record/tasks/database_tasks.rb def load_seed if seed_loader seed_loader.load_seed else raise "You

    tried to load seed data..." end end
  35. # activerecord/lib/active_record/tasks/database_tasks.rb def load_seed if seed_loader seed_loader.load_seed else raise "You

    tried to load seed data..." end end
  36. # activerecord/lib/active_record/tasks/database_tasks.rb def seed_loader @seed_loader ||= Rails.application end

  37. # railties/lib/rails/engine.rb def load_seed seed_file = paths["db/seeds.rb"].existent.first with_inline_jobs { load(seed_file)

    } if seed_file end
  38. TL;DR: it’s just Ruby.

  39. Creating Seeds

  40. # db/seeds.rb Person.create!( given_name: 'Eva', family_name: 'Smith' )

  41. # db/seeds.rb Person.create!([ { given_name: ‘Eva', family_name: 'Smith' }, {

    given_name: ‘Daisy', family_name: 'Renton' } ])
  42. gem install ffaker

  43. # db/seeds.rb 50.times do Person.create!( given_name: FFaker::Name.unique.first_name, family_name: FFaker::Name.unique.last_name )

    end
  44. # Make a pie "#{FFaker::Food.meat} and #{FFaker::Food.herb_or_spice} Pie" # Personal

    favourite FFaker::Boolean.maybe
  45. Seeding a monolith

  46. # Two seed files # db/seeds/people.rb # db/seeds/pies.rb # db/seeds.rb

    %w[ people pies ].each do |seed| require_relative("seeds/#{seed}") end
  47. class Seeder SEEDS = %w[people pies] def seed! SEEDS.each do

    |seed| require_relative("seeds/#{seed}") end end end
  48. class Seeder SEEDS = %w[people pies] def initialize @seeds =

    ENV["seedfile"] || SEEDS end def seed! Array(@seeds).each do |seed| require_relative("seeds/#{seed}") end end end
  49. Bonus round

  50. # activerecord/lib/active_record/tasks/database_tasks.rb def seed_loader @seed_loader ||= Rails.application end

  51. # activerecord/lib/active_record/tasks/database_tasks.rb def load_seed if seed_loader seed_loader.load_seed else raise "You

    tried to load seed data..." end end
  52. class Seeder SEEDS = %w[people pies] def initialize @seeds =

    ENV["seedfile"] || SEEDS end def load_seed Array(@seeds).each do |seed| require_relative("seeds/#{seed}") end end end
  53. # config/application.rb require_relative '../db/seeder.rb' ActiveRecord::Tasks::DatabaseTasks.seed_loader = Seeder.new

  54. Instant Local Environment

  55. Consistent Local Environment

  56. Resilient Local Environment

  57. Prosperous Local Environment More on this here -> https://christhesoul.com/2019/05/06/seeds-rails-and-you.html

  58. Instant Staging Environment

  59. Reset yourself free Railsconf 2019