Skip to content
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 12 commits into from
Feb 27, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ feel free to ask us and community.
### Bug Fixes

* fixed signatures of `update`/`insert` methods, some `find*` methods in repositories, entity managers, BaseEntity and QueryBuilders
* fixed call to deprecated `uuid_generate_v4()` for Postgres, now using `gen_random_uuid()`

### Features

Expand Down
4 changes: 2 additions & 2 deletions src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ export class PostgresDriver implements Driver {
if (err) return fail(err);
if (hasUuidColumns)
try {
await this.executeQuery(connection, `CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
await this.executeQuery(connection, `CREATE EXTENSION IF NOT EXISTS "pgcrypto"`);
} catch (_) {
logger.log("warn", "At least one of the entities has uuid column, but the 'uuid-ossp' extension cannot be installed automatically. Please install it manually using superuser rights");
logger.log("warn", "At least one of the entities has uuid column, but the 'pgcrypto' extension cannot be installed automatically. Please install it manually using superuser rights");
}
if (hasCitextColumns)
try {
Expand Down
5 changes: 2 additions & 3 deletions src/driver/postgres/PostgresQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
if (dbColumn["column_default"].replace(/"/gi, "") === `nextval('${this.buildSequenceName(table, dbColumn["column_name"], currentSchema, true)}'::regclass)`) {
tableColumn.isGenerated = true;
tableColumn.generationStrategy = "increment";

} else if (/^uuid_generate_v\d\(\)/.test(dbColumn["column_default"])) {
} else if (dbColumn["column_default"] === "gen_random_uuid()") {
tableColumn.isGenerated = true;
tableColumn.generationStrategy = "uuid";

Expand Down Expand Up @@ -1912,7 +1911,7 @@ export class PostgresQueryRunner extends BaseQueryRunner implements QueryRunner
if (column.default !== undefined && column.default !== null)
c += " DEFAULT " + column.default;
if (column.isGenerated && column.generationStrategy === "uuid" && !column.default)
c += " DEFAULT uuid_generate_v4()";
c += " DEFAULT gen_random_uuid()";

return c;
}
Expand Down
2 changes: 1 addition & 1 deletion test/functional/schema-builder/change-column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ describe("schema builder > change column", () => {
const queryRunner = connection.createQueryRunner();

if (connection.driver instanceof PostgresDriver)
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"`);
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "pgcrypto"`);

const postMetadata = connection.getMetadata(Post);
const idColumn = postMetadata.findColumnWithPropertyName("id")!;
Expand Down