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: Fix unnecessary migrations for timestamps with default values of CURRENT_TIMESTAMP and precision #7229

Closed

Conversation

RohanTalip
Copy link
Contributor

@RohanTalip RohanTalip commented Dec 30, 2020

Description of change

After upgrading typeorm from 0.2.12 to 0.2.29, I started seeing COMMENT ON COLUMN "tableName"."columnName" IS NULL statements in both the up and down migrations for columns that had default values but no changes.

I saw that issue #3991 looked similar, so I fixed it. I have a separate fix for the comment issue that I will send in another pull request.

Details:

  • Returned the database specific equivalent of CURRENT_TIMESTAMP in normalizeDefault().
  • Pulled out individual conditions for easier debugging.
  • Intentionally used '!=' instead of '!==' for the precision comparison, so that undefined and null compare as equal.

Fixes #3991

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run lint passes with this change
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change - N/A
  • The new commits follow conventions explained in CONTRIBUTING.md

…recision

- Returned the database specific equivalent of CURRENT_TIMESTAMP in normalizeDefault()
- Pulled out individual conditions for easier debugging.
- Intentionally used '!=' instead of '!==' for the precision comparison, so that undefined and null compare as equal.
…d normalizeDefault() that were useful for debugging.
@RohanTalip RohanTalip changed the title fix: Fix unnecessary migrations for timestamps with default values and precision fix: Fix unnecessary migrations for timestamps with default values of CURRENT_TIMESTAMP and precision Dec 30, 2020
@RohanTalip
Copy link
Contributor Author

Actually, I think I got one of the tests wrong ... and likely some production code as a result ... I'll take a look at it soon.

const lengthCond = tableColumn.length !== columnMetadata.length;

// Intentionally using '!=' instead of '!==' so that undefined and null compare as equal.
const precisionCond = tableColumn.precision != columnMetadata.precision;
Copy link
Member

Choose a reason for hiding this comment

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

not sure I really like != approach instead of explicit check for null or undefined, because != forces me to ask questions:

  • what if tableColumn.precision is 0 and columnMetadata.precision is undefined or null? What behaviour should I expect?
  • why the hell precision can be both null or undefined? In what situations it can be null and in what situations it can be undefined? Feels like it should be only one of them when the value isn't defined? Maybe normalization can be applied somewhere else before this check?

Copy link
Member

@pleerock pleerock Jan 4, 2021

Choose a reason for hiding this comment

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

same questions can arise in other people heads, and instead of writing the comments maybe it's better to replace this check with explicit one?

Copy link
Contributor

Choose a reason for hiding this comment

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

If we could bump the TypeScript version a nice check here would be

Suggested change
const precisionCond = tableColumn.precision != columnMetadata.precision;
const precisionCond = (tableColumn.precision ?? null) !== (columnMetadata.precision ?? null);

Copy link
Contributor

Choose a reason for hiding this comment

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

This is now possible on latest TypeScript 😀

@@ -483,14 +483,21 @@ export class CockroachDriver implements Driver {
const defaultValue = columnMetadata.default;
const arrayCast = columnMetadata.isArray ? `::${columnMetadata.type}[]` : "";

// console.log(`normalizeDefault() : columnMetadata.propertyName = ${columnMetadata.propertyName}`);
// console.log(`normalizeDefault() : defaultValue = ${defaultValue}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should probably remove all of the commented out logging lines.

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

Successfully merging this pull request may close these issues.

Migration keeps changing @CreateDateColumn/@UpdateDateColumn timestamp column to same definition
4 participants