Rails Best Practices
Reset Yourself Free (The Joy of Destroying your DB Daily)

Summarized using AI

Reset Yourself Free (The Joy of Destroying your DB Daily)

Chris Waters • May 26, 2019 • Minneapolis, MN

In his talk "Reset Yourself Free (The Joy of Destroying your DB Daily)" at RailsConf 2019, Chris Waters examines the importance and benefits of regularly resetting local development databases using the Rake command db:reset in Ruby on Rails applications. Waters argues that this practice not only helps in maintaining a clean and consistent development environment but also enhances team productivity.

Key points discussed in the talk include:
- Introduction: Chris shares his excitement about attending his first RailsConf and introduces himself, mentioning his background in building online booking systems for tourism.
- The Bottleneck Problem: Waters describes his experience as the only full-time developer while relying on contractors, which created a bottleneck in the development process. He realized this dependency hindered team efficiency.
- The Solution - Rake DB Reset: Emphasizing the utility of rake db:reset, he explains that this command not only deletes the database but also seeds it with data, thus providing developers with an immediate and consistent local environment.
- Seeding Strategies: Waters outlines various methods for creating seeds for the database, including leveraging the faker gem to generate realistic sample data and organizing seed files around the application's models. He highlights the creation of seed classes for improved organization and maintenance.
- Benefits of a Reset Database:
- Instant local environment setup
- Consistency among developers' environments
- Increased resilience by allowing developers to reset their environments easily
- Encouragement of collaborative development as team members can build and contribute to the seed database easily.
- Real-world Applications: Waters discusses seeding complicated relationships in the database and even brings up examples of integrating with external services like Stripe.
- Conclusions: By adopting a system where the database can be reset easily and quickly, the team saw significant improvements in speed and collaboration. Waters encourages developers to embrace the rake db:reset as a way to 'reset yourself free' from the constraints of traditional development environments.

Overall, Waters advocates for the benefits of simplifying the onboarding process for new developers, allowing greater experimentation, and ultimately leading to a more productive development cycle.

This talk is an insightful guide for Rails developers looking to improve their workflows and enhance team collaboration through effective database management.

Reset Yourself Free (The Joy of Destroying your DB Daily)
Chris Waters • May 26, 2019 • Minneapolis, MN

RailsConf 2019 - Reset Yourself Free (The Joy of Destroying your DB Daily) by Chris Waters

_______________________________________________________________________________________________

Cloud 66 - Pain Free Rails Deployments
Cloud 66 for Rails acts like your in-house DevOps team to build, deploy and maintain your Rails applications on any cloud or server.

Get $100 Cloud 66 Free Credits with the code: RailsConf-19
($100 Cloud 66 Free Credits, for the new user only, valid till 31st December 2019)

Link to the website: https://cloud66.com/rails?utm_source=-&utm_medium=-&utm_campaign=RailsConf19
Link to sign up: https://app.cloud66.com/users/sign_in?utm_source=-&utm_medium=-&utm_campaign=RailsConf19
_______________________________________________________________________________________________

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.

Ready to reset with me?

RailsConf 2019

00:00:20.689 Thank you very much for coming. Before we start, I guess full disclosure, this is my first RailsConf.
00:00:26.189 So if I do come up to you looking pretty excited, that's because I am. We flew out on Monday from London and I'm flying home on Friday.
00:00:36.960 It's just been a tremendous privilege to be here to meet some people that I've looked at from afar, perhaps followed on Twitter, and yeah, it's been a real thrill.
00:00:44.960 The other disclosure is that this is actually my first ever talk. I've never done a tech talk before.
00:00:51.210 So I really appreciate you all coming, but more importantly, thank you if you also bother to stay.
00:00:57.559 As a side note, if you've never done a talk before, can I recommend that you apply to do this next year? Because the process has been incredibly easy.
00:01:06.840 There's none of that crazy stuff about sending your slides three years in advance or any of that; it's just been really relaxed and friendly.
00:01:19.890 If you've never done it before, please consider it. Everyone has been super lovely.
00:01:26.039 I'm Chris, so Chris the sole on pretty much all of the internet, well, all the internet things that you can sign up for because this name is always available.
00:01:38.390 I come from a lovely little county called Suffolk in the UK. If you know where Suffolk is, great! If not, let me describe it. There's a handy saying that the UK looks like a witch riding a pig.
00:01:51.880 You can decide which part of the pig you think Suffolk is. However, the good news is that Suffolk doesn't actually look like that.
00:02:04.860 This here is Southwold in Suffolk, and these colorful things are called beach huts.
00:02:13.560 If you thought bricks were a terrible idea, then you should see how much people will pay for one of these things. It's a staggering amount of money.
00:02:26.250 A lot of people decide to visit Suffolk because it's a beautiful area. We have quite a few hotels, campsites, B&Bs, and cottages.
00:02:39.120 I used to work at a consultancy business where we built websites, and all our clients wanted booking systems. We initially thought there had to be something out there, but they said there wasn't.
00:02:58.920 So about eight years ago, we started a company called Staylists, offering online booking for hotels and B&Bs.
00:03:09.270 That system is built on Rails, which I guess makes it a monolith.
00:03:16.590 We have about 300 models, approximately 40,000 commits to the codebase, and 72 contributors. And I'm curious, how many of you think 72 sounds like a lot?
00:03:36.780 The interesting thing is that I'm the only full-time developer we've had for a long time, which makes me the CTO.
00:03:44.100 We worked mostly with contractors, many of whom were consultants from a company in Poland called Debt Guru, with whom we've worked for about six years.
00:03:59.700 In our setup, we have rotations where we get developers for six to nine months, and we take some junior developers on for maybe six weeks.
00:04:08.970 This means we constantly have new people coming in and out, and we need to get them up to speed quickly so they can tackle the work.
00:04:19.830 One nice thing is that when people leave, they always say nice things. They often remark that "Chris knows everything about this codebase. If you need anything, just ask Chris." That feels pretty good.
00:04:31.390 However, this inevitably leads to an important realization: I’m not really the Oracle or the linchpin; I'm just the bottleneck.
00:04:39.480 I realized I made a mistake by not spreading that knowledge earlier, and that's a problem I needed to solve.
00:04:52.900 People have had bottlenecks in business before, and I thought about how to alleviate that.
00:05:02.280 I started looking into other areas beyond just programming because my background is more on the creative and product side.
00:05:13.860 I found inspiration from an interesting figure: Walter, a grocery store owner from St. Paul, Minnesota, who observed that customers only bought as much as they could carry.
00:05:23.610 So, he invented the paper bag, which transformed grocery shopping. It was lightweight and repeatable, and he didn't need to follow customers home.
00:05:37.470 There's something similar in Rails that solved my problem: it's called rake DB reset.
00:05:49.950 This talk is about rake DB reset. I want to convince you that it's a good idea. How many of you have your computers with you today?
00:06:00.990 How many of you have a local environment set up?
00:06:06.570 How many of you are prepared to open up a Rails app and run rake db:reset? Quite a few of you.
00:06:14.520 Anyone not prepared to do that? Okay.
00:06:19.320 I'm going to persuade you that you should want to do this. What exactly happens when you run rake db:reset?
00:06:31.860 Essentially, rake db:reset does two things: it drops the database and then runs db:seed.
00:06:38.280 The seeding part is what I want to focus on because it can reduce dependency on you, especially if you’re the one who knows how all these objects interact.
00:06:44.760 So, what happens when I run rake db:seed? There's a module called database tasks that runs load_seed.
00:06:52.290 If we dive into that, we see that load_seed is delegated to the seed loader.
00:06:58.740 The seed loader is part of the Rails application, which finds the db/seeds.rb file and loads it.
00:07:06.030 In essence, rake db:reset drops the database, loads the schema, and then seeds the database.
00:07:12.300 Does everyone have a db/seeds.rb file? Who here does not have a db/seeds.rb file in their Rails application?
00:07:24.090 Now, what about creating seeds? We know it’s just a Ruby file, and we can use Active Record to create stuff.
00:07:36.060 I prefer using 'create!' because I want it to fail loudly if something goes wrong.
00:07:43.860 A few tricks you might not know about: the create method can take an array of hashes.
00:07:51.000 Instead of thinking of names for a large number of records, you can use gems like 'faker' to help.
00:08:01.140 Faker provides a ridiculous amount of fake data, making it interesting for testing different scenarios.
00:08:12.210 Overall, we're seeding a monolith. We have a lot of models, so the seeds file can get quite big.
00:08:20.460 To keep it manageable, we choose to extract our seeds into separate classes.
00:08:31.020 We organize our seeds to improve readability and maintainability, only requiring the relevant files in our seeds.
00:08:40.320 Additionally, we discovered a nifty trick: you can run 'rake db:seed' while specifying a particular seed file using an environment variable.
00:08:52.350 This makes it easy to run specific seed files while working on particular segments of your application.
00:09:01.890 The interesting part is whether you can run rake db:seeds without a DB seeds file.
00:09:09.870 We found that if the seed loader is nil, we can set it to a class instance.
00:09:18.240 What it allows us to do is to change the seed loader dynamically and delete the seeds file if necessary.
00:09:29.940 This feature leads to a more flexible implementation of seeds across different environments.
00:09:41.610 The main takeaway was that adding this seed class provided us with an instant and consistent local environment.
00:09:52.920 No more slack calls or wasting time explaining the same thing over and over again.
00:10:04.590 What we're actually doing now is allowing developers to engage more effectively with the system.
00:10:16.620 It fosters a productive environment where they can reproduce the conditions they need to do their work.
00:10:28.590 Overall, the significant benefit is that you can reset your local environment whenever you need to.
00:10:42.000 In doing so, we've also created a resilient local environment that helps ensure a smoother workflow.
00:10:55.140 The joy of being able to reset daily leads to a more prosperous development experience.
00:11:07.230 When everyone contributes to the seeds file, we have access to more contextual data to work with.
00:11:18.030 The process of creating a new model now includes adding seeds, allowing everyone to build together.
00:11:30.090 In conclusion, I was particularly impressed with the speed we could develop after this change.
00:11:41.520 It’s practical creating a staging environment where we can run seed data, improving communication across teams.
00:11:52.590 Now QA teams can understand what they’re looking at without setting it up themselves every time.
00:12:11.490 Overall, this simplification contributes to a streamlined process that benefits all the developers involved.
00:12:22.590 I urge you to consider resetting yourself free. Thank you very much for listening.
00:12:34.960 Does anybody have any questions?
00:12:40.270 That's a great question. For external data sources like Stripe, we actually use a dummy account.
00:12:45.960 This allows us to connect through the application and seed the relevant data.
00:12:51.320 We put any necessary keys directly into the seed file instead of going through complicated setups.
00:13:01.540 To keep things organized, we place our seeds in a subfolder called seeds and name them according to the model.
00:13:12.210 This has allowed us to manage our 300 models effectively.
00:13:23.030 When it comes to handling relationships, we load the parent object first to ensure proper associations.
00:13:35.170 If we have multiple files for related data, we just create them and relate them accordingly at the top of the seed file.
00:13:47.050 We've noticed some people worry about seeds becoming stale. Honestly, we're still in the honeymoon phase with this implementation.
00:14:04.650 In the future, there could be challenges, but right now, it feels efficient.
00:14:23.220 We even wrote a small utility class to help manage relationships and keep our seeding fun.
00:14:36.300 We also found joy in communicating about what we were doing in the console while handling seeds.
00:14:48.490 To sum up, we did originally explore gems for seeding but decided to keep it simple with our own classes.
00:15:00.490 We develop organically, and that worked out well for us.
00:15:15.750 We don’t currently use seeds in our tests, though that could change in the future.
00:15:28.520 But as it stands, we generate dummy data to facilitate our testing processes.
00:15:39.480 I can always share insights on how we structure our seeding if there’s interest.
00:15:49.520 If you want, I'm happy to discuss what we've implemented or contribute to discussions about this.
00:15:58.290 Thank you all once again, I'm Chris. You can easily find me due to my shirt. Have a wonderful time!
Explore all talks recorded at RailsConf 2019
+102