handles.migration.ts 1.5 KB

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