Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: columns with transformer should be normalized for update (#5700)
* fix: columns with transformer should be normalized for update Closes: #2703 * fix: test case to use separate logger per connection * fix: test dummy column type int means int8 on cockroachdb. Explicitly specify int4. * fix: use string instead of number for test int4 doesn't work for all dbs. Use string because it's universal. * fix: let typeorm infer proper test column type Co-authored-by: Ryan Shea <ryan.shea@alphaledger.com>
- Loading branch information
1 parent
215f106
commit 4ef6b65
Showing
6 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {Entity} from "../../../../src/decorator/entity/Entity"; | ||
import {Column} from "../../../../src/decorator/columns/Column"; | ||
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {WrappedString, wrappedStringTransformer} from "../wrapped-string"; | ||
|
||
@Entity() | ||
export class Dummy { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({type: String, transformer: wrappedStringTransformer}) | ||
value: WrappedString; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {expect} from "chai"; | ||
import {Connection} from "../../../src/connection/Connection"; | ||
import {createTestingConnections, reloadTestingDatabases, closeTestingConnections} from "../../utils/test-utils"; | ||
import {Dummy} from "./entity/Dummy"; | ||
import {WrappedString} from "./wrapped-string"; | ||
import {MemoryLogger} from "./memory-logger"; | ||
|
||
describe.only("github issues > #2703 Column with transformer is not normalized for update", () => { | ||
let connections: Connection[]; | ||
|
||
before(async () => connections = await createTestingConnections({ | ||
entities: [`${__dirname}/entity/*{.js,.ts}`], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
createLogger: () => new MemoryLogger(false), | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
afterEach(() => connections.forEach(connection => { | ||
const logger = connection.logger as MemoryLogger; | ||
logger.enabled = false; | ||
logger.clear(); | ||
})); | ||
|
||
it("should transform values when computing changed columns", () => Promise.all(connections.map(async connection => { | ||
const repository = connection.getRepository(Dummy); | ||
|
||
const dummy = repository.create({ | ||
value: new WrappedString("test"), | ||
}); | ||
await repository.save(dummy); | ||
|
||
const logger = connection.logger as MemoryLogger; | ||
logger.enabled = true; | ||
|
||
await repository.save(dummy); | ||
|
||
const updateQueries = logger.queries.filter(q => q.startsWith("UPDATE")); | ||
|
||
expect(updateQueries).to.be.empty; | ||
}))); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {Logger} from "../../../src/logger/Logger"; | ||
|
||
export class MemoryLogger implements Logger { | ||
constructor(public enabled = true) {} | ||
|
||
private _queries: string[] = []; | ||
get queries() { return this._queries; } | ||
|
||
logQuery(query: string) { | ||
if (this.enabled) { | ||
this._queries.push(query); | ||
} | ||
} | ||
|
||
logQueryError(error: string, query: string) {} | ||
|
||
logQuerySlow(time: number, query: string) {} | ||
|
||
logSchemaBuild(message: string) {} | ||
|
||
logMigration(message: string) {} | ||
|
||
log(level: "log" | "info" | "warn", message: any) {} | ||
|
||
writeToConsole() { | ||
this.queries.forEach(q => console.log(`query: ${q}`)); | ||
} | ||
|
||
clear() { | ||
this._queries = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {ValueTransformer} from "../../../src/decorator/options/ValueTransformer"; | ||
|
||
export class WrappedString { | ||
constructor(readonly value: string) {} | ||
} | ||
|
||
export const wrappedStringTransformer: ValueTransformer = { | ||
from(value: string): WrappedString { | ||
return new WrappedString(value); | ||
}, | ||
to(value: WrappedString): string { | ||
return value.value; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters