diff --git a/editoast/migrations/2023-12-18-145922_search_op_rename_table/down.sql b/editoast/migrations/2023-12-18-145922_search_op_rename_table/down.sql new file mode 100644 index 00000000000..34e520e4fd0 --- /dev/null +++ b/editoast/migrations/2023-12-18-145922_search_op_rename_table/down.sql @@ -0,0 +1,11 @@ +ALTER TRIGGER search_operational_point__ins_trig ON infra_object_operational_point +RENAME TO search_operationalpoint__ins_trig; + +ALTER TRIGGER search_operational_point__upd_trig ON infra_object_operational_point +RENAME TO search_operationalpoint__upd_trig; + +ALTER FUNCTION search_operational_point__ins_trig_fun () +RENAME TO search_operationalpoint__ins_trig_fun; + +ALTER FUNCTION search_operational_point__upd_trig_fun () +RENAME TO search_operationalpoint__upd_trig_fun; diff --git a/editoast/migrations/2023-12-18-145922_search_op_rename_table/up.sql b/editoast/migrations/2023-12-18-145922_search_op_rename_table/up.sql new file mode 100644 index 00000000000..e5975db78da --- /dev/null +++ b/editoast/migrations/2023-12-18-145922_search_op_rename_table/up.sql @@ -0,0 +1,11 @@ +ALTER TRIGGER search_operationalpoint__ins_trig ON infra_object_operational_point +RENAME TO search_operational_point__ins_trig; + +ALTER TRIGGER search_operationalpoint__upd_trig ON infra_object_operational_point +RENAME TO search_operational_point__upd_trig; + +ALTER FUNCTION search_operationalpoint__ins_trig_fun () +RENAME TO search_operational_point__ins_trig_fun; + +ALTER FUNCTION search_operationalpoint__upd_trig_fun () +RENAME TO search_operational_point__upd_trig_fun; diff --git a/editoast/migrations/2023-12-19-122428_search_op_ci_code/down.sql b/editoast/migrations/2023-12-19-122428_search_op_ci_code/down.sql new file mode 100644 index 00000000000..ef3f892f02e --- /dev/null +++ b/editoast/migrations/2023-12-19-122428_search_op_ci_code/down.sql @@ -0,0 +1,7 @@ +-- DO NOT EDIT THIS FILE MANUALLY! + +DROP TABLE IF EXISTS "search_operational_point"; +DROP TRIGGER IF EXISTS search_operational_point__ins_trig ON "infra_object_operational_point"; +DROP TRIGGER IF EXISTS search_operational_point__upd_trig ON "infra_object_operational_point"; +DROP FUNCTION IF EXISTS search_operational_point__ins_trig_fun; +DROP FUNCTION IF EXISTS search_operational_point__upd_trig_fun; diff --git a/editoast/migrations/2023-12-19-122428_search_op_ci_code/up.sql b/editoast/migrations/2023-12-19-122428_search_op_ci_code/up.sql new file mode 100644 index 00000000000..f97212180fd --- /dev/null +++ b/editoast/migrations/2023-12-19-122428_search_op_ci_code/up.sql @@ -0,0 +1,85 @@ +-- DO NOT EDIT THIS FILE MANUALLY! +-- To change the migration's content, use `editoast search make-migration`. +-- To add custom SQL code, check out `#[derive(Search)]` attributes `prepend_sql` and `append_sql`. + +DROP TABLE IF EXISTS "search_operational_point"; + +CREATE TABLE "search_operational_point" ( + id BIGINT PRIMARY KEY REFERENCES "infra_object_operational_point"("id") ON UPDATE CASCADE ON DELETE CASCADE, + "obj_id" varchar(255), + "infra_id" integer, + "uic" integer, + "trigram" varchar(3), + "ci" integer, + "ch" text, + "name" text +); + +CREATE INDEX "search_operational_point_obj_id" ON "search_operational_point" ("obj_id"); +CREATE INDEX "search_operational_point_infra_id" ON "search_operational_point" ("infra_id"); +CREATE INDEX "search_operational_point_uic" ON "search_operational_point" ("uic"); +CREATE INDEX "search_operational_point_trigram" ON "search_operational_point" ("trigram"); +CREATE INDEX "search_operational_point_ci" ON "search_operational_point" ("ci"); +CREATE INDEX "search_operational_point_ch" ON "search_operational_point" ("ch"); +CREATE INDEX "search_operational_point_name" ON "search_operational_point" USING gin ("name" gin_trgm_ops); + +CREATE OR REPLACE FUNCTION search_operational_point__ins_trig_fun() + RETURNS TRIGGER + LANGUAGE plpgsql +AS $$ +BEGIN + INSERT INTO "search_operational_point" (id, obj_id, infra_id, uic, trigram, ci, ch, name) + SELECT "infra_object_operational_point".id AS id, (infra_object_operational_point.obj_id) AS obj_id, + (infra_object_operational_point.infra_id) AS infra_id, + ((infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer) AS uic, + (infra_object_operational_point.data->'extensions'->'sncf'->>'trigram') AS trigram, + ((infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer) AS ci, + (infra_object_operational_point.data->'extensions'->'sncf'->>'ch') AS ch, + osrd_prepare_for_search(infra_object_operational_point.data->'extensions'->'identifier'->>'name') AS name + FROM (SELECT NEW.*) AS "infra_object_operational_point" + ; + RETURN NEW; +END; +$$; +CREATE OR REPLACE TRIGGER search_operational_point__ins_trig +AFTER INSERT ON "infra_object_operational_point" +FOR EACH ROW EXECUTE FUNCTION search_operational_point__ins_trig_fun(); + + +CREATE OR REPLACE FUNCTION search_operational_point__upd_trig_fun() + RETURNS TRIGGER + LANGUAGE plpgsql +AS $$ +BEGIN + UPDATE "search_operational_point" + SET "obj_id" = (infra_object_operational_point.obj_id), + "infra_id" = (infra_object_operational_point.infra_id), + "uic" = ((infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer), + "trigram" = (infra_object_operational_point.data->'extensions'->'sncf'->>'trigram'), + "ci" = ((infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer), + "ch" = (infra_object_operational_point.data->'extensions'->'sncf'->>'ch'), + "name" = osrd_prepare_for_search(infra_object_operational_point.data->'extensions'->'identifier'->>'name') + FROM (SELECT NEW.*) AS "infra_object_operational_point" + + WHERE "infra_object_operational_point".id = "search_operational_point".id; + RETURN NEW; +END; +$$; +CREATE OR REPLACE TRIGGER search_operational_point__upd_trig +AFTER UPDATE ON "infra_object_operational_point" +FOR EACH ROW EXECUTE FUNCTION search_operational_point__upd_trig_fun(); + + + +INSERT INTO "search_operational_point" +SELECT + "infra_object_operational_point"."id" AS id, + (infra_object_operational_point.obj_id) AS obj_id +, (infra_object_operational_point.infra_id) AS infra_id +, ((infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer) AS uic +, (infra_object_operational_point.data->'extensions'->'sncf'->>'trigram') AS trigram +, ((infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer) AS ci +, (infra_object_operational_point.data->'extensions'->'sncf'->>'ch') AS ch +, osrd_prepare_for_search(infra_object_operational_point.data->'extensions'->'identifier'->>'name') AS name +FROM "infra_object_operational_point" + ; diff --git a/editoast/openapi.yaml b/editoast/openapi.yaml index 0c16c0403a8..6ac2e4f48a1 100644 --- a/editoast/openapi.yaml +++ b/editoast/openapi.yaml @@ -2388,6 +2388,10 @@ components: properties: ch: type: string + ci: + format: int64 + minimum: 0 + type: integer geographic: $ref: '#/components/schemas/GeoJsonPoint' infra_id: @@ -2424,6 +2428,7 @@ components: - name - trigram - ch + - ci - geographic - schematic - track_sections diff --git a/editoast/src/tables.rs b/editoast/src/tables.rs index b02e18764e2..345ad67cb31 100644 --- a/editoast/src/tables.rs +++ b/editoast/src/tables.rs @@ -477,14 +477,15 @@ diesel::table! { search_operational_point (id) { id -> Int8, - name -> Nullable, - infra_id -> Nullable, #[max_length = 255] obj_id -> Nullable, + infra_id -> Nullable, uic -> Nullable, - ch -> Nullable, #[max_length = 3] trigram -> Nullable, + ci -> Nullable, + ch -> Nullable, + name -> Nullable, } } diff --git a/editoast/src/views/search/objects.rs b/editoast/src/views/search/objects.rs index 560fa22c5ac..bb8231d4d95 100644 --- a/editoast/src/views/search/objects.rs +++ b/editoast/src/views/search/objects.rs @@ -38,16 +38,47 @@ pub(super) struct SearchResultItemTrack { #[search( name = "operationalpoint", table = "search_operational_point", + migration(src_table = "infra_object_operational_point"), joins = " INNER JOIN infra_object_operational_point AS OP ON OP.id = search_operational_point.id INNER JOIN (SELECT DISTINCT ON (infra_id, obj_id) * FROM infra_layer_operational_point) AS lay ON OP.obj_id = lay.obj_id AND OP.infra_id = lay.infra_id", - column(name = "obj_id", data_type = "string"), - column(name = "infra_id", data_type = "integer"), - column(name = "name", data_type = "string"), - column(name = "uic", data_type = "integer"), - column(name = "ch", data_type = "string"), - column(name = "trigram", data_type = "string") + column( + name = "obj_id", + data_type = "varchar(255)", + sql = "infra_object_operational_point.obj_id", + ), + column( + name = "infra_id", + data_type = "integer", + sql = "infra_object_operational_point.infra_id", + ), + column( + name = "uic", + data_type = "integer", + sql = "(infra_object_operational_point.data->'extensions'->'identifier'->>'uic')::integer", + ), + column( + name = "trigram", + data_type = "varchar(3)", + sql = "infra_object_operational_point.data->'extensions'->'sncf'->>'trigram'", + ), + column( + name = "ci", + data_type = "integer", + sql = "(infra_object_operational_point.data->'extensions'->'sncf'->>'ci')::integer", + ), + column( + name = "ch", + data_type = "text", + sql = "infra_object_operational_point.data->'extensions'->'sncf'->>'ch'", + ), + column( + name = "name", + data_type = "text", + sql = "infra_object_operational_point.data->'extensions'->'identifier'->>'name'", + textual_search, + ) )] #[allow(unused)] /// A search result item for a query with `object = "operationalpoint"` @@ -64,6 +95,8 @@ pub(super) struct SearchResultItemOperationalPoint { trigram: String, #[search(sql = "OP.data#>>'{extensions,sncf,ch}'")] ch: String, + #[search(sql = "OP.data#>>'{extensions,sncf,ci}'")] + ci: u64, #[search(sql = "ST_AsGeoJSON(ST_Transform(lay.geographic, 4326))::json")] geographic: GeoJsonPoint, #[search(sql = "ST_AsGeoJSON(ST_Transform(lay.schematic, 4326))::json")] diff --git a/editoast/src/views/search/search_object.rs b/editoast/src/views/search/search_object.rs index 7dbaaf97aac..4be08384368 100644 --- a/editoast/src/views/search/search_object.rs +++ b/editoast/src/views/search/search_object.rs @@ -113,6 +113,8 @@ impl SearchConfig { }; let insert_trigger_name = format!("{table}__ins_trig"); let update_trigger_name = format!("{table}__upd_trig"); + let insert_trigger_function = format!("{table}__ins_trig_fun"); + let update_trigger_function = format!("{table}__upd_trig_fun"); let insert_trigger = format!( include_str!("sql/insert_trigger_template.sql"), trigger = insert_trigger_name, @@ -166,8 +168,8 @@ CREATE TABLE "{table}" ( DROP TABLE IF EXISTS "{table}"; DROP TRIGGER IF EXISTS {insert_trigger_name} ON "{src_table}"; DROP TRIGGER IF EXISTS {update_trigger_name} ON "{src_table}"; -DROP FUNCTION IF EXISTS {insert_trigger_name}; -DROP FUNCTION IF EXISTS {update_trigger_name}; +DROP FUNCTION IF EXISTS {insert_trigger_function}; +DROP FUNCTION IF EXISTS {update_trigger_function}; {append_down}"# ); (up, down) diff --git a/front/src/common/api/osrdEditoastApi.ts b/front/src/common/api/osrdEditoastApi.ts index 95ccb630ec2..0b91c1c4655 100644 --- a/front/src/common/api/osrdEditoastApi.ts +++ b/front/src/common/api/osrdEditoastApi.ts @@ -1960,6 +1960,7 @@ export type SearchResultItemTrack = { }; export type SearchResultItemOperationalPoint = { ch: string; + ci: number; geographic: GeoJsonPoint; infra_id: number; name: string;