Skip to content

Commit

Permalink
fix: add PhysicalAddress migrations to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmelati committed Apr 10, 2024
1 parent 1216d67 commit afd441c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<void> {
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<void> {
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`,
)
}
}
}
3 changes: 2 additions & 1 deletion packages/data-store/src/migrations/generic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class CreatePhysicalAddressTable1712695589134 implements MigrationInterface {

public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
await queryRunner.query(`DROP TABLE "PhysicalAddress"`)
}

}

0 comments on commit afd441c

Please sign in to comment.