Skip to content

Commit

Permalink
fix: correctly strip type conversion in postgres for default values (#…
Browse files Browse the repository at this point in the history
…7681)

* fix: correctly strip type conversion in postgres for default values

Use better regex to remove only type conversion, not the whole string to the end

Closes: 7110

* fix: better regex

* fix: modify postgres default regex to not break #1532

* fix: modify postgres default regex to support user-defined types

This makes sure updated regex does not break migrations chnages introduced in #7647

* test: add test case for #5132
  • Loading branch information
kauz committed May 30, 2021
1 parent 60a6c5d commit 069b8b6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1728,9 +1728,9 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
tableColumn.isGenerated = true;
tableColumn.generationStrategy = "uuid";
} else if (dbColumn["column_default"] === "now()" || dbColumn["column_default"].indexOf("'now'::text") !== -1) {
tableColumn.default = dbColumn["column_default"]
tableColumn.default = dbColumn["column_default"];
} else {
tableColumn.default = dbColumn["column_default"].replace(/::.*/, "");
tableColumn.default = dbColumn["column_default"].replace(/::[\w\s\[\]\"]+/g, "");
tableColumn.default = tableColumn.default.replace(/^(-?\d+)$/, "'$1'");
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/5132/entity/foo.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "../../../../src";

@Entity("foo")
export class Foo extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;

@Column({ default: 1, type: "int" })
bar_default_1: number;

@Column({ default: -1, type: "int" })
bar_default_minus_1: number;
}
40 changes: 40 additions & 0 deletions test/github-issues/5132/issue-5132.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "reflect-metadata";
import { Connection } from "../../../src";

import { createTestingConnections, closeTestingConnections } from "../../utils/test-utils";

import { Foo } from "./entity/foo.entity";

describe("github issues > #5132 postgres: Default of -1 (minus 1) generates useless migrations`", () => {
describe("-1 (minus 1) in default value", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
schemaCreate: false,
dropSchema: true,
entities: [Foo],
}));
after(() => closeTestingConnections(connections));

it("can recognize model changes", () => Promise.all(connections.map(async connection => {
if (connection.driver.options.type !== "postgres") {
return;
}
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 => {
if (connection.driver.options.type !== "postgres") {
return;
}
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);
})));
});
});
15 changes: 15 additions & 0 deletions test/github-issues/7110/entity/foo.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {BaseEntity, Column, Entity, PrimaryGeneratedColumn} from "../../../../src";

@Entity("foo_test")
export class Foo extends BaseEntity {

@PrimaryGeneratedColumn()
id: number;

@Column({
nullable: false,
type: "varchar",
default: () => "TO_CHAR(100, 'FMU000')",
})
displayId: string;
}
39 changes: 39 additions & 0 deletions test/github-issues/7110/issue-7110.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import "reflect-metadata";
import { Connection } from "../../../src";

import { createTestingConnections, closeTestingConnections } from "../../utils/test-utils";

import { Foo } from "./entity/foo.entity";

describe("github issues > #7110 postgres: Typeorm Migrations ignore existing default value on column`", () => {
describe("double type conversion in default value", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
schemaCreate: false,
dropSchema: true,
entities: [Foo],
}));
after(() => closeTestingConnections(connections));

it("can recognize model changes", () => Promise.all(connections.map(async connection => {
if (connection.driver.options.type !== "postgres") {
return;
}
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 => {
if (connection.driver.options.type !== "postgres") {
return;
}
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 069b8b6

Please sign in to comment.