joschan21 / drizzle-planetscale-starter

NextJS 13 Starter Kit for Planetscale & DrizzleORM
198 stars 18 forks source link

Add drizzle-kit push:mysql script #1

Closed AndriiSherman closed 1 year ago

AndriiSherman commented 1 year ago

This is an alternative way to run your migrations on PlanetScale without using any migration files and tracking table in your database.


Drizzle Kit itself should be used only locally and not pushed to a bundle to be deployed. It means you can exclude drizzle.config.ts from being bundled and can use any dependencies you need (even those that are not edge-compatible).

Changes

In this PR, you can see 2 changes:

  1. Add a script to package.json.
  2. Import .env variables to drizzle.config.ts. As long as it's a separate process of creating and pushing the migrations, it has nothing to do with other project setups like Next or any other library.

Usage explanation

  1. Anytime you change your schema in ts code and are ready to push it to your dev/feature branches on PlanetScale, run the db:push script from package.json. It will sync your schema with the database and warn if any data-loss actions will be applied.
  2. When you are ready with a feature, deploy your code and invoke branch merging on the PlanetScale side so the database will be in sync with your code.

Useful tips

If you want to exclude some tables from syncing with your database, you can use the filters option in the Drizzle Kit config. Refer to this link for more information: https://orm.drizzle.team/kit-docs/conf#multi-project-schema

In this case, you can exclude some tables from being deleted while syncing the schema with your code.