-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Postgres connection option to use the pgcrypto extension to generate UUIDs #3537
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6f7923a
Use pgcrypto gen_random_uuid() instead of uuid-ossp uuid_generate_v4()
typokign 0709b44
fix reference to uuid-ossp in tests
typokign 9e06268
mark UUID columns as generated
typokign b40de5e
Merge remote-tracking branch 'upstream/master'
typokign 7dc76f8
Merge remote-tracking branch 'upstream/master'
typokign 0910f63
Allow the uuid extension to be specified in connection options
typokign aff15d6
revert one test and update changelog
typokign 4fa4064
clean whitespace
typokign a58bd07
document new option
typokign 80bd28e
fix typo
typokign ca4a821
Merge remote-tracking branch 'upstream/master'
typokign b96aad8
update docs
typokign File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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,98 @@ | ||
import "reflect-metadata"; | ||
import {expect} from "chai"; | ||
import {Record} from "./entity/Record"; | ||
import {Connection} from "../../../../src/connection/Connection"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils"; | ||
import {Post} from "./entity/Post"; | ||
import {Question} from "./entity/Question"; | ||
|
||
describe("uuid-ossp", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
enabledDrivers: ["postgres"], | ||
driverSpecific: { | ||
uuidExtension: "uuid-ossp" | ||
} | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should make correct schema with Postgres' uuid type", () => Promise.all(connections.map(async connection => { | ||
const queryRunner = connection.createQueryRunner(); | ||
const schema = await queryRunner.getTable("record"); | ||
await queryRunner.release(); | ||
expect(schema).not.to.be.empty; | ||
expect(schema!.columns.find(tableColumn => tableColumn.name === "id" && tableColumn.type === "uuid" && tableColumn.isGenerated)).to.be.not.empty; | ||
}))); | ||
|
||
it("should persist uuid correctly", () => Promise.all(connections.map(async connection => { | ||
const recordRepo = connection.getRepository(Record); | ||
const record = new Record(); | ||
record.id = "fd357b8f-8838-42f6-b7a2-ae027444e895"; | ||
const persistedRecord = await recordRepo.save(record); | ||
const foundRecord = await recordRepo.findOne(persistedRecord.id); | ||
expect(foundRecord).to.be.exist; | ||
expect(foundRecord!.id).to.eq("fd357b8f-8838-42f6-b7a2-ae027444e895"); | ||
}))); | ||
|
||
it("should persist uuid correctly when it is generated non primary column", () => Promise.all(connections.map(async connection => { | ||
|
||
const postRepository = connection.getRepository(Post); | ||
const questionRepository = connection.getRepository(Question); | ||
const queryRunner = connection.createQueryRunner(); | ||
const postTable = await queryRunner.getTable("post"); | ||
const questionTable = await queryRunner.getTable("question"); | ||
await queryRunner.release(); | ||
|
||
const post = new Post(); | ||
await postRepository.save(post); | ||
const loadedPost = await postRepository.findOne(1); | ||
expect(loadedPost!.uuid).to.be.exist; | ||
postTable!.findColumnByName("uuid")!.type.should.be.equal("uuid"); | ||
|
||
const post2 = new Post(); | ||
post2.uuid = "fd357b8f-8838-42f6-b7a2-ae027444e895"; | ||
await postRepository.save(post2); | ||
const loadedPost2 = await postRepository.findOne(2); | ||
expect(loadedPost2!.uuid).to.equal("fd357b8f-8838-42f6-b7a2-ae027444e895"); | ||
|
||
const question = new Question(); | ||
question.uuid2 = "fd357b8f-8838-42f6-b7a2-ae027444e895"; | ||
|
||
const savedQuestion = await questionRepository.save(question); | ||
expect(savedQuestion!.id).to.be.exist; | ||
expect(savedQuestion!.uuid).to.be.exist; | ||
expect(savedQuestion!.uuid2).to.equal("fd357b8f-8838-42f6-b7a2-ae027444e895"); | ||
expect(savedQuestion!.uuid3).to.be.null; | ||
expect(savedQuestion!.uuid4).to.be.exist; | ||
|
||
const loadedQuestion = await questionRepository.findOne(savedQuestion.id); | ||
expect(loadedQuestion!.id).to.be.exist; | ||
expect(loadedQuestion!.uuid).to.be.exist; | ||
expect(loadedQuestion!.uuid2).to.equal("fd357b8f-8838-42f6-b7a2-ae027444e895"); | ||
expect(loadedQuestion!.uuid3).to.be.null; | ||
expect(loadedQuestion!.uuid4).to.be.exist; | ||
questionTable!.findColumnByName("id")!.type.should.be.equal("uuid"); | ||
questionTable!.findColumnByName("uuid")!.type.should.be.equal("uuid"); | ||
questionTable!.findColumnByName("uuid2")!.type.should.be.equal("uuid"); | ||
questionTable!.findColumnByName("uuid3")!.type.should.be.equal("uuid"); | ||
|
||
const question2 = new Question(); | ||
question2.id = "1ecad7f6-23ee-453e-bb44-16eca26d5189"; | ||
question2.uuid = "35b44650-b2cd-44ec-aa54-137fbdf1c373"; | ||
question2.uuid2 = "fd357b8f-8838-42f6-b7a2-ae027444e895"; | ||
question2.uuid3 = null; | ||
question2.uuid4 = null; | ||
await questionRepository.save(question2); | ||
const loadedQuestion2 = await questionRepository.findOne("1ecad7f6-23ee-453e-bb44-16eca26d5189"); | ||
expect(loadedQuestion2!.id).to.equal("1ecad7f6-23ee-453e-bb44-16eca26d5189"); | ||
expect(loadedQuestion2!.uuid).to.equal("35b44650-b2cd-44ec-aa54-137fbdf1c373"); | ||
expect(loadedQuestion2!.uuid2).to.equal("fd357b8f-8838-42f6-b7a2-ae027444e895"); | ||
expect(loadedQuestion2!.uuid3).to.be.null; | ||
expect(loadedQuestion2!.uuid4).to.be.null; | ||
}))); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The diff isn't exactly clear what I did here - I actually renamed the
uuid-postgres
tests touuid-ossp
, and configured the tests to use theuuid-ossp
extension. Then I copied theuuid-ossp.ts
file topgcrypto.ts
, left all the tests the same, and set the extension topgcrypto
. Looks like Git linked the old uuid-postgres file to the new pgcrypto file, but if you manually diff the two files you'll see that none of the tests have been changed.