1669113322388-init.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { MigrationInterface, QueryRunner } from 'typeorm'
  2. export class init1669113322388 implements MigrationInterface {
  3. name = 'init1669113322388'
  4. public async up(queryRunner: QueryRunner): Promise<void> {
  5. await queryRunner.query(
  6. 'CREATE TABLE `revisions_revisions` (`uuid` varchar(36) NOT NULL, `item_uuid` varchar(36) NOT NULL, `user_uuid` varchar(36) NULL, `content` mediumtext NULL, `content_type` varchar(255) NULL, `items_key_id` varchar(255) NULL, `enc_item_key` text NULL, `auth_hash` varchar(255) NULL, `creation_date` date NULL, `created_at` datetime(6) NULL, `updated_at` datetime(6) NULL, INDEX `item_uuid` (`item_uuid`), INDEX `user_uuid` (`user_uuid`), INDEX `creation_date` (`creation_date`), INDEX `created_at` (`created_at`), PRIMARY KEY (`uuid`)) ENGINE=InnoDB',
  7. )
  8. await this.syncSchemaBetweenLegacyRevisions(queryRunner)
  9. }
  10. public async down(queryRunner: QueryRunner): Promise<void> {
  11. await queryRunner.query('DROP INDEX `created_at` ON `revisions_revisions`')
  12. await queryRunner.query('DROP INDEX `creation_date` ON `revisions_revisions`')
  13. await queryRunner.query('DROP INDEX `user_uuid` ON `revisions_revisions`')
  14. await queryRunner.query('DROP INDEX `item_uuid` ON `revisions_revisions`')
  15. await queryRunner.query('DROP TABLE `revisions_revisions`')
  16. }
  17. private async syncSchemaBetweenLegacyRevisions(queryRunner: QueryRunner): Promise<void> {
  18. const revisionsTableExistsQueryResult = await queryRunner.manager.query(
  19. 'SELECT COUNT(*) as count FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = "revisions"',
  20. )
  21. const revisionsTableExists = revisionsTableExistsQueryResult[0].count === 1
  22. if (!revisionsTableExists) {
  23. return
  24. }
  25. await queryRunner.query(
  26. 'INSERT INTO `revisions_revisions`(`uuid`, `item_uuid`, `user_uuid`, `content`, `content_type`, `items_key_id`, `enc_item_key`, `auth_hash`, `creation_date`, `created_at`, `updated_at`) SELECT `uuid`, `item_uuid`, NULL, `content`, `content_type`, `items_key_id`, `enc_item_key`, `auth_hash`, `creation_date`, `created_at`, `updated_at` FROM `revisions`',
  27. )
  28. await queryRunner.query('DROP TABLE `revisions`')
  29. }
  30. }