1669113322388-init.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 this.syncSchemaBetweenLegacyRevisions(queryRunner)
  6. await queryRunner.query(
  7. 'CREATE TABLE IF NOT EXISTS `revisions` (`uuid` varchar(36) NOT NULL, `item_uuid` varchar(36) NOT NULL, `user_uuid` varchar(36) NOT 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',
  8. )
  9. }
  10. public async down(queryRunner: QueryRunner): Promise<void> {
  11. await queryRunner.query('DROP INDEX `created_at` ON `revisions`')
  12. await queryRunner.query('DROP INDEX `creation_date` ON `revisions`')
  13. await queryRunner.query('DROP INDEX `user_uuid` ON `revisions`')
  14. await queryRunner.query('DROP INDEX `item_uuid` ON `revisions`')
  15. await queryRunner.query('DROP TABLE `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. const revisionsTableHasUserUuidColumnQueryResult = await queryRunner.manager.query(
  26. 'SELECT COUNT(*) as count FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = "revisions" AND column_name = "user_uuid"',
  27. )
  28. const revisionsTableHasUserUuidColumn = revisionsTableHasUserUuidColumnQueryResult[0].count === 1
  29. if (revisionsTableHasUserUuidColumn) {
  30. return
  31. }
  32. await queryRunner.query('ALTER TABLE `revisions` ADD COLUMN `user_uuid` varchar(36) NULL')
  33. }
  34. }