Loading
Current section: Seeding Data 4 exercises
lesson

Intro to Seeding Data

Transcript

00:00 All right, let's let it grow with seeding data. So manual creation of data inside of Prisma Studio, not fun, I don't wanna do that. Whenever I mess up data, I've gotta like reset it. That would be so, so annoying. So instead, we automate stuff because we're software engineers. We're gonna make this automatic.

00:19 And so that is what seeding is. It's just a script that will create data in the database, deletes it and then creates a new, anytime you need to get that new data. And then one other cool thing about doing that is you can actually set the record IDs. And so for example, in our app, we have slash user slash notes,

00:38 or like user ID slash note. It's actually username, not user ID, then slash notes and then slash note ID. And so if I'm sitting on that note ID and I'm working on that, and then I'm like, oh, I need to reseed the database, reseed, and then I have to go find the new note ID that was generated. So the other nice thing about seeding data

00:55 is you can actually seed it with some specific ID. So you don't have to go and find that stuff every single time, super annoying. So that's nice. Production also has some needs for seeding data sometimes. So imagine that you're making a site that allows people to find things relatively close to them,

01:14 like what's the biggest city next to me or whatever. So you're gonna have some data that isn't really editable by users. It's like just part of the application data. And so like not something that your users are gonna be creating over time necessarily. And so that's some data that you need to start the app out with. It's like city data.

01:34 And so the process for that, especially with SQLite is actually pretty straightforward. First off, if you have existing data, make sure to back it up. Otherwise you could be in a bad place. And then you're gonna create a new database file locally with all the data that you want to have in the production database.

01:54 And then you create a dump file. This will be what I call your seed.sql using the dump command from SQLite 3. That will generate all the SQL necessary to create that data in the database. This could be very large depending on how much data that is, and that's fine. Then you copy that seed file to production.

02:13 Now, if you do have existing data, you may need to make some changes to that SQL file to do if not exists on like creating indexes and stuff like that. I believe it does do that for tables, but for some reason it doesn't do that for indexes. So you'll want to make sure that you can actually execute this

02:31 against a database with data in it that already has the tables, already has the indexes maybe, for example. But yeah, so maybe finesse the SQL just a little bit and then copy it over to production. And then you run it on production using SQLite 3 and then verify that it is what you want it to be.

02:51 And if it's not, then restore from your backup really quick and then figure out what you did wrong. So this would be like a pretty simple way to seed a production database. You don't, I suppose you do actually seed databases over time, you add a new feature and it requires some data. So you're gonna be seeding at that time.

03:11 And so you'll definitely want to test this out in a staging environment first to make sure that you don't mess things up in that sort of a scenario. So with that, I think you're ready to create a seed script. We're going to be deleting all of our users

03:27 and creating a new user and new notes and images and all that stuff in our seed script. And that way we can just run this anytime we want to get fresh data. So have a good time with that and we'll see you when you're done.