names.migration.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { MigrationInterface, QueryRunner } from "typeorm";
  2. export class NamesMigration1603710751018 implements MigrationInterface {
  3. name = 'namesMigration1603710751018'
  4. public async up(queryRunner: QueryRunner): Promise<void> {
  5. // TODO: escape
  6. await queryRunner.query(`
  7. ALTER TABLE category
  8. ADD COLUMN names_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 category
  16. ADD COLUMN names_doc text
  17. GENERATED ALWAYS AS (
  18. coalesce("name", '')
  19. )
  20. STORED;
  21. `);
  22. await queryRunner.query(`CREATE INDEX names_category_idx ON category USING GIN (names_tsv)`);
  23. await queryRunner.query(`
  24. CREATE VIEW names_view AS
  25. SELECT
  26. text 'category' AS origin_table, id, names_tsv AS tsv, names_doc AS document
  27. FROM
  28. category
  29. `);
  30. }
  31. public async down(queryRunner: QueryRunner): Promise<void> {
  32. await queryRunner.query(`DROP VIEW names_view`);
  33. await queryRunner.query(`DROP INDEX names_category_idx`);
  34. await queryRunner.query(`ALTER TABLE category DROP COLUMN names_tsv`);
  35. await queryRunner.query(`ALTER TABLE category DROP COLUMN names_doc`);
  36. }
  37. }