Skip to content

Commit

Permalink
fix(data-store): fix react-native migrations on older Android install…
Browse files Browse the repository at this point in the history
…ations (#1152)
  • Loading branch information
nklomp authored Mar 31, 2023
1 parent 9948ed6 commit 826b994
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 80 deletions.
14 changes: 6 additions & 8 deletions packages/data-store/src/entities/PreMigrationEntities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseEntity, Column, Entity, PrimaryColumn } from 'typeorm'
import { KeyType } from "./key.js";
import { Column, Entity, PrimaryColumn } from 'typeorm'
import { Key } from './key.js'

/**
* This represents the private key data of keys that were stored by {@link @veramo/data-store#KeyStore} before Veramo
Expand All @@ -8,16 +8,14 @@ import { KeyType } from "./key.js";
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
@Entity('key')
export class PreMigrationKey extends BaseEntity {
@Entity('key', )
export class PreMigrationKey extends Key {
// Key contains all the other columns present needed for successful migrations

@PrimaryColumn()
// @ts-ignore
kid: string

@Column()
// @ts-ignore
type: KeyType

@Column({ nullable: true })
privateKeyHex?: string
}
70 changes: 34 additions & 36 deletions packages/data-store/src/migrations/1.createDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MigrationInterface, QueryRunner, Table } from 'typeorm'
import Debug from 'debug'
import { migrationGetTableName } from './migration-functions.js'

const debug = Debug('veramo:data-store:initial-migration')

/**
Expand All @@ -8,22 +10,18 @@ const debug = Debug('veramo:data-store:initial-migration')
* @public
*/
export class CreateDatabase1447159020001 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
function getTableName(givenName: string): string {
return (
queryRunner.connection.entityMetadatas.find((meta) => meta.givenTableName === givenName)?.tableName ||
givenName
)
}

name = 'CreateDatabase1447159020001' // Used in case this class gets minified, which would change the classname

async up(queryRunner: QueryRunner): Promise<void> {
const dateTimeType: string = queryRunner.connection.driver.mappedDataTypes.createDate as string

debug(`creating identifier table`)
// "CREATE TABLE \"identifier\" (\"did\" varchar PRIMARY KEY NOT NULL, \"provider\" varchar, \"alias\" varchar, \"saveDate\" datetime NOT NULL DEFAULT (datetime('now')), \"updateDate\" datetime NOT NULL DEFAULT (datetime('now')), \"controllerKeyId\" varchar)",
// "CREATE UNIQUE INDEX \"IDX_6098cca69c838d91e55ef32fe1\" ON \"identifier\" (\"alias\", \"provider\")",
await queryRunner.createTable(
new Table({
name: getTableName('identifier'),
name: migrationGetTableName(queryRunner,'identifier'),
columns: [
{ name: 'did', type: 'varchar', isPrimary: true },
{ name: 'provider', type: 'varchar', isNullable: true },
Expand All @@ -46,7 +44,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE TABLE \"key\" (\"kid\" varchar PRIMARY KEY NOT NULL, \"kms\" varchar NOT NULL, \"type\" varchar NOT NULL, \"publicKeyHex\" varchar NOT NULL, \"privateKeyHex\" varchar NOT NULL, \"meta\" text, \"identifierDid\" varchar, CONSTRAINT \"FK_3f40a9459b53adf1729dbd3b787\" FOREIGN KEY (\"identifierDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION)",
await queryRunner.createTable(
new Table({
name: getTableName('key'),
name: migrationGetTableName(queryRunner,'key'),
columns: [
{ name: 'kid', type: 'varchar', isPrimary: true },
{ name: 'kms', type: 'varchar' },
Expand All @@ -60,7 +58,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['identifierDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
},
],
}),
Expand All @@ -71,7 +69,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE TABLE \"service\" (\"id\" varchar PRIMARY KEY NOT NULL, \"type\" varchar NOT NULL, \"serviceEndpoint\" varchar NOT NULL, \"description\" varchar, \"identifierDid\" varchar, CONSTRAINT \"FK_e16e0280d906951809f95dd09f1\" FOREIGN KEY (\"identifierDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION)",
await queryRunner.createTable(
new Table({
name: getTableName('service'),
name: migrationGetTableName(queryRunner, 'service'),
columns: [
{ name: 'id', type: 'varchar', isPrimary: true },
{ name: 'type', type: 'varchar' },
Expand All @@ -83,7 +81,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['identifierDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
onDelete: 'cascade',
},
],
Expand All @@ -95,7 +93,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE TABLE \"credential\" (\"hash\" varchar PRIMARY KEY NOT NULL, \"raw\" text NOT NULL, \"id\" varchar, \"issuanceDate\" datetime NOT NULL, \"expirationDate\" datetime, \"context\" text NOT NULL, \"type\" text NOT NULL, \"issuerDid\" varchar, \"subjectDid\" varchar, CONSTRAINT \"FK_123d0977e0976565ee0932c0b9e\" FOREIGN KEY (\"issuerDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT \"FK_b790831f44e2fa7f9661a017b0a\" FOREIGN KEY (\"subjectDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION)",
await queryRunner.createTable(
new Table({
name: getTableName('credential'),
name: migrationGetTableName(queryRunner, 'credential'),
columns: [
{ name: 'hash', type: 'varchar', isPrimary: true },
{ name: 'raw', type: 'text' },
Expand All @@ -111,13 +109,13 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['issuerDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
onDelete: 'cascade',
},
{
columnNames: ['subjectDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
},
],
}),
Expand All @@ -128,7 +126,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE TABLE \"claim\" (\"hash\" varchar PRIMARY KEY NOT NULL, \"issuanceDate\" datetime NOT NULL, \"expirationDate\" datetime, \"context\" text NOT NULL, \"credentialType\" text NOT NULL, \"type\" varchar NOT NULL, \"value\" text, \"isObj\" boolean NOT NULL, \"issuerDid\" varchar, \"subjectDid\" varchar, \"credentialHash\" varchar, CONSTRAINT \"FK_d972c73d0f875c0d14c35b33baa\" FOREIGN KEY (\"issuerDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT \"FK_f411679379d373424100a1c73f4\" FOREIGN KEY (\"subjectDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT \"FK_3d494b79143de3d0e793883e351\" FOREIGN KEY (\"credentialHash\") REFERENCES \"credential\" (\"hash\") ON DELETE NO ACTION ON UPDATE NO ACTION)",
await queryRunner.createTable(
new Table({
name: getTableName('claim'),
name: migrationGetTableName(queryRunner, 'claim'),
columns: [
{ name: 'hash', type: 'varchar', isPrimary: true },
{ name: 'issuanceDate', type: dateTimeType },
Expand All @@ -146,18 +144,18 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['issuerDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
onDelete: 'cascade',
},
{
columnNames: ['subjectDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
},
{
columnNames: ['credentialHash'],
referencedColumnNames: ['hash'],
referencedTableName: getTableName('credential'),
referencedTableName: migrationGetTableName(queryRunner, 'credential'),
onDelete: 'cascade',
},
],
Expand All @@ -169,7 +167,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE TABLE \"presentation\" (\"hash\" varchar PRIMARY KEY NOT NULL, \"raw\" text NOT NULL, \"id\" varchar, \"issuanceDate\" datetime NOT NULL, \"expirationDate\" datetime, \"context\" text NOT NULL, \"type\" text NOT NULL, \"holderDid\" varchar, CONSTRAINT \"FK_a5e418449d9f527776a3bd0ca61\" FOREIGN KEY (\"holderDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION)",
await queryRunner.createTable(
new Table({
name: getTableName('presentation'),
name: migrationGetTableName(queryRunner, 'presentation'),
columns: [
{ name: 'hash', type: 'varchar', isPrimary: true },
{ name: 'raw', type: 'text' },
Expand All @@ -184,7 +182,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['holderDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
onDelete: 'cascade',
},
],
Expand All @@ -196,7 +194,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE TABLE \"message\" (\"id\" varchar PRIMARY KEY NOT NULL, \"saveDate\" datetime NOT NULL DEFAULT (datetime('now')), \"updateDate\" datetime NOT NULL DEFAULT (datetime('now')), \"createdAt\" datetime, \"expiresAt\" datetime, \"threadId\" varchar, \"type\" varchar NOT NULL, \"raw\" varchar, \"data\" text, \"replyTo\" text, \"replyUrl\" varchar, \"metaData\" text, \"fromDid\" varchar, \"toDid\" varchar, CONSTRAINT \"FK_63bf73143b285c727bd046e6710\" FOREIGN KEY (\"fromDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT \"FK_1a666b2c29bb2b68d91259f55df\" FOREIGN KEY (\"toDid\") REFERENCES \"identifier\" (\"did\") ON DELETE NO ACTION ON UPDATE NO ACTION)",
await queryRunner.createTable(
new Table({
name: getTableName('message'),
name: migrationGetTableName(queryRunner, 'message'),
columns: [
{ name: 'id', type: 'varchar', isPrimary: true },
{ name: 'saveDate', type: dateTimeType },
Expand All @@ -217,12 +215,12 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['fromDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
},
{
columnNames: ['toDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
},
],
}),
Expand All @@ -235,7 +233,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE INDEX \"IDX_3a460e48557bad5564504ddad9\" ON \"presentation_verifier_identifier\" (\"identifierDid\")",
await queryRunner.createTable(
new Table({
name: getTableName('presentation_verifier_identifier'),
name: migrationGetTableName(queryRunner, 'presentation_verifier_identifier'),
columns: [
{ name: 'presentationHash', type: 'varchar', isPrimary: true },
{ name: 'identifierDid', type: 'varchar', isPrimary: true },
Expand All @@ -249,13 +247,13 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['presentationHash'],
referencedColumnNames: ['hash'],
referencedTableName: getTableName('presentation'),
referencedTableName: migrationGetTableName(queryRunner, 'presentation'),
onDelete: 'cascade',
},
{
columnNames: ['identifierDid'],
referencedColumnNames: ['did'],
referencedTableName: getTableName('identifier'),
referencedTableName: migrationGetTableName(queryRunner, 'identifier'),
onDelete: 'cascade',
},
],
Expand All @@ -269,7 +267,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE INDEX \"IDX_ef88f92988763fee884c37db63\" ON \"presentation_credentials_credential\" (\"credentialHash\")",
await queryRunner.createTable(
new Table({
name: getTableName('presentation_credentials_credential'),
name: migrationGetTableName(queryRunner, 'presentation_credentials_credential'),
columns: [
{ name: 'presentationHash', type: 'varchar', isPrimary: true },
{ name: 'credentialHash', type: 'varchar', isPrimary: true },
Expand All @@ -283,13 +281,13 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['presentationHash'],
referencedColumnNames: ['hash'],
referencedTableName: getTableName('presentation'),
referencedTableName: migrationGetTableName(queryRunner, 'presentation'),
onDelete: 'cascade',
},
{
columnNames: ['credentialHash'],
referencedColumnNames: ['hash'],
referencedTableName: getTableName('credential'),
referencedTableName: migrationGetTableName(queryRunner, 'credential'),
onDelete: 'cascade',
},
],
Expand All @@ -303,7 +301,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE INDEX \"IDX_a13b5cf828c669e61faf489c18\" ON \"message_presentations_presentation\" (\"presentationHash\")",
await queryRunner.createTable(
new Table({
name: getTableName('message_presentations_presentation'),
name: migrationGetTableName(queryRunner, 'message_presentations_presentation'),
columns: [
{ name: 'messageId', type: 'varchar', isPrimary: true },
{ name: 'presentationHash', type: 'varchar', isPrimary: true },
Expand All @@ -317,13 +315,13 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['messageId'],
referencedColumnNames: ['id'],
referencedTableName: getTableName('message'),
referencedTableName: migrationGetTableName(queryRunner, 'message'),
onDelete: 'cascade',
},
{
columnNames: ['presentationHash'],
referencedColumnNames: ['hash'],
referencedTableName: getTableName('presentation'),
referencedTableName: migrationGetTableName(queryRunner, 'presentation'),
onDelete: 'cascade',
},
],
Expand All @@ -337,7 +335,7 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
// "CREATE INDEX \"IDX_8ae8195a94b667b185d2c023e3\" ON \"message_credentials_credential\" (\"credentialHash\")",
await queryRunner.createTable(
new Table({
name: getTableName('message_credentials_credential'),
name: migrationGetTableName(queryRunner, 'message_credentials_credential'),
columns: [
{ name: 'messageId', type: 'varchar', isPrimary: true },
{ name: 'credentialHash', type: 'varchar', isPrimary: true },
Expand All @@ -351,13 +349,13 @@ export class CreateDatabase1447159020001 implements MigrationInterface {
{
columnNames: ['messageId'],
referencedColumnNames: ['id'],
referencedTableName: getTableName('message'),
referencedTableName: migrationGetTableName(queryRunner, 'message'),
onDelete: 'cascade',
},
{
columnNames: ['credentialHash'],
referencedColumnNames: ['hash'],
referencedTableName: getTableName('credential'),
referencedTableName: migrationGetTableName(queryRunner, 'credential'),
onDelete: 'cascade',
},
],
Expand Down
17 changes: 8 additions & 9 deletions packages/data-store/src/migrations/2.simplifyRelations.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm'
import Debug from 'debug'
import { migrationGetExistingTableByName } from './migration-functions.js'
import { PreMigrationKey } from '../entities/PreMigrationEntities.js'

/**
* Fix inconsistencies between Entity data and column data.
*
* @public
*/
export class SimplifyRelations1447159020002 implements MigrationInterface {

name = 'SimplifyRelations1447159020002' // Used in case this class gets minified, which would change the classname

async up(queryRunner: QueryRunner): Promise<void> {
function getTableName(givenName: string): string {
return (
queryRunner.connection.entityMetadatas.find((meta) => meta.givenTableName === givenName)?.tableName ||
givenName
)
}
await queryRunner.changeColumn(
getTableName('key'),
migrationGetExistingTableByName(queryRunner, 'PreMigrationKey', true),
'identifierDid',
new TableColumn({ name: 'identifierDid', type: 'varchar', isNullable: true }),
)
await queryRunner.changeColumn(
getTableName('service'),
migrationGetExistingTableByName(queryRunner, 'service'),
'identifierDid',
new TableColumn({ name: 'identifierDid', type: 'varchar', isNullable: true }),
)

}

async down(queryRunner: QueryRunner): Promise<void> {
Expand Down
Loading

0 comments on commit 826b994

Please sign in to comment.