* 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

@ -8,6 +8,7 @@ import {
JoinColumn, JoinColumn,
OneToMany, OneToMany,
DeleteDateColumn, DeleteDateColumn,
Index,
} from 'typeorm'; } from 'typeorm';
import { User } from '../../user/entities/user.entity'; import { User } from '../../user/entities/user.entity';
import { Workspace } from '../../workspace/entities/workspace.entity'; import { Workspace } from '../../workspace/entities/workspace.entity';
@ -15,6 +16,7 @@ import { Comment } from '../../comment/entities/comment.entity';
import { PageHistory } from './page-history.entity'; import { PageHistory } from './page-history.entity';
@Entity('pages') @Entity('pages')
@Index('pages_tsv_idx', ['tsv'])
export class Page { export class Page {
@PrimaryGeneratedColumn('uuid') @PrimaryGeneratedColumn('uuid')
id: string; id: string;
@ -36,9 +38,6 @@ export class Page {
@Column({ @Column({
type: 'tsvector', type: 'tsvector',
generatedType: 'STORED',
asExpression:
"setweight(to_tsvector('english', coalesce(pages.title, '')), 'A') || setweight(to_tsvector('english', coalesce(pages.\"textContent\", '')), 'B')",
select: false, select: false,
nullable: true, nullable: true,
}) })

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' name = 'AddIndexTSVColumn1706453158729'
public async up(queryRunner: QueryRunner): Promise<void> { 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> { 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;`);
} }
} }