Pull schema changes without using drizzle migrations
Hey all, we are currently using Postgres and have migrations running through Flyway. I want to just use Drizzle as a query builder essentially, but to do that I need to keep my schema up-to-date with external migrations.
I see the introspect command, but that seems to completely re-write the schema (where there are errors that I have to manually fix).
Is there a better way to just "pull" changes if need-be and only alter tables that differ while using external migrations?
1 Reply
Hi, I just recently posted about a similar setup I'm using with flyway in the #kit-discussion channel. I'm currently continuing to use flyway to handle my migrations and have a Makefile that I use to run the db introspection after my flyway migrations run to update the drizzle types. I'm treating the generated introspection files as read-only and only update my sql in the flyway migrations. If you just have one schema in your database like
public
it should be pretty simple, if you're using multiple database schemas it can be a little more of a setup. I use multiple schemas and had to add a few adjustments to make it work, so for the example below let's say we have two database schemas, public
and otherSchema
. This setup allows you to have 1 or many schemas under the same database even though drizzle-kit doesn't support that out of the box well. This setup keeps schemas in their own namespace so they can even share same table names.
Here's my Makefile
that I use to run flyway migrations, then I run the drizzle db introspection to update the models, and then a few commands to clean up the drizzle migrations folders that I don't need as well as fix some of the errors drizzle-kit has on parsing.
So with this setup, I just run make migrate-db
after I change some sql in the flyway migration files, and it flows down to my app and keeps the drizzle schema types updated. You can add more sed
commands to fix any other types of parsing errors that come out of introspection.
Makefile
package.json
docker-compose.yml
My app directory structure is the following:
/repo-name/apps/ts-app/src/clients/postgres/public/drizzle.config.ts
/repo-name/apps/ts-app/src/clients/postgres/otherSchema/drizzle.config.ts
/repo-name/apps/ts-app/src/clients/postgres/index.ts