From 48cce661d55c0a48059469c1cd4483dd6f95ca85 Mon Sep 17 00:00:00 2001 From: Lucas Date: Sun, 20 Oct 2024 12:00:46 +0800 Subject: [PATCH] Use comment on view to generate TileJson (#1507) Try to fix #1494 - [x] Update [query_available_tables.sql](https://github.com/maplibre/martin/pull/1507/files#diff-449496b294ee2413fc0d5e4290688a50f008e07fece5a613da1c2417548a06bf) - [x] Add and update test --- martin/src/pg/scripts/query_available_tables.sql | 3 ++- martin/tests/pg_server_test.rs | 7 ++++--- martin/tests/pg_table_source_test.rs | 7 ++++--- tests/expected/auto/catalog_auto.json | 3 ++- tests/fixtures/tables/points1.sql | 9 +++++++++ 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/martin/src/pg/scripts/query_available_tables.sql b/martin/src/pg/scripts/query_available_tables.sql index 4c4d0be5c..783a0d30e 100755 --- a/martin/src/pg/scripts/query_available_tables.sql +++ b/martin/src/pg/scripts/query_available_tables.sql @@ -55,9 +55,10 @@ WITH SELECT pg_namespace.nspname AS schema_name, relname AS table_name, - CAST(obj_description(relfilenode, 'pg_class') AS VARCHAR) AS description + pg_description.description AS description FROM pg_class JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid + LEFT JOIN pg_description ON pg_class.oid = pg_description.objoid WHERE relkind = 'r' OR relkind = 'v' ) SELECT schema, diff --git a/martin/tests/pg_server_test.rs b/martin/tests/pg_server_test.rs index da2f040f0..7fa96d2cf 100644 --- a/martin/tests/pg_server_test.rs +++ b/martin/tests/pg_server_test.rs @@ -52,7 +52,7 @@ postgres: let response = assert_response(response).await; let body = read_body(response).await; let body: serde_json::Value = serde_json::from_slice(&body).unwrap(); - assert_yaml_snapshot!(body, @r###" + assert_yaml_snapshot!(body, @r#" --- fonts: {} sprites: {} @@ -111,8 +111,9 @@ postgres: content_type: application/x-protobuf description: public.points1.geom points1_vw: + attribution: some attribution from SQL comment content_type: application/x-protobuf - description: public.points1_vw.geom + description: description from SQL comment points2: content_type: application/x-protobuf description: public.points2.geom @@ -127,7 +128,7 @@ postgres: table_source_multiple_geom.1: content_type: application/x-protobuf description: public.table_source_multiple_geom.geom2 - "###); + "#); } #[actix_rt::test] diff --git a/martin/tests/pg_table_source_test.rs b/martin/tests/pg_table_source_test.rs index ed9983ec3..9628d58eb 100644 --- a/martin/tests/pg_table_source_test.rs +++ b/martin/tests/pg_table_source_test.rs @@ -15,7 +15,7 @@ fn init() { #[actix_rt::test] async fn table_source() { let mock = mock_sources(mock_pgcfg("connection_string: $DATABASE_URL")).await; - assert_yaml_snapshot!(mock.0.tiles.get_catalog(), @r###" + assert_yaml_snapshot!(mock.0.tiles.get_catalog(), @r#" --- "-function.withweired---_-characters": content_type: application/x-protobuf @@ -72,7 +72,8 @@ async fn table_source() { description: public.points1.geom points1_vw: content_type: application/x-protobuf - description: public.points1_vw.geom + description: description from SQL comment + attribution: some attribution from SQL comment points2: content_type: application/x-protobuf description: public.points2.geom @@ -87,7 +88,7 @@ async fn table_source() { table_source_multiple_geom.1: content_type: application/x-protobuf description: public.table_source_multiple_geom.geom2 - "###); + "#); let source = table(&mock, "table_source"); assert_yaml_snapshot!(source, @r###" diff --git a/tests/expected/auto/catalog_auto.json b/tests/expected/auto/catalog_auto.json index cf0f37400..bd4b5b683 100644 --- a/tests/expected/auto/catalog_auto.json +++ b/tests/expected/auto/catalog_auto.json @@ -106,7 +106,8 @@ }, "points1_vw": { "content_type": "application/x-protobuf", - "description": "public.points1_vw.geom" + "description": "description from SQL comment", + "attribution": "some attribution from SQL comment" }, "points2": { "content_type": "application/x-protobuf", diff --git a/tests/fixtures/tables/points1.sql b/tests/fixtures/tables/points1.sql index 28bf6f7e9..02dc9e93e 100644 --- a/tests/fixtures/tables/points1.sql +++ b/tests/fixtures/tables/points1.sql @@ -53,3 +53,12 @@ values (1, '0101000020E6100000EC3A2806EDDA61401C2041E87DDA2740'), CREATE INDEX ON points1 USING GIST (geom); CLUSTER points1_geom_idx ON points1; + +DO $do$ BEGIN + EXECUTE 'COMMENT ON VIEW points1_vw IS $tj$' || $$ + { + "description": "description from SQL comment", + "attribution": "some attribution from SQL comment" + } + $$::json || '$tj$'; +END $do$;