From 26422f878f7d6c2f7c51c5b7d9803de61654ccfb Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Tue, 8 Aug 2023 11:13:00 -0300 Subject: [PATCH 1/3] adding data migrations --- packages/data-store/src/helpers.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/data-store/src/helpers.ts b/packages/data-store/src/helpers.ts index 2b473880..7075d206 100644 --- a/packages/data-store/src/helpers.ts +++ b/packages/data-store/src/helpers.ts @@ -6,6 +6,7 @@ import {NetworkEntity} from './entities/network.entity'; import {DocumentEntity} from './entities/document/document.entity'; import {DocumentTypeEntity} from './entities/document-type.entity'; import {WalletEntity} from './entities/wallet.entity'; +import typeOrmMigrations from './migrations'; export function documentHasType(document: any, type: string) { if (Array.isArray(document.type)) { @@ -15,7 +16,7 @@ export function documentHasType(document: any, type: string) { return document.type === type; } -export async function initializeTypeORM(options: DataStoreConfigs) { +export function getDataSource(options: DataStoreConfigs) { const dataSource = new DataSource({ type: (options.dbType as any) || 'sqlite', database: options.databasePath, @@ -24,9 +25,17 @@ export async function initializeTypeORM(options: DataStoreConfigs) { dropSchema: options.dropSchema, driver: options.driver, sqlJsConfig: options.sqlJsConfig, + migrationsRun: process.env.NODE_ENV === 'test', + migrations: typeOrmMigrations, ...(options.typeORMConfigs || {}), }); + return dataSource; +} + +export async function initializeTypeORM(options: DataStoreConfigs) { + const dataSource = getDataSource(options); + await dataSource .initialize() .then(() => { From 81deef54bb4caa90c8124713d020fcef8c46eae8 Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Tue, 8 Aug 2023 11:13:13 -0300 Subject: [PATCH 2/3] adding data migrations --- packages/data-store/1691497790337-migration1.ts | 11 +++++++++++ packages/data-store/src/migrations-data-source.ts | 6 ++++++ 2 files changed, 17 insertions(+) create mode 100644 packages/data-store/1691497790337-migration1.ts create mode 100644 packages/data-store/src/migrations-data-source.ts diff --git a/packages/data-store/1691497790337-migration1.ts b/packages/data-store/1691497790337-migration1.ts new file mode 100644 index 00000000..3a7ad845 --- /dev/null +++ b/packages/data-store/1691497790337-migration1.ts @@ -0,0 +1,11 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class Migration11691497790337 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + } + + public async down(queryRunner: QueryRunner): Promise { + } + +} diff --git a/packages/data-store/src/migrations-data-source.ts b/packages/data-store/src/migrations-data-source.ts new file mode 100644 index 00000000..e270373a --- /dev/null +++ b/packages/data-store/src/migrations-data-source.ts @@ -0,0 +1,6 @@ +import {getDataSource} from './helpers'; + +export default getDataSource({ + dbType: 'sqlite', + databasePath: 'data-store.sqlite', +}); From 014a713bc4d719a0937af214bd1a0a326ac60705 Mon Sep 17 00:00:00 2001 From: Maycon Mello Date: Tue, 8 Aug 2023 11:15:23 -0300 Subject: [PATCH 3/3] adding data migrations --- .../data-store/1691497790337-migration1.ts | 11 --- .../src/migrations/1691498362273-bootstrap.ts | 94 +++++++++++++++++++ packages/data-store/src/migrations/index.ts | 3 + 3 files changed, 97 insertions(+), 11 deletions(-) delete mode 100644 packages/data-store/1691497790337-migration1.ts create mode 100644 packages/data-store/src/migrations/1691498362273-bootstrap.ts create mode 100644 packages/data-store/src/migrations/index.ts diff --git a/packages/data-store/1691497790337-migration1.ts b/packages/data-store/1691497790337-migration1.ts deleted file mode 100644 index 3a7ad845..00000000 --- a/packages/data-store/1691497790337-migration1.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { MigrationInterface, QueryRunner } from "typeorm" - -export class Migration11691497790337 implements MigrationInterface { - - public async up(queryRunner: QueryRunner): Promise { - } - - public async down(queryRunner: QueryRunner): Promise { - } - -} diff --git a/packages/data-store/src/migrations/1691498362273-bootstrap.ts b/packages/data-store/src/migrations/1691498362273-bootstrap.ts new file mode 100644 index 00000000..746666b0 --- /dev/null +++ b/packages/data-store/src/migrations/1691498362273-bootstrap.ts @@ -0,0 +1,94 @@ +import {MigrationInterface, QueryRunner} from 'typeorm'; + +export class Bootstrap1691498362273 implements MigrationInterface { + name = 'Bootstrap1691498362273'; + + public async up(queryRunner: QueryRunner): Promise { + console.log('Running table bootstrap migration'); + + try { + const result = await queryRunner.query(`SELECT * FROM "network_entity"`); + console.log('Table already bootstrapped'); + console.log(result); + return; + } catch (err) { + console.error(err); + } + + console.log('Running table bootstrap migration'); + + await queryRunner.query( + `CREATE TABLE "network_entity" ("id" varchar PRIMARY KEY NOT NULL, "name" varchar NOT NULL, "configs" varchar NOT NULL)`, + ); + await queryRunner.query( + `CREATE TABLE "document_type_entity" ("id" text PRIMARY KEY NOT NULL)`, + ); + await queryRunner.query( + `CREATE TABLE "document_entity" ("id" text PRIMARY KEY NOT NULL, "networkId" text, "type" text NOT NULL, "correlation" text NOT NULL, "data" blob NOT NULL)`, + ); + await queryRunner.query( + `CREATE TABLE "wallet_entity" ("id" varchar PRIMARY KEY NOT NULL, "version" varchar, "networkId" varchar NOT NULL)`, + ); + await queryRunner.query( + `CREATE TABLE "document_entity__type_rel_document_type_entity" ("documentEntityId" text NOT NULL, "documentTypeEntityId" text NOT NULL, PRIMARY KEY ("documentEntityId", "documentTypeEntityId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_e929f32563f62d753a51bcd8b9" ON "document_entity__type_rel_document_type_entity" ("documentEntityId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_7d377ef9ddb323247aedd63d66" ON "document_entity__type_rel_document_type_entity" ("documentTypeEntityId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_e929f32563f62d753a51bcd8b9"`); + await queryRunner.query(`DROP INDEX "IDX_7d377ef9ddb323247aedd63d66"`); + await queryRunner.query( + `CREATE TABLE "temporary_document_entity__type_rel_document_type_entity" ("documentEntityId" text NOT NULL, "documentTypeEntityId" text NOT NULL, CONSTRAINT "FK_e929f32563f62d753a51bcd8b9b" FOREIGN KEY ("documentEntityId") REFERENCES "document_entity" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_7d377ef9ddb323247aedd63d663" FOREIGN KEY ("documentTypeEntityId") REFERENCES "document_type_entity" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION, PRIMARY KEY ("documentEntityId", "documentTypeEntityId"))`, + ); + await queryRunner.query( + `INSERT INTO "temporary_document_entity__type_rel_document_type_entity"("documentEntityId", "documentTypeEntityId") SELECT "documentEntityId", "documentTypeEntityId" FROM "document_entity__type_rel_document_type_entity"`, + ); + await queryRunner.query( + `DROP TABLE "document_entity__type_rel_document_type_entity"`, + ); + await queryRunner.query( + `ALTER TABLE "temporary_document_entity__type_rel_document_type_entity" RENAME TO "document_entity__type_rel_document_type_entity"`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_e929f32563f62d753a51bcd8b9" ON "document_entity__type_rel_document_type_entity" ("documentEntityId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_7d377ef9ddb323247aedd63d66" ON "document_entity__type_rel_document_type_entity" ("documentTypeEntityId") `, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_7d377ef9ddb323247aedd63d66"`); + await queryRunner.query(`DROP INDEX "IDX_e929f32563f62d753a51bcd8b9"`); + await queryRunner.query( + `ALTER TABLE "document_entity__type_rel_document_type_entity" RENAME TO "temporary_document_entity__type_rel_document_type_entity"`, + ); + await queryRunner.query( + `CREATE TABLE "document_entity__type_rel_document_type_entity" ("documentEntityId" text NOT NULL, "documentTypeEntityId" text NOT NULL, PRIMARY KEY ("documentEntityId", "documentTypeEntityId"))`, + ); + await queryRunner.query( + `INSERT INTO "document_entity__type_rel_document_type_entity"("documentEntityId", "documentTypeEntityId") SELECT "documentEntityId", "documentTypeEntityId" FROM "temporary_document_entity__type_rel_document_type_entity"`, + ); + await queryRunner.query( + `DROP TABLE "temporary_document_entity__type_rel_document_type_entity"`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_7d377ef9ddb323247aedd63d66" ON "document_entity__type_rel_document_type_entity" ("documentTypeEntityId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_e929f32563f62d753a51bcd8b9" ON "document_entity__type_rel_document_type_entity" ("documentEntityId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_7d377ef9ddb323247aedd63d66"`); + await queryRunner.query(`DROP INDEX "IDX_e929f32563f62d753a51bcd8b9"`); + await queryRunner.query( + `DROP TABLE "document_entity__type_rel_document_type_entity"`, + ); + await queryRunner.query(`DROP TABLE "wallet_entity"`); + await queryRunner.query(`DROP TABLE "document_entity"`); + await queryRunner.query(`DROP TABLE "document_type_entity"`); + await queryRunner.query(`DROP TABLE "network_entity"`); + } +} diff --git a/packages/data-store/src/migrations/index.ts b/packages/data-store/src/migrations/index.ts new file mode 100644 index 00000000..8ad481a2 --- /dev/null +++ b/packages/data-store/src/migrations/index.ts @@ -0,0 +1,3 @@ +import {Bootstrap1691498362273} from './1691498362273-bootstrap'; + +export default [Bootstrap1691498362273];