videoCategoriesByName.migration.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { MigrationInterface, QueryRunner } from "typeorm";
  2. export class VideoCategoriesByNameMigration1617981663821 implements MigrationInterface {
  3. name = 'videoCategoriesByNameMigration1617981663821'
  4. public async up(queryRunner: QueryRunner): Promise<void> {
  5. // TODO: escape
  6. await queryRunner.query(`
  7. ALTER TABLE video_category
  8. ADD COLUMN video_categories_by_name_tsv tsvector
  9. GENERATED ALWAYS AS (
  10. setweight(to_tsvector('english', coalesce("name", '')), 'A')
  11. )
  12. STORED;
  13. `);
  14. await queryRunner.query(`
  15. ALTER TABLE video_category
  16. ADD COLUMN video_categories_by_name_doc text
  17. GENERATED ALWAYS AS (
  18. coalesce("name", '')
  19. )
  20. STORED;
  21. `);
  22. await queryRunner.query(`CREATE INDEX video_categories_by_name_video_category_idx ON video_category USING GIN (video_categories_by_name_tsv)`);
  23. await queryRunner.query(`CREATE INDEX video_category_id_idx ON video_category (('video_category' || '_' || id))`);
  24. await queryRunner.query(`
  25. CREATE VIEW video_categories_by_name_view AS
  26. SELECT
  27. text 'video_category' AS origin_table, 'video_category' || '_' || id AS unique_id, id, video_categories_by_name_tsv AS tsv, video_categories_by_name_doc AS document
  28. FROM
  29. video_category
  30. `);
  31. }
  32. public async down(queryRunner: QueryRunner): Promise<void> {
  33. await queryRunner.query(`DROP VIEW video_categories_by_name_view`);
  34. await queryRunner.query(`DROP INDEX video_categories_by_name_video_category_idx`);
  35. await queryRunner.query(`DROP INDEX video_category_id_idx`);
  36. await queryRunner.query(`ALTER TABLE video_category DROP COLUMN video_categories_by_name_tsv`);
  37. await queryRunner.query(`ALTER TABLE video_category DROP COLUMN video_categories_by_name_doc`);
  38. }
  39. }