Skip to content

Commit

Permalink
fix: better timestamp comparison (#4769)
Browse files Browse the repository at this point in the history
  • Loading branch information
silentroach authored and pleerock committed Oct 18, 2019
1 parent f392f89 commit 0a13e6a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
66 changes: 44 additions & 22 deletions src/persistence/SubjectChangedColumnsComputer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,50 @@ export class SubjectChangedColumnsComputer {
let normalizedValue = entityValue;
// normalize special values to make proper comparision
if (entityValue !== null) {
if (column.type === "date") {
normalizedValue = DateUtils.mixedDateToDateString(entityValue);

} else if (column.type === "time") {
normalizedValue = DateUtils.mixedDateToTimeString(entityValue);

} else if (column.type === "datetime" || column.type === Date) {
normalizedValue = DateUtils.mixedDateToUtcDatetimeString(entityValue);
databaseValue = DateUtils.mixedDateToUtcDatetimeString(databaseValue);

} else if (column.type === "json" || column.type === "jsonb") {
// JSON.stringify doesn't work because postgresql sorts jsonb before save.
// If you try to save json '[{"messages": "", "attribute Key": "", "level":""}] ' as jsonb,
// then postgresql will save it as '[{"level": "", "message":"", "attributeKey": ""}]'
if (OrmUtils.deepCompare(entityValue, databaseValue)) return;

} else if (column.type === "simple-array") {
normalizedValue = DateUtils.simpleArrayToString(entityValue);
databaseValue = DateUtils.simpleArrayToString(databaseValue);
} else if (column.type === "simple-enum") {
normalizedValue = DateUtils.simpleEnumToString(entityValue);
databaseValue = DateUtils.simpleEnumToString(databaseValue);
switch (column.type) {
case "date":
normalizedValue = DateUtils.mixedDateToDateString(entityValue);
break;

case "time":
case "time with time zone":
case "time without time zone":
case "timetz":
normalizedValue = DateUtils.mixedDateToTimeString(entityValue);
break;

case "datetime":
case "datetime2":
case Date:
case "timestamp":
case "timestamp without time zone":
case "timestamp with time zone":
case "timestamp with local time zone":
case "timestamptz":
normalizedValue = DateUtils.mixedDateToUtcDatetimeString(entityValue);
databaseValue = DateUtils.mixedDateToUtcDatetimeString(databaseValue);
break;

case "json":
case "jsonb":
// JSON.stringify doesn't work because postgresql sorts jsonb before save.
// If you try to save json '[{"messages": "", "attribute Key": "", "level":""}] ' as jsonb,
// then postgresql will save it as '[{"level": "", "message":"", "attributeKey": ""}]'
if (OrmUtils.deepCompare(entityValue, databaseValue)) return;
break;

case "simple-array":
normalizedValue = DateUtils.simpleArrayToString(entityValue);
databaseValue = DateUtils.simpleArrayToString(databaseValue);
break;
case "simple-enum":
normalizedValue = DateUtils.simpleEnumToString(entityValue);
databaseValue = DateUtils.simpleEnumToString(databaseValue);
break;
case "simple-json":
normalizedValue = DateUtils.simpleJsonToString(entityValue);
databaseValue = DateUtils.simpleJsonToString(databaseValue);
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/util/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class DateUtils {
}

/**
* Converts given value into utc datetime string in a "YYYY-MM-DD HH-mm-ss" format.
* Converts given value into utc datetime string in a "YYYY-MM-DD HH-mm-ss.sss" format.
*/
static mixedDateToUtcDatetimeString(value: Date|any): string|any {
if (typeof value === "string") {
Expand Down

0 comments on commit 0a13e6a

Please sign in to comment.