drizzle-team / drizzle-team/drizzle-orm

[FEATURE]: Avoid alter statements in drizzle-kit export

Open
#3,769 0 comments 0 reactions 0 assignees View on GitHub
drizzle/kit enhancement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Feature hasn't been suggested before.

- [X] I have verified this feature I'm about to request hasn't been suggested before.

### Describe the enhancement you want to request

**Description:**

I am currently using Drizzle as my TypeScript ORM for SQL databases, and I am looking to integrate it with schemadiff, a CLI utility for generating formal database migrations for MySQL. However, I have encountered an issue: Drizzle’s `export` feature generates schema output that includes `ALTER` statements, but schemadiff does not support `ALTER` statements. This incompatibility prevents me from using the export output directly with schemadiff to generate migrations.

**Proposed Solution:**

Either avoid using `ALTER` statements altogether in `export` or add a configuration option in `drizzle.config` that controls whether `ALTER` statements could be used in the exported schema.

From what I could see `ALTER` statements are only used for foreign keys, example before and after:
```sql
-- before
CREATE TABLE `session` (
`id` varchar(255) NOT NULL,
`user_id` int unsigned NOT NULL,
`expires_at` datetime NOT NULL,
`verified` boolean NOT NULL,
CONSTRAINT `session_id_pk` PRIMARY KEY(`id`)
);

CREATE TABLE `user` (
`id` int unsigned AUTO_INCREMENT NOT NULL,
`email` varchar(255) NOT NULL,
`password_hash` varchar(97) NOT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
CONSTRAINT `user_id_pk` PRIMARY KEY(`id`),
CONSTRAINT `email_unique` UNIQUE(`email`)
);

ALTER TABLE `session` ADD CONSTRAINT `session_user_id_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE cascade ON UPDATE no action;

-- after
CREATE TABLE session (
`id` varchar(255) NOT NULL,
`user_id` int unsigned NOT NULL,
`expires_at` datetime NOT NULL,
`verified` boolean NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `session_user_id_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
);

CREATE TABLE user (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(255) NOT NULL,
`password_hash` varchar(97) NOT NULL,
`name` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `email_unique` (`email`)
);
```

**Why This is Beneficial:**

- **Mostly Reliability:** schemadiff generates migrations in a formal and predictable manner, and it is developed by Vitess. By integrating Drizzle with schemadiff, I can take advantage of these reliable migration capabilities.

**Thank you for considering this request!**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.