* Replace tsvector generated column with triggers.

* reason: due to typeorm generated column metadata bug
This commit is contained in:
Philipinho
2024-02-26 13:57:28 +00:00
parent 4008e15c04
commit 1d620eba49
4 changed files with 46 additions and 5 deletions

View File

@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddTsvectorColumn1706450034470 implements MigrationInterface {
name = 'AddTsvectorColumn1706450034470';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "pages" ADD "tsv" tsvector`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "pages" DROP COLUMN "tsv"`);
}
}

View File

@ -0,0 +1,28 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddTsvectorTrigger1706450034471 implements MigrationInterface {
name = 'AddTsvectorTrigger1706450034471';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE FUNCTION pages_tsvector_trigger() RETURNS trigger AS $$
begin
new.tsv :=
setweight(to_tsvector('english', coalesce(new.title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(new.\"textContent\", '')), 'B');
return new;
end;
$$ LANGUAGE plpgsql;
`);
await queryRunner.query(`
CREATE TRIGGER pages_tsvector_update BEFORE INSERT OR UPDATE
ON pages FOR EACH ROW EXECUTE FUNCTION pages_tsvector_trigger();
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TRIGGER pages_tsvector_update ON Pages`);
await queryRunner.query(`DROP FUNCTION pages_tsvector_trigger`);
}
}

View File

@ -4,10 +4,11 @@ export class AddIndexTSVColumn1706453158729 implements MigrationInterface {
name = 'AddIndexTSVColumn1706453158729'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE INDEX pages_tsv_index ON pages USING GIN ("tsv");`);
await queryRunner.query(`DROP INDEX IF EXISTS pages_tsv_idx;`);
await queryRunner.query(`CREATE INDEX pages_tsv_idx ON pages USING GIN ("tsv");`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX IF EXISTS pages_tsv_index;`);
await queryRunner.query(`DROP INDEX IF EXISTS pages_tsv_idx;`);
}
}