Skip to content

Commit

Permalink
fix: Array type default value should not generate SQL commands withou…
Browse files Browse the repository at this point in the history
…t change (#7409)

* fix(1532) Array type default value should not generate SQL commands without change

* Update PostgresDriver.ts

* removed `arrayCast` from `normalizeDefault` since casting for default value is already removed in `PostgresQueryRunner.loadTables()` method;
* removed support for function definition in `default` because function syntax suppose to support raw sql, we don't have to confuse things by applying custom modifications.

* Update User.ts

removed incorrect `default` definition with functions

Co-authored-by: AlexMesser <dmzt08@gmail.com>
  • Loading branch information
edeesis and AlexMesser committed Feb 24, 2021
1 parent 01a215a commit 7f06e44
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,17 +721,12 @@ export class PostgresDriver implements Driver {
*/
normalizeDefault(columnMetadata: ColumnMetadata): string {
const defaultValue = columnMetadata.default;
const arrayCast = columnMetadata.isArray ? `::${columnMetadata.type}[]` : "";
if (columnMetadata.isArray && Array.isArray(defaultValue)) {
return `'{${defaultValue.map((val: string) => `${val}`).join(",")}}'`;
}

if (
(
columnMetadata.type === "enum"
|| columnMetadata.type === "simple-enum"
) && defaultValue !== undefined
) {
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}'`;
}

Expand All @@ -745,7 +740,7 @@ export class PostgresDriver implements Driver {
return defaultValue();

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

} else if (typeof defaultValue === "object" && defaultValue !== null) {
return `'${JSON.stringify(defaultValue)}'`;
Expand Down
27 changes: 27 additions & 0 deletions test/github-issues/1532/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {PrimaryColumn, Column} from "../../../../src";
import {Entity} from "../../../../src/decorator/entity/Entity";

@Entity()
export class User {

@PrimaryColumn()
id: number;

@Column({ type: "varchar", array: true })
array: string[];

@Column({ type: "varchar", array: true, nullable: false })
nonNullArray: string[];

@Column({ type: "varchar", array: true, nullable: false, default: [] })
emptyArrayDefault: string[];

@Column({ type: "varchar", array: true, nullable: false, default: ["a", "b", "c"] })
filledArrayDefault: string[];

@Column({ type: "varchar", array: true, nullable: false, default: "{}" })
emptyArrayDefaultString: string[];

@Column({ type: "varchar", array: true, nullable: false, default: "{a,b,c}" })
filledArrayDefaultString: string[];
}
30 changes: 30 additions & 0 deletions test/github-issues/1532/issue-1532.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import "reflect-metadata";
import {Connection} from "../../../src";
import {createTestingConnections, closeTestingConnections} from "../../utils/test-utils";
import {User} from "./entity/User";

describe("github issues > #1532 Array type default value doesnt work. PostgreSQL", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
migrations: [],
enabledDrivers: ["postgres"],
schemaCreate: false,
dropSchema: true,
entities: [User],
}));
after(() => closeTestingConnections(connections));

it("can recognize model changes", () => Promise.all(connections.map(async connection => {
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.upQueries.length.should.be.greaterThan(0);
sqlInMemory.downQueries.length.should.be.greaterThan(0);
})));

it("does not generate when no model changes", () => Promise.all(connections.map(async connection => {
await connection.driver.createSchemaBuilder().build();

const sqlInMemory = await connection.driver.createSchemaBuilder().log();
sqlInMemory.upQueries.length.should.be.equal(0);
sqlInMemory.downQueries.length.should.be.equal(0);
})));
});

0 comments on commit 7f06e44

Please sign in to comment.