Skip to content

Commit

Permalink
fix: defaults type cast filtering in Cockroachdb (#8144)
Browse files Browse the repository at this point in the history
* fix: defaults type cast filtering

This fixes column default value in cockroachdb continuously producing schema changes when there should be none

Refs: #7110

* fix: defaults type cast filtering

Remove debugging code

Refs: #7110
  • Loading branch information
julius-welink committed Nov 4, 2021
1 parent 4638dea commit 28c183e
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/driver/cockroachdb/CockroachQueryRunner.ts
Expand Up @@ -1577,7 +1577,7 @@ export class CockroachQueryRunner extends BaseQueryRunner implements QueryRunner
tableColumn.generationStrategy = "uuid";

} 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
9 changes: 2 additions & 7 deletions test/github-issues/5132/issue-5132.ts
Expand Up @@ -5,30 +5,25 @@ import { createTestingConnections, closeTestingConnections } from "../../utils/t

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

describe("github issues > #5132 postgres: Default of -1 (minus 1) generates useless migrations`", () => {
describe("github issues > #5132: 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],
enabledDrivers: ["postgres", "cockroachdb"],
}));
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();
Expand Down
2 changes: 1 addition & 1 deletion test/github-issues/7110/entity/foo.entity.ts
Expand Up @@ -9,7 +9,7 @@ export class Foo extends BaseEntity {
@Column({
nullable: false,
type: "varchar",
default: () => "TO_CHAR(100, 'FMU000')",
default: () => "concat(chr(65), 'FMU000')",
})
displayId: string;
}
12 changes: 4 additions & 8 deletions test/github-issues/7110/issue-7110.ts
Expand Up @@ -5,34 +5,30 @@ import { createTestingConnections, closeTestingConnections } from "../../utils/t

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

describe("github issues > #7110 postgres: Typeorm Migrations ignore existing default value on column`", () => {
describe("github issues > #7110: 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],
enabledDrivers: ["postgres", "cockroachdb"],
}));
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.upQueries.length.should.be.equal(0, sqlInMemory.downQueries.map(q => q.query).join("\n"));
sqlInMemory.downQueries.length.should.be.equal(0);
})));
});
Expand Down

0 comments on commit 28c183e

Please sign in to comment.