membersByHandle.migration.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { MigrationInterface, QueryRunner } from "typeorm";
  2. export class MembersByHandleMigration1617981663821 implements MigrationInterface {
  3. name = 'membersByHandleMigration1617981663821'
  4. public async up(queryRunner: QueryRunner): Promise<void> {
  5. // TODO: escape
  6. await queryRunner.query(`
  7. ALTER TABLE membership
  8. ADD COLUMN members_by_handle_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 membership
  16. ADD COLUMN members_by_handle_doc text
  17. GENERATED ALWAYS AS (
  18. coalesce("handle", '')
  19. )
  20. STORED;
  21. `);
  22. await queryRunner.query(`CREATE INDEX members_by_handle_membership_idx ON membership USING GIN (members_by_handle_tsv)`);
  23. await queryRunner.query(`CREATE INDEX membership_id_idx ON membership (('membership' || '_' || id))`);
  24. await queryRunner.query(`
  25. CREATE VIEW members_by_handle_view AS
  26. SELECT
  27. text 'membership' AS origin_table, 'membership' || '_' || id AS unique_id, id, members_by_handle_tsv AS tsv, members_by_handle_doc AS document
  28. FROM
  29. membership
  30. `);
  31. }
  32. public async down(queryRunner: QueryRunner): Promise<void> {
  33. await queryRunner.query(`DROP VIEW members_by_handle_view`);
  34. await queryRunner.query(`DROP INDEX members_by_handle_membership_idx`);
  35. await queryRunner.query(`DROP INDEX membership_id_idx`);
  36. await queryRunner.query(`ALTER TABLE membership DROP COLUMN members_by_handle_tsv`);
  37. await queryRunner.query(`ALTER TABLE membership DROP COLUMN members_by_handle_doc`);
  38. }
  39. }