drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Drizzle migration dont create data base if not existed in postgres

Open lord007tn opened this issue 1 year ago • 1 comments

What version of drizzle-orm are you using?

0.32.2

What version of drizzle-kit are you using?

0.23.2

Describe the Bug

not sure if its a bug or a feature

When starting a migration and running drizzle-kit migrate if database not existed locally it rise an error.

Expected behavior

it should create the database if not existed like prisma do ( i came from prisma background )

Environment & setup

System: Windows 11 Bun: 1.1.21

lord007tn avatar Aug 08 '24 22:08 lord007tn

I believe this is intentional, the migration only do the table schema migration, however someone from drizzle team probably could clarify, anyway, as a workaround we have our custom script for migration and add it to the package.json scripts, when the script is executed, it first check if the database exists or not, if not it create the database otherwise do nothing, and then additionally the script ensuring the extensions are created CREATE EXTENSION IF NOT EXISTS ...; and after that finally the actual migration script got executed and run the migrate function from drizzle migrator

deanrih avatar Aug 09 '24 12:08 deanrih

This is not a bug, this is intentional. The developer is responsible for the creation of the database.

L-Mario564 avatar Oct 24 '24 16:10 L-Mario564

IMHO, developer should be responsible for standing up a DB server for certain... but the DB itself is debatable IMHO. In Dev mode, we tear down and redeploy fresh constantly. In Prod mode, we definitely want safeties. Would be real nice to have the 0000 migration default to CREATE DB blah blah. Remember, the hope is that 10 million people use your software... but in the end its only purpose is doing work for them and saving them time... one has to look at every crevasse, every benefit danger and drawback... great thing about software, everyone can be made happy... the only real challenge is organization... IMHO.

gunslingor avatar Oct 28 '24 18:10 gunslingor

This is not a bug, this is intentional. The developer is responsible for the creation of the database.

With such attitude a developer should not use migrator at all. He should do everything manually.

tvolodi avatar Dec 04 '24 04:12 tvolodi

This is not a bug, this is intentional. The developer is responsible for the creation of the database.

With such attitude a developer should not use migrator at all. He should do everything manually.

But I guess the problem is handling database directly is quite involved, like managing extensions to install as well as other thing, so it is quite personalized, in our case we make some helper scripts that runs before every migration that checks if a database exists and what additional parameter being given to the script, if it doesn't exists, it will just create the database with all the required extension we need, but if it does exists, it won't touch it and go straight into the migration part, unless something like --force is passed an the DB is destroyed and re-built before doing migration

Maybe drizzle could give some middle ground, a good default DB migration but can be extended for specific use case

deanrih avatar Dec 04 '24 08:12 deanrih

Prisma creates the database though

SwapnilSoni1999 avatar Jul 03 '25 21:07 SwapnilSoni1999

then add a flag to create db ... I don't see why not

mustafamagdy avatar Jul 18 '25 20:07 mustafamagdy