drizzle-team / drizzle-team/drizzle-orm

[BUG]: drizzle-kit push --force / pushSchema require manual input when adding unique constraints

Open
#4,531 6 comments 6 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] I have verified that the bug I'm about to report hasn't been filed before.

### What version of `drizzle-orm` are you using?

0.43.1

### What version of `drizzle-kit` are you using?

0.31.1

### Other packages

_No response_

### Describe the Bug

In drizzle-kit, `drizzle-kit push` (and the API version [pushSchema](https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-kit/src/api.ts#L109)) are supposed to be APIs so they shouldn’t require human command line input, but in its call to [pgSuggestions](https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-kit/src/cli/commands/pgPushUtils.ts#L228-L237) (and a very similar helper function for `pushSchema`: [pgSuggestions](https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-kit/src/api.ts#L160)):

```typescript
const { shouldAskForApprove, statementsToExecute, infoToPrint } = await pgSuggestions(db, statements);
```

the function is asking for human input when we are adding a unique constraint:

```typescript
...
} else if (statement.type === 'create_unique_constraint') {
const res = await db.query(
`select count(*) as count from ${
tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
}`,
);
const count = Number(res[0].count);
if (count > 0) {
const unsquashedUnique = PgSquasher.unsquashUnique(statement.data);
console.log(
`· You're about to add ${
chalk.underline(
unsquashedUnique.name,
)
} unique constraint to the table, which contains ${count} items. If this statement fails, you will receive an error from the database. Do you want to truncate ${
chalk.underline(
statement.tableName,
)
} table?\n`,
);
const { status, data } = await render(
new Select(['No, add the constraint without truncating the table', `Yes, truncate the table`]),
);
if (data?.index === 1) {
tablesToTruncate.push(statement.tableName);
statementsToExecute.push(
`truncate table ${
tableNameWithSchemaFrom(statement.schema, statement.tableName, renamedSchemas, renamedTables)
} cascade;`,
);
shouldAskForApprove = true;
}
}
}
```

This defeats the whole purpose of `pushSchema` as an API call (humans would just use the CLI directly) and makes it unusable in CI/CD.

Expected behavior: this should not be a manual user input.

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.