Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Initial migration should use IF NOT EXISTS #409

Open
jschuur opened this issue May 15, 2024 · 5 comments
Open

[BUG] Initial migration should use IF NOT EXISTS #409

jschuur opened this issue May 15, 2024 · 5 comments

Comments

@jschuur
Copy link

jschuur commented May 15, 2024

The initial migration file generated with drizzle-kit generate for an existing project seemed to use a CREATE TABLE table_name (..) format, rather than
CREATE TABLE IF NOT EXISTS table_name (...).

At least this was the case for me for a turso/sqlite driver/dialect config and drizzle-kit: v0.21.2.

drizzle-kit introspect also did not add IF NOT EXISTS.

Shouldn't IF NOT EXISTS be use here, so that it can gracefully skip existing tables? The same should apply to index generating statements.

@jschuur jschuur changed the title Breakpoints in migration don't work with Turso, initial migration should use IF NOT EXISTS [BUG] Breakpoints in migration don't work with Turso, initial migration should use IF NOT EXISTS May 15, 2024
@jschuur jschuur changed the title [BUG] Breakpoints in migration don't work with Turso, initial migration should use IF NOT EXISTS [BUG] Initial migration should use IF NOT EXISTS May 23, 2024
@migsdeving
Copy link

Same problem here, what are the necessary steps you need to take if you want to start using migrations after having used push for a while ?
I tried uncommenting, that sql statement and running the migration, deleting it, generating a migration and running it (fails because it looks for that previous sql file I deleted)

@jschuur
Copy link
Author

jschuur commented Jun 17, 2024

Same problem here, what are the necessary steps you need to take if you want to start using migrations after having used push for a while ?

If I recall correctly, I ended up manually adding IF NOT EXISTS to all the CREATE INDEX an CREATE TABLE statements.

So...

CREATE TABLE IF NOT EXISTS `user` ( ...schema here... );
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `Account_userId_index` ON `account` (`userId`);--> statement-breakpoint

This avoids an error for existing database tables but still sets it up for new instances.

The --> statement-breakpoint bits are really just there for Drizzle to know how to split up the SQL statements when you run drizzle-kit migrate (see here), so they wouldn't run directly in a SQL client.

But yeah... this is clearly not ideal and will confuse people :)

@migsdeving
Copy link

Same problem here, what are the necessary steps you need to take if you want to start using migrations after having used push for a while ?

If I recall correctly, I ended up manually adding IF NOT EXISTS to all the CREATE INDEX an CREATE TABLE statements.

So...

CREATE TABLE IF NOT EXISTS `user` ( ...schema here... );
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `Account_userId_index` ON `account` (`userId`);--> statement-breakpoint

This avoids an error for existing database tables but still sets it up for new instances.

The --> statement-breakpoint bits are really just there for Drizzle to know how to split up the SQL statements when you run drizzle-kit migrate (see here), so they wouldn't run directly in a SQL client.

But yeah... this is clearly not ideal and will confuse people :)

Yeah if I add IF NOT EXISTS I'm sure it will work, but I'm wondering what the reason is for drizzle not adding it automatically ? Also seems to be a bit of confusion on how to work with introspect and generate. If I introspect first and then generate, drizzle correctly generates the migration file only for the new tables, but when I migrate, it tries to run the instrospect generated file, and not the new one + doesn't prompt you on which one you want to run and ends up failing because the instrospect migration file is commented

@jschuur
Copy link
Author

jschuur commented Jun 17, 2024

There's a hint in a tweet from yesterday by @AndriiSherman to 'a big change to the generate+migrate flow'.

He just replied 'yes' that it will address the stuff mentioned here.

@migsdeving
Copy link

There's a hint in a tweet from yesterday by @AndriiSherman to 'a big change to the generate+migrate flow'.

He just replied 'yes' that it will address the stuff mentioned here.

Well it does need a significant refactor. Hopefully it will be coming soon though

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants