From 259f5d127dba88e07df36c7696e379ecf71d3a3b Mon Sep 17 00:00:00 2001 From: mertalev <101130780+mertalev@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:49:28 -0500 Subject: [PATCH] tweaks --- ...78-RemoveStrictKeywordFromEarthFunction.ts | 17 ++++++++ server/src/repositories/map.repository.ts | 43 ++++++++----------- 2 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 server/src/migrations/1732128889378-RemoveStrictKeywordFromEarthFunction.ts diff --git a/server/src/migrations/1732128889378-RemoveStrictKeywordFromEarthFunction.ts b/server/src/migrations/1732128889378-RemoveStrictKeywordFromEarthFunction.ts new file mode 100644 index 0000000000..b417f5cf0a --- /dev/null +++ b/server/src/migrations/1732128889378-RemoveStrictKeywordFromEarthFunction.ts @@ -0,0 +1,17 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class RemoveStrictKeywordFromEarthFunction1732128889378 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE OR REPLACE FUNCTION ll_to_earth_public(latitude double precision, longitude double precision) RETURNS public.earth PARALLEL SAFE IMMUTABLE LANGUAGE SQL AS $$ + SELECT public.cube(public.cube(public.cube(public.earth()*cos(radians(latitude))*cos(radians(longitude))),public.earth()*cos(radians(latitude))*sin(radians(longitude))),public.earth()*sin(radians(latitude)))::public.earth + $$`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE OR REPLACE FUNCTION ll_to_earth_public(latitude double precision, longitude double precision) RETURNS public.earth PARALLEL SAFE IMMUTABLE STRICT LANGUAGE SQL AS $$ + SELECT public.cube(public.cube(public.cube(public.earth()*cos(radians(latitude))*cos(radians(longitude))),public.earth()*cos(radians(latitude))*sin(radians(longitude))),public.earth()*sin(radians(latitude)))::public.earth + $$`); + } +} diff --git a/server/src/repositories/map.repository.ts b/server/src/repositories/map.repository.ts index 348736a33d..f9a1177cbf 100644 --- a/server/src/repositories/map.repository.ts +++ b/server/src/repositories/map.repository.ts @@ -218,8 +218,26 @@ export class MapRepository implements IMapRepository { await this.dataSource.query( 'CREATE UNLOGGED TABLE geodata_places_tmp (LIKE geodata_places INCLUDING ALL EXCLUDING INDEXES)', ); + await this.dataSource.query(` + CREATE INDEX IDX_geodata_gist_earthcoord_${randomUUID().replaceAll('-', '_')} + ON geodata_places_tmp + USING gist (ll_to_earth_public(latitude, longitude))`); await this.loadCities500(admin1, admin2); - await this.createGeodataIndices(); + await Promise.all([ + this.dataSource.query('ALTER TABLE geodata_places_tmp ADD PRIMARY KEY (id) WITH (FILLFACTOR = 100)'), + this.dataSource.query(` + CREATE INDEX idx_geodata_places_name_${randomUUID().replaceAll('-', '_')} + ON geodata_places_tmp + USING gin (f_unaccent(name) gin_trgm_ops)`), + this.dataSource.query(` + CREATE INDEX idx_geodata_places_admin1_name_${randomUUID().replaceAll('-', '_')} + ON geodata_places_tmp + USING gin (f_unaccent("admin1Name") gin_trgm_ops)`), + this.dataSource.query(` + CREATE INDEX idx_geodata_places_admin2_name_${randomUUID().replaceAll('-', '_')} + ON geodata_places_tmp + USING gin (f_unaccent("admin2Name") gin_trgm_ops)`), + ]); await this.dataSource.transaction(async (manager) => { await manager.query('ALTER TABLE geodata_places RENAME TO geodata_places_old'); @@ -300,27 +318,4 @@ export class MapRepository implements IMapRepository { return adminMap; } - - private createGeodataIndices() { - return Promise.all([ - this.dataSource.query(`ALTER TABLE geodata_places_tmp ADD PRIMARY KEY (id) WITH (FILLFACTOR = 100)`), - this.dataSource.query(` - CREATE INDEX IDX_geodata_gist_earthcoord_${randomUUID().replaceAll('-', '_')} - ON geodata_places_tmp - USING gist (ll_to_earth_public(latitude, longitude)) - WITH (fillfactor = 100)`), - this.dataSource.query(` - CREATE INDEX idx_geodata_places_name_${randomUUID().replaceAll('-', '_')} - ON geodata_places_tmp - USING gin (f_unaccent(name) gin_trgm_ops)`), - this.dataSource.query(` - CREATE INDEX idx_geodata_places_admin1_name_${randomUUID().replaceAll('-', '_')} - ON geodata_places_tmp - USING gin (f_unaccent("admin1Name") gin_trgm_ops)`), - this.dataSource.query(` - CREATE INDEX idx_geodata_places_admin2_name_${randomUUID().replaceAll('-', '_')} - ON geodata_places_tmp - USING gin (f_unaccent("admin2Name") gin_trgm_ops)`), - ]); - } }