Skip to content

Commit

Permalink
fix: fixed all known enum issues (#7419)
Browse files Browse the repository at this point in the history
* fix #5371

* fix #6471;
fix: `enumName` changes not handled;
fix: `enumName` does not handle table schema;

* fixed falling test;

* added test for #7217

* fix #6047, #7283;

* fix #5871

* added support for `enumName` in `joinColumns` (#5729)

* fix #5478

* fixed falling test;
updated `postgres-enum` test;

* added column `array` property change detection (#5882);
updated `postgres-enum` test;

* fix #5275

* added validation for `enum` property (#2233)

* fix #5648

* improved missing "enum" or "enumName" properties validation;

* fix #4897, #6376

* lint fix;

* fixed falling tests;

* fixed falling tests;

* removed .only

* fix #6115
  • Loading branch information
AlexMesser committed Mar 5, 2021
1 parent 2fa6231 commit 724d80b
Show file tree
Hide file tree
Showing 39 changed files with 921 additions and 110 deletions.
4 changes: 4 additions & 0 deletions src/driver/aurora-data-api/AuroraDataApiDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,10 @@ export class AuroraDataApiDriver implements Driver {
normalizeDefault(columnMetadata: ColumnMetadata): string | undefined {
const defaultValue = columnMetadata.default;

if (defaultValue === null) {
return undefined
}

if ((columnMetadata.type === "enum" || columnMetadata.type === "simple-enum") && defaultValue !== undefined) {
return `'${defaultValue}'`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/driver/aurora-data-api/AuroraDataApiQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ export class AuroraDataApiQueryRunner extends BaseQueryRunner implements QueryRu

if (tableColumn.type === "enum" || tableColumn.type === "simple-enum") {
const colType = dbColumn["COLUMN_TYPE"];
const items = colType.substring(colType.indexOf("(") + 1, colType.indexOf(")")).split(",");
const items = colType.substring(colType.indexOf("(") + 1, colType.lastIndexOf(")")).split(",");
tableColumn.enum = (items as string[]).map(item => {
return item.substring(1, item.length - 1);
});
Expand Down
21 changes: 10 additions & 11 deletions src/driver/mysql/MysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,20 @@ export class MysqlDriver implements Driver {
normalizeDefault(columnMetadata: ColumnMetadata): string | undefined {
const defaultValue = columnMetadata.default;

if ((columnMetadata.type === "enum" || columnMetadata.type === "simple-enum") && defaultValue !== undefined) {
if (defaultValue === null) {
return undefined

} else if (
(columnMetadata.type === "enum"
|| columnMetadata.type === "simple-enum"
|| typeof defaultValue === "string")
&& defaultValue !== undefined) {
return `'${defaultValue}'`;
}

if ((columnMetadata.type === "set") && defaultValue !== undefined) {
} else if ((columnMetadata.type === "set") && defaultValue !== undefined) {
return `'${DateUtils.simpleArrayToString(defaultValue)}'`;
}

if (typeof defaultValue === "number") {
} else if (typeof defaultValue === "number") {
return `'${defaultValue.toFixed(columnMetadata.scale)}'`;

} else if (typeof defaultValue === "boolean") {
Expand All @@ -599,12 +604,6 @@ export class MysqlDriver implements Driver {
} else if (typeof defaultValue === "function") {
return defaultValue();

} else if (typeof defaultValue === "string") {
return `'${defaultValue}'`;

} else if (defaultValue === null) {
return undefined;

} else {
return defaultValue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/driver/mysql/MysqlQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {

if (tableColumn.type === "enum" || tableColumn.type === "simple-enum" || tableColumn.type === "set") {
const colType = dbColumn["COLUMN_TYPE"];
const items = colType.substring(colType.indexOf("(") + 1, colType.indexOf(")")).split(",");
const items = colType.substring(colType.indexOf("(") + 1, colType.lastIndexOf(")")).split(",");
tableColumn.enum = (items as string[]).map(item => {
return item.substring(1, item.length - 1);
});
Expand Down Expand Up @@ -1861,7 +1861,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
const isMariaDb = this.driver.options.type === "mariadb";
if (isMariaDb && column.asExpression && (column.generatedType || "VIRTUAL") === "VIRTUAL") {
// do nothing - MariaDB does not support NULL/NOT NULL expressions for VIRTUAL columns
} else {
} else {
if (!column.isNullable)
c += " NOT NULL";
if (column.isNullable)
Expand Down
48 changes: 31 additions & 17 deletions src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,21 +586,30 @@ export class PostgresDriver implements Driver {

} else if (columnMetadata.type === "enum" || columnMetadata.type === "simple-enum" ) {
if (columnMetadata.isArray) {
if (value === "{}") return [];

// manually convert enum array to array of values (pg does not support, see https://github.com/brianc/node-pg-types/issues/56)
value = value !== "{}" ? (value as string).substr(1, (value as string).length - 2).split(",") : [];
// convert to number if that exists in poosible enum options
value = (value as string).substr(1, (value as string).length - 2).split(",").map(val => {
// replace double quotes from the beginning and from the end
if (val.startsWith(`"`) && val.endsWith(`"`)) val = val.slice(1, -1);
// replace double escaped backslash to single escaped e.g. \\\\ -> \\
val = val.replace(/(\\\\)/g, "\\")
// replace escaped double quotes to non-escaped e.g. \"asd\" -> "asd"
return val.replace(/(\\")/g, '"')
});

// convert to number if that exists in possible enum options
value = value.map((val: string) => {
return !isNaN(+val) && columnMetadata.enum!.indexOf(parseInt(val)) >= 0 ? parseInt(val) : val;
});
} else {
// convert to number if that exists in poosible enum options
// convert to number if that exists in possible enum options
value = !isNaN(+value) && columnMetadata.enum!.indexOf(parseInt(value)) >= 0 ? parseInt(value) : value;
}
}

if (columnMetadata.transformer)
value = ApplyValueTransformers.transformFrom(columnMetadata.transformer, value);

return value;
}

Expand Down Expand Up @@ -719,18 +728,22 @@ export class PostgresDriver implements Driver {
/**
* Normalizes "default" value of the column.
*/
normalizeDefault(columnMetadata: ColumnMetadata): string {
normalizeDefault(columnMetadata: ColumnMetadata): string | undefined {
const defaultValue = columnMetadata.default;
if (columnMetadata.isArray && Array.isArray(defaultValue)) {
return `'{${defaultValue.map((val: string) => `${val}`).join(",")}}'`;
}

if ((columnMetadata.type === "enum" || columnMetadata.type === "simple-enum")
&& defaultValue !== undefined) {
return `'${defaultValue}'`;
}
if (defaultValue === null) {
return undefined;

} else if (columnMetadata.isArray && Array.isArray(defaultValue)) {
return `'{${defaultValue.map((val: string) => `${val}`).join(",")}}'`;

if (typeof defaultValue === "number") {
} else if (
(columnMetadata.type === "enum"
|| columnMetadata.type === "simple-enum"
|| typeof defaultValue === "number"
|| typeof defaultValue === "string")
&& defaultValue !== undefined
) {
return `'${defaultValue}'`;

} else if (typeof defaultValue === "boolean") {
Expand All @@ -739,10 +752,7 @@ export class PostgresDriver implements Driver {
} else if (typeof defaultValue === "function") {
return defaultValue();

} else if (typeof defaultValue === "string") {
return `'${defaultValue}'`;

} else if (typeof defaultValue === "object" && defaultValue !== null) {
} else if (typeof defaultValue === "object") {
return `'${JSON.stringify(defaultValue)}'`;

} else {
Expand Down Expand Up @@ -867,13 +877,15 @@ export class PostgresDriver implements Driver {
const isColumnChanged = tableColumn.name !== columnMetadata.databaseName
|| tableColumn.type !== this.normalizeType(columnMetadata)
|| tableColumn.length !== columnMetadata.length
|| tableColumn.isArray !== columnMetadata.isArray
|| tableColumn.precision !== columnMetadata.precision
|| (columnMetadata.scale !== undefined && tableColumn.scale !== columnMetadata.scale)
|| tableColumn.comment !== columnMetadata.comment
|| (!tableColumn.isGenerated && this.lowerDefaultValueIfNecessary(this.normalizeDefault(columnMetadata)) !== tableColumn.default) // we included check for generated here, because generated columns already can have default values
|| tableColumn.isPrimary !== columnMetadata.isPrimary
|| tableColumn.isNullable !== columnMetadata.isNullable
|| tableColumn.isUnique !== this.normalizeIsUnique(columnMetadata)
|| tableColumn.enumName !== columnMetadata.enumName
|| (tableColumn.enum && columnMetadata.enum && !OrmUtils.isArraysEqual(tableColumn.enum, columnMetadata.enum.map(val => val + ""))) // enums in postgres are always strings
|| tableColumn.isGenerated !== columnMetadata.isGenerated
|| (tableColumn.spatialFeatureType || "").toLowerCase() !== (columnMetadata.spatialFeatureType || "").toLowerCase()
Expand All @@ -885,9 +897,11 @@ export class PostgresDriver implements Driver {
// console.log("name:", tableColumn.name, columnMetadata.databaseName);
// console.log("type:", tableColumn.type, this.normalizeType(columnMetadata));
// console.log("length:", tableColumn.length, columnMetadata.length);
// console.log("isArray:", tableColumn.isArray, columnMetadata.isArray);
// console.log("precision:", tableColumn.precision, columnMetadata.precision);
// console.log("scale:", tableColumn.scale, columnMetadata.scale);
// console.log("comment:", tableColumn.comment, columnMetadata.comment);
// console.log("enumName:", tableColumn.enumName, columnMetadata.enumName);
// console.log("enum:", tableColumn.enum && columnMetadata.enum && !OrmUtils.isArraysEqual(tableColumn.enum, columnMetadata.enum.map(val => val + "")));
// console.log("onUpdate:", tableColumn.onUpdate, columnMetadata.onUpdate);
// console.log("isPrimary:", tableColumn.isPrimary, columnMetadata.isPrimary);
Expand Down
Loading

0 comments on commit 724d80b

Please sign in to comment.