titles.migration.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { MigrationInterface, QueryRunner } from "typeorm";
  2. export class TitlesMigration1603710751027 implements MigrationInterface {
  3. name = 'titlesMigration1603710751027'
  4. public async up(queryRunner: QueryRunner): Promise<void> {
  5. // TODO: escape
  6. await queryRunner.query(`
  7. ALTER TABLE channel
  8. ADD COLUMN titles_tsv tsvector
  9. GENERATED ALWAYS AS (
  10. setweight(to_tsvector('english', coalesce("title", '')), 'A')
  11. )
  12. STORED;
  13. `);
  14. await queryRunner.query(`
  15. ALTER TABLE channel
  16. ADD COLUMN titles_doc text
  17. GENERATED ALWAYS AS (
  18. coalesce("title", '')
  19. )
  20. STORED;
  21. `);
  22. await queryRunner.query(`CREATE INDEX titles_channel_idx ON channel USING GIN (titles_tsv)`);
  23. await queryRunner.query(`
  24. ALTER TABLE video
  25. ADD COLUMN titles_tsv tsvector
  26. GENERATED ALWAYS AS (
  27. setweight(to_tsvector('english', coalesce("title", '')), 'A')
  28. )
  29. STORED;
  30. `);
  31. await queryRunner.query(`
  32. ALTER TABLE video
  33. ADD COLUMN titles_doc text
  34. GENERATED ALWAYS AS (
  35. coalesce("title", '')
  36. )
  37. STORED;
  38. `);
  39. await queryRunner.query(`CREATE INDEX titles_video_idx ON video USING GIN (titles_tsv)`);
  40. await queryRunner.query(`
  41. CREATE VIEW titles_view AS
  42. SELECT
  43. text 'channel' AS origin_table, id, titles_tsv AS tsv, titles_doc AS document
  44. FROM
  45. channel
  46. UNION ALL
  47. SELECT
  48. text 'video' AS origin_table, id, titles_tsv AS tsv, titles_doc AS document
  49. FROM
  50. video
  51. `);
  52. }
  53. public async down(queryRunner: QueryRunner): Promise<void> {
  54. await queryRunner.query(`DROP VIEW titles_view`);
  55. await queryRunner.query(`DROP INDEX titles_channel_idx`);
  56. await queryRunner.query(`ALTER TABLE channel DROP COLUMN titles_tsv`);
  57. await queryRunner.query(`ALTER TABLE channel DROP COLUMN titles_doc`);
  58. await queryRunner.query(`DROP INDEX titles_video_idx`);
  59. await queryRunner.query(`ALTER TABLE video DROP COLUMN titles_tsv`);
  60. await queryRunner.query(`ALTER TABLE video DROP COLUMN titles_doc`);
  61. }
  62. }