From afd441ca1eccdb9438115de8b8e45d597dbf1fca Mon Sep 17 00:00:00 2001 From: John Melati Date: Wed, 10 Apr 2024 09:39:45 +0200 Subject: [PATCH] fix: add PhysicalAddress migrations to postgres --- .../generic/8-CreatePhysicalAddressTable.ts | 59 +++++++++++++++++++ .../src/migrations/generic/index.ts | 3 +- ...712695589134-CreatePhysicalAddressTable.ts | 29 +++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 packages/data-store/src/migrations/generic/8-CreatePhysicalAddressTable.ts create mode 100644 packages/data-store/src/migrations/postgres/1712695589134-CreatePhysicalAddressTable.ts diff --git a/packages/data-store/src/migrations/generic/8-CreatePhysicalAddressTable.ts b/packages/data-store/src/migrations/generic/8-CreatePhysicalAddressTable.ts new file mode 100644 index 000000000..4f9d4664a --- /dev/null +++ b/packages/data-store/src/migrations/generic/8-CreatePhysicalAddressTable.ts @@ -0,0 +1,59 @@ +import { DatabaseType, MigrationInterface, QueryRunner } from 'typeorm' +import Debug from 'debug' +import { CreatePhysicalAddressTable1712695589134 } from '../postgres/1712695589134-CreatePhysicalAddressTable' + +const debug: Debug.Debugger = Debug('sphereon:ssi-sdk:migrations') + +export class CreatePhysicalAddressTable1708098041262 implements MigrationInterface { + name = 'CreatePhysicalAddressTable1708098041262' + + public async up(queryRunner: QueryRunner): Promise { + debug('migration: creating physical address tables on postgres') + const dbType: DatabaseType = queryRunner.connection.driver.options.type + + switch (dbType) { + case 'postgres': { + debug('using postgres migration file') + const mig: CreatePhysicalAddressTable1712695589134 = new CreatePhysicalAddressTable1712695589134() + await mig.up(queryRunner) + debug('Migration statements executed') + return + } + case 'sqlite': + case 'expo': + case 'react-native': { + debug('No need to execute migration for this update') + return + } + default: + return Promise.reject( + `Migrations are currently only supported for sqlite, react-native, expo and postgres. Was ${dbType}. Please run your database without migrations and with 'migrationsRun: false' and 'synchronize: true' for now`, + ) + } + } + + public async down(queryRunner: QueryRunner): Promise { + debug('migration: reverting machine state tables') + const dbType: DatabaseType = queryRunner.connection.driver.options.type + + switch (dbType) { + case 'postgres': { + debug('using postgres migration file') + const mig: CreatePhysicalAddressTable1712695589134 = new CreatePhysicalAddressTable1712695589134() + await mig.down(queryRunner) + debug('Migration statements executed') + return + } + case 'sqlite': + case 'expo': + case 'react-native': { + debug('No need to execute migration for this update') + return + } + default: + return Promise.reject( + `Migrations are currently only supported for sqlite, react-native, expo and postgres. Was ${dbType}. Please run your database without migrations and with 'migrationsRun: false' and 'synchronize: true' for now`, + ) + } + } +} diff --git a/packages/data-store/src/migrations/generic/index.ts b/packages/data-store/src/migrations/generic/index.ts index 9769074fb..70a94c013 100644 --- a/packages/data-store/src/migrations/generic/index.ts +++ b/packages/data-store/src/migrations/generic/index.ts @@ -5,6 +5,7 @@ import { CreateStatusList1693866470000 } from './4-CreateStatusList' import { CreateAuditEvents1701635835330 } from './5-CreateAuditEvents' import { CreateDigitalCredential1708525189000 } from './6-CreateDigitalCredential' import { CreateMachineStateStore1708098041262 } from './7-CreateMachineStateStore' +import { CreatePhysicalAddressTable1708098041262 } from './8-CreatePhysicalAddressTable' /** * The migrations array that SHOULD be used when initializing a TypeORM database connection. @@ -15,7 +16,7 @@ import { CreateMachineStateStore1708098041262 } from './7-CreateMachineStateStor */ // Individual migrations per purpose. Allows parties to not run migrations and thus create/update tables if they are not using a particular feature (yet) -export const DataStoreContactMigrations = [CreateContacts1659463079429, CreateContacts1690925872318] +export const DataStoreContactMigrations = [CreateContacts1659463079429, CreateContacts1690925872318, CreatePhysicalAddressTable1708098041262] export const DataStoreIssuanceBrandingMigrations = [CreateIssuanceBranding1659463079429] export const DataStoreStatusListMigrations = [CreateStatusList1693866470000] export const DataStoreEventLoggerMigrations = [CreateAuditEvents1701635835330] diff --git a/packages/data-store/src/migrations/postgres/1712695589134-CreatePhysicalAddressTable.ts b/packages/data-store/src/migrations/postgres/1712695589134-CreatePhysicalAddressTable.ts new file mode 100644 index 000000000..9fe3a3bf6 --- /dev/null +++ b/packages/data-store/src/migrations/postgres/1712695589134-CreatePhysicalAddressTable.ts @@ -0,0 +1,29 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class CreatePhysicalAddressTable1712695589134 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE TABLE "PhysicalAddress" ( + "id" text PRIMARY KEY NOT NULL, + "type" varchar(255) NOT NULL, + "street_name" varchar(255) NOT NULL, + "street_number" varchar(255) NOT NULL, + "postal_code" varchar(255) NOT NULL, + "city_name" varchar(255) NOT NULL, + "province_name" varchar(255) NOT NULL, + "country_code" varchar(2) NOT NULL, + "building_name" varchar(255), + "partyId" uuid, + "created_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "last_updated_at" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT "FK_PhysicalAddressEntity_partyId" FOREIGN KEY ("partyId") REFERENCES "Party" ("id") ON DELETE CASCADE + );` + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE "PhysicalAddress"`) + } + +}