Skip to content

Commit

Permalink
add logs about seeding production databases
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed Aug 4, 2023
1 parent 3858fb8 commit eac6a1e
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,46 @@ you can utilize Fly's `ssh` and `proxy` commands.
To work with Prisma Studio and your deployed app's database, simply open
`http://localhost:5556` in your browser.

## Seeding Production

Let's say you're building an application that allows users to lookup the nearest
city with a certain population threshold from their geographic location, you'll
need to have a database of cities with their population and geographic
coordinates. You could manually enter this data into your database, but that
would be tedious and error prone. Instead, you can write a script that will
populate your database with the data you need.
The easiest way to seed production data is to use the `sqlite3` command line
tool. You can use raw SQL, or write a script which can be committed to the repo,
generate the database file you like (all while working locally), and then copy
it to your production environment.
It's surprisingly straightforward to do production DB seeding with SQLite 😅

If you have existing data in your production database and you'd like to seed it
with more data, then it's a tiny bit more involved.

1. Backup your production database (lol).
1. Create a new database file (locally) with the data you want to seed.
1. Create a "dump" of the seed database using the `sqlite3` command line tool.
```sh nonumber
sqlite3 seed.db .dump > seed.sql
```
1. Copy the `seed.sql` file to your production volume next to your database (via
`fly sftp`)
1. SSH into your production server and run the following command:
```sh nonumber
sqlite3 data.db < seed.sql
```
1. Verify that your production database has been seeded correctly. If it hasn't,
then restore your backup (asap).
> **Warning**: You may need to adjust some of the SQL generated by the `.dump`
> command to allow you to update the database without issue. It will have
> `CREATE TABLE` commands which should include `IF NOT EXISTS`, but the
> `CREATE UNIQUE INDEX` commands will not. You'll need to add `IF NOT EXISTS` to
> those commands manually, or remove them entirely.

## Deploying locally

If you'd like to deploy locally you definitely can. You need to (temporarily)
Expand Down

0 comments on commit eac6a1e

Please sign in to comment.