Skip to content

Commit

Permalink
fix: refactored usages of enablePostgresUuidExtension to accept query…
Browse files Browse the repository at this point in the history
…Runner as the main param
  • Loading branch information
sksadjad committed Nov 25, 2023
1 parent 5161837 commit 3654a8a
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { createQueryRunnerAdapter } from './utils'
import { enablePostgresUuidExtension } from '@sphereon/ssi-sdk.core'

export class CreateContacts1659463079428 implements MigrationInterface {
name = 'CreateContacts1659463079428'

public async up(queryRunner: QueryRunner): Promise<void> {
await enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner))
await enablePostgresUuidExtension(queryRunner)
await queryRunner.query(
`CREATE TABLE "BaseConfigEntity" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "client_id" character varying(255), "client_secret" character varying(255), "scopes" text, "issuer" character varying(255), "redirect_url" text, "dangerously_allow_insecure_http_requests" boolean, "client_auth_method" text, "identifier" character varying(255), "session_id" character varying(255), "type" character varying NOT NULL, "connectionId" uuid, CONSTRAINT "REL_BaseConfig_connectionId" UNIQUE ("connectionId"), CONSTRAINT "PK_BaseConfigEntity_id" PRIMARY KEY ("id"))`
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { createQueryRunnerAdapter } from './utils'
import { enablePostgresUuidExtension } from '@sphereon/ssi-sdk.core'

export class CreateIssuanceBranding1685628974232 implements MigrationInterface {
name = 'CreateIssuanceBranding1685628974232'

public async up(queryRunner: QueryRunner): Promise<void> {
await enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner))
await enablePostgresUuidExtension(queryRunner)
await queryRunner.query(
`CREATE TABLE "ImageDimensions" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "width" integer NOT NULL, "height" integer NOT NULL, CONSTRAINT "PK_ImageDimensions_id" PRIMARY KEY ("id"))`
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { createQueryRunnerAdapter } from './utils'
import { enablePostgresUuidExtension } from '@sphereon/ssi-sdk.core'

export class CreateContacts1690925872592 implements MigrationInterface {
name = 'CreateContacts1690925872592'

public async up(queryRunner: QueryRunner): Promise<void> {
await enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner))
await enablePostgresUuidExtension(queryRunner)
await queryRunner.query(`ALTER TABLE "CorrelationIdentifier" DROP CONSTRAINT "FK_CorrelationIdentifier_identityId"`)
await queryRunner.query(`ALTER TABLE "IdentityMetadata" DROP CONSTRAINT "FK_IdentityMetadata_identityId"`)
await queryRunner.query(`ALTER TABLE "Identity" DROP CONSTRAINT "FK_Identity_contactId"`)
Expand Down
8 changes: 0 additions & 8 deletions packages/data-store/src/migrations/postgres/utils.ts

This file was deleted.

13 changes: 6 additions & 7 deletions packages/ssi-sdk-core/src/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ export const flattenArray = <T>(args: { items: Array<T | Array<T>> }): Array<T>

export const flattenMigrations = <T>(args: { migrations: Array<T | Array<T>> }): Array<T> => args.migrations.flat() as Array<T>

export type WithTypeOrmQuery =
((query: string, parameters?: any[]) => Promise<any>) |
((query: string, parameters?: any[], useStructuredResult?: boolean) => Promise<any>)


export const enablePostgresUuidExtension = async (query: WithTypeOrmQuery) => {
// It should accept queryRunner from the typeorm
export const enablePostgresUuidExtension = async (queryRunner: any) => {
if (!queryRunner.query) {
throw new Error("You should pass a QueryRunner object to this function.")
}
try {
await query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`);
await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`);
} catch (error) {
console.log(
`Please enable the uuid-ossp.control extension in your PostgreSQL installation. It enables generating V4 UUID and can be found in the postgresql-contrib package`
Expand Down
2 changes: 1 addition & 1 deletion packages/ssi-sdk-core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './encoding'
export { enablePostgresUuidExtension, flattenArray, flattenMigrations, WithTypeOrmQuery } from './database'
export { enablePostgresUuidExtension, flattenArray, flattenMigrations } from './database'
export { getImageMediaType, getImageDimensions, downloadImage } from './image'
export * from './vc'

0 comments on commit 3654a8a

Please sign in to comment.