Skip to content

feat: add queryInterface.changeColumns, fix many bugs with changeColumn - #14687

Draft
ephys wants to merge 33 commits into
mainfrom
ephys/rewrite-changeColumn
Draft

ephys wants to merge 33 commits into
mainfrom
ephys/rewrite-changeColumn

Conversation

@ephys

@ephys ephys commented Jun 26, 2022 •

Copy link
Copy Markdown
Member

Description Of Change

Closes #14477
Closes #15522
Closes #7649
Closes #15285
Closes #9635
Closes #13167

The initial goal of this PR was to investigate a bug reported here, it quickly turned out that changeColumn had a completely broken implementation and I opted for doing a complete rewrite and fixing many bugs in the process.

changeColumns

I've added changeColumns which is able to change more than one column from the same table in one ALTER TABLE query, reducing the number of necessary queries.

changeColumns follows our new API design, meaning it's possible to use it with a Model, a table name, a table object (table+schema), and it will default to the schema defined in the sequelize options if none is specified.

That method also throws clear error messages when the user tries to do something out of its scope. Previously, it would just crash with an obscure message.

changeColumn just defers to changeColumns, and inherits from its features.

All dialects

  • changeColumns only changes what has been provided. No more dropping the default value because it was not provided in the call. Instead dropDefaultValue has been added as an option. Similarly, the nullability of the column is only changed if allowNull is explicitly set to true or false (undefined just does not change the definition at all).
  • The previous implementation was broken because it used regexes to modify another query, which broke very easily by using keywords in the default value.
  • This method supports enabling/disabling autoIncrement & autoIncrementIdentity
  • Model.getTableName() returns a normalized table name object, including a normalized schema. This makes the rest of the implementation much easier as we can just normalize a table name at the start of each queryGenerator methods.

Postgres

  • changeColumns is able to replace an enum with another, drastically simplifying the modification of enums in migrations. Before, it was impossible to use changeColumn to change the definition of a column's enum.

Commit message


BREAKING CHANGE: `changeColumn` does not change a property if it is not specified. For instance, previously it would default `allowNull` to true and add `DROP NOT NULL` if unspecified.
postgres: `changeColumn` can replace a column's enum, as long as only that column is using that enum (which is the default behavior for tables generated by Sequelize).
`changeColumn` does not break if a SQL keyword is used in the default message anymore.

Todos

@ephys ephys self-assigned this Jun 26, 2022
@ephys ephys added type: bug DEPRECATED: replace with the "bug" issue type type: feature DEPRECATED: replace with the "feature" issue type labels Jun 26, 2022
@WikiRik

WikiRik commented Jun 26, 2022

Copy link
Copy Markdown
Member

Does this replace #14477?

@ephys

ephys commented Jun 27, 2022

Copy link
Copy Markdown
Member Author

Yep! It does fix the issue I mentioned but still checks whether a partially built query, that can include user-provided values (through defaultValue) contains some keywords, meaning the query will break if the defaultValue option includes one of those keywords

I'll merge the necessary changes from that PR into this one

@ephys ephys mentioned this pull request Jun 27, 2022
6 of 8 tasks
@WikiRik

WikiRik commented Dec 1, 2022 •

Copy link
Copy Markdown
Member

I've been giving this some thought after the monthly and I think it would be nice to include this in a v7 alpha after all. Possibly a v7 beta but I'd like to minimize the number of breaking changes in the beta(s) as much as possible

Do you agree?

@WikiRik

WikiRik commented Feb 11, 2024

Copy link
Copy Markdown
Member

Just to check; this is still blocked by describeTable, correct?

Any chance parts can be done already? Like migrating the enum functions to TS.

@ephys

ephys commented Feb 11, 2024

Copy link
Copy Markdown
Member Author

Yep! I already introduced the internal query generator files & other similar refactors in a recent PR, the canHaveDefaultValue logic could be moved too, as well as the changes to withUsageContext, getForeignKeyReferencesForTable. The change to Nullish can also be done. Maybe the enum functions but I'm not sure. At least some of them can. Accepting DbObjectId in Identifier and other places would be great too, at sql.identifier could then directly specify the schema. Not using .field or .fieldName in our source code to remove deprecation warnings would be great too.

I added MapView#getOrThrow in #17077

Those would already be pretty great. As for the rest, it's indeed mostly blocked by describeTable

@ephys
ephys requested a review from a team February 12, 2024 13:39
@ephys

ephys commented Feb 12, 2024 •

Copy link
Copy Markdown
Member Author

(ignore notification, was demoing something, sorry)

@lohart13

Copy link
Copy Markdown
Contributor

Just wondering what the status of this PR is?

I notice the last comments mention the issues with describeTable but was wondering if some of the changes such as the datatype ones (especially the ENUM's for Postgres) extracted into a separate PR?

I'm working on a addColumn rewrite some of the changes in the PR would greatly help.

@WikiRik

WikiRik commented Oct 18, 2024

Copy link
Copy Markdown
Member

Just wondering what the status of this PR is?

It's on hold. I think this will land somewhere later in v7 as an experimental feature and becomes the default in v8.

I notice the last comments mention the issues with describeTable but was wondering if some of the changes such as the datatype ones (especially the ENUM's for Postgres) extracted into a separate PR?

As far as I know, those have not been extracted yet but feel free to do so.

@ephys ephys removed type: bug DEPRECATED: replace with the "bug" issue type type: feature DEPRECATED: replace with the "feature" issue type labels Mar 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Doing (any)

3 participants