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

fix: disallow AlterColumn method chaining #468

Merged

Conversation

soanvig
Copy link
Contributor

@soanvig soanvig commented May 8, 2023

Fixes: #467

It was slightly tricky, but I decided that "unwinding" looks the best in terms of clarity of what is happening.

This effectively produces query:

ALTER TABLE "x" ALTER COLUMN "y" SET DEFAULT true, ALTER COLUMN "z" DROP NOT NULL, [...]

as in Postgres docs. MySQL has exact syntax for chaining ALTER COLUMN, but supports only drop default and set default operations, so probably nobody will chain those two together, plus alter column usage for MySQL is not properly handled in whole Kysely anyway. I tested MySQL case manually.


EDIT: now it just disallows chaining

@vercel
Copy link

vercel bot commented May 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
kysely ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 28, 2023 9:06am

@igalklebanov
Copy link
Member

igalklebanov commented May 8, 2023

Hey 👋

Thank you! ❤️

I understand what you were going for with this and it does seem convenient, but it is not aligned with Kysely's WYSIWYG design principle. Not sure about these changes.

We should consider either:

  1. each method returns a builder that cannot chain further and .alterColumn expects it as argument. So the original issue is blocked by more precise API.

  2. make compiler just add a whitespace between each operation, and keep it a single alter column clause. This'll cause a runtime error obviously with PostgreSQL/MySQL - but consumer should know SQL. This might provide future-proofing in case some dialect supports this.

I'm leaning towards 1.

@koskimas WDYT?

@igalklebanov
Copy link
Member

plus alter column usage for MySQL is not properly handled in whole Kysely anyway. I tested MySQL case manually.

Can you explain this? does it require an issue?

@soanvig
Copy link
Contributor Author

soanvig commented May 8, 2023

@igalklebanov that's rather new to me that Kysely pushes WYSIWYG to such extent.
I can wait for other people decision on that topic, sure.


Can you explain this? does it require an issue?

No. I was just saying, that you can call AlterTableBuilder.dropNotNull even if it is not supported in MySQL (and you will get runtime error)

@igalklebanov
Copy link
Member

No. I was just saying, that you can call AlterTableBuilder.dropNotNull even if it is not supported in MySQL (and you will get runtime error)

Oh, gotcha. The API is dialect agnostic by design.

@igalklebanov igalklebanov added mysql Related to MySQL postgres Related to PostgreSQL labels May 8, 2023
@koskimas
Copy link
Member

koskimas commented May 8, 2023

each method returns a builder that cannot chain further and .alterColumn expects it as argument. So the original issue is blocked by more precise API.

@igalklebanov Yes, I agree. This is the way to proceed.

@igalklebanov igalklebanov added bug Something isn't working api Related to library's API labels May 8, 2023
@soanvig
Copy link
Contributor Author

soanvig commented May 8, 2023

@koskimas @igalklebanov
So for fun I'll implement that part, and we'll see. I really like this library, and we heavily use it at work, so I'm glad to contribute something.

@soanvig
Copy link
Contributor Author

soanvig commented May 12, 2023

image

image

@soanvig
Copy link
Contributor Author

soanvig commented May 12, 2023

@igalklebanov

I updated the PR. Above you can see intelisense suggestion (type compilation result).
Besides restricting the result, I also narrowed API of AlterColumnNode, so it cannot contain more than one operation at once (since it won't be handled properly anyway): I removed cloneWith, because it was not needed, and creating the node now requires passing the property to be set.

@soanvig soanvig changed the title fix: AlterColumn now properly applies all operations fix: disallow AlterColumn method chaining May 12, 2023
Copy link
Member

@igalklebanov igalklebanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 💪

Left a few minor suggestions..

src/operation-node/alter-column-node.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
src/schema/alter-column-builder.ts Outdated Show resolved Hide resolved
@soanvig
Copy link
Contributor Author

soanvig commented May 16, 2023

@igalklebanov thanks for the suggestions. I've implemented them all in one commit.

Copy link
Member

@igalklebanov igalklebanov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🚀

@koskimas koskimas force-pushed the fix-alter-column-multiple-operations branch from 13b4ca4 to 1235cc2 Compare May 28, 2023 09:05
@koskimas koskimas merged commit 7ba8ccd into kysely-org:master May 28, 2023
5 checks passed
@soanvig soanvig deleted the fix-alter-column-multiple-operations branch May 30, 2023 09:29
Gaspero pushed a commit to Gaspero/kysely that referenced this pull request Oct 2, 2023
* fix: disallow chaining AlterColumnBuilder operations (kysely-org#467)

* chore: limit AlterColumnNode.create available params, change AlterColumnBuilder fields names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Related to library's API bug Something isn't working mysql Related to MySQL postgres Related to PostgreSQL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AlteredColumnBuilder incorrectly joins operations
3 participants