From 3d274c4dd1489f9409dfa5d693dd0f8898323b9c Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 10 Nov 2015 12:50:31 +0100 Subject: [PATCH 1/2] Remove schema_triggers as a dependency (it is an indirect dependency, maybe to remove in the future) --- server/extension/cdb_geocoder_server.control | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/extension/cdb_geocoder_server.control b/server/extension/cdb_geocoder_server.control index 310c9a5..7ac080a 100644 --- a/server/extension/cdb_geocoder_server.control +++ b/server/extension/cdb_geocoder_server.control @@ -1,6 +1,6 @@ # cdb geocoder server extension comment = 'CartoDB server geocoder extension' default_version = '0.0.1' -requires = 'plpythonu, schema_triggers, postgis, cdb_geocoder' +requires = 'plpythonu, postgis, cdb_geocoder' superuser = true schema = cdb_geocoder_server From 7f26134632186ae9610569ac4537b37805cd0f70 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 10 Nov 2015 15:28:12 +0100 Subject: [PATCH 2/2] My try at creating the client extension (to be tested) --- client/.gitignore | 1 + client/Makefile | 26 ++++++++++++++ client/cdb_geocoder_client.control | 6 ++++ client/expected/00_installation_test.out | 5 +++ client/sql/0.0.1/00_header.sql | 2 ++ client/sql/0.0.1/05_geocoder_server_conf.sql | 38 ++++++++++++++++++++ client/sql/0.0.1/10_admin0.sql | 29 +++++++++++++++ client/sql/00_installation_test.sql | 6 ++++ client/sql/10_admin0_test.sql | 0 9 files changed, 113 insertions(+) create mode 100644 client/expected/00_installation_test.out create mode 100644 client/sql/0.0.1/00_header.sql create mode 100644 client/sql/0.0.1/05_geocoder_server_conf.sql create mode 100644 client/sql/0.0.1/10_admin0.sql create mode 100644 client/sql/00_installation_test.sql create mode 100644 client/sql/10_admin0_test.sql diff --git a/client/.gitignore b/client/.gitignore index e710f0e..1ff37f5 100644 --- a/client/.gitignore +++ b/client/.gitignore @@ -1,3 +1,4 @@ results/ regression.diffs regression.out +cdb_geocoder_client--0.0.1.sql diff --git a/client/Makefile b/client/Makefile index e69de29..652539f 100644 --- a/client/Makefile +++ b/client/Makefile @@ -0,0 +1,26 @@ +# Makefile to generate the extension out of separate sql source files. +# Once a version is released, it is not meant to be changed. E.g: once version 0.0.1 is out, it SHALL NOT be changed. +EXTENSION = cdb_geocoder_client +EXTVERSION = $(shell grep default_version $(EXTENSION).control | sed -e "s/default_version[[:space:]]*=[[:space:]]*'\([^']*\)'/\1/") + +DATA = $(EXTENSION)--$(EXTVERSION).sql + +REGRESS = $(notdir $(basename $(wildcard sql/*test.sql))) + +# postgres build stuff +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) + + +SOURCES_DATA = $(wildcard sql/$(EXTVERSION)/*.sql) + +$(DATA): $(SOURCES_DATA) + rm -f $@ + cat $(SOURCES_DATA) >> $@ + +all: $(DATA) + +# Only meant for development time, do not use once a version is released +devclean: + rm -f $(DATA) diff --git a/client/cdb_geocoder_client.control b/client/cdb_geocoder_client.control index e69de29..b439b22 100644 --- a/client/cdb_geocoder_client.control +++ b/client/cdb_geocoder_client.control @@ -0,0 +1,6 @@ +# CartoDB geocoder client API extension +comment = 'CartoDB geocoder client API extension' +default_version = '0.0.1' +requires = 'plproxy' +superuser = true +schema = cdb_geocoder_client diff --git a/client/expected/00_installation_test.out b/client/expected/00_installation_test.out new file mode 100644 index 0000000..4b07dfe --- /dev/null +++ b/client/expected/00_installation_test.out @@ -0,0 +1,5 @@ +-- Install dependencies +CREATE EXTENSION postgis; +CREATE EXTENSION plproxy; +-- Install the extension +CREATE EXTENSION cdb_geocoder_client; diff --git a/client/sql/0.0.1/00_header.sql b/client/sql/0.0.1/00_header.sql new file mode 100644 index 0000000..a140c33 --- /dev/null +++ b/client/sql/0.0.1/00_header.sql @@ -0,0 +1,2 @@ +-- Complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION cdb_geocoder_client" to load this file. \quit diff --git a/client/sql/0.0.1/05_geocoder_server_conf.sql b/client/sql/0.0.1/05_geocoder_server_conf.sql new file mode 100644 index 0000000..f491e2e --- /dev/null +++ b/client/sql/0.0.1/05_geocoder_server_conf.sql @@ -0,0 +1,38 @@ +-- +-- This extension has its own table for configurations. +-- +-- The table and the function are considered to be private and therefore +-- no permissions are granted for any other user but the creator. + +CREATE TABLE IF NOT EXISTS _config ( KEY TEXT PRIMARY KEY, VALUE JSON NOT NULL ); + +-- Needed to dump config in backups +-- This can only be called from an SQL script executed by CREATE EXTENSION +SELECT pg_catalog.pg_extension_config_dump('_config', ''); + + +CREATE OR REPLACE FUNCTION _config_set(key text, value JSON) +RETURNS VOID AS $$ +BEGIN + PERFORM _config_remove(key); + EXECUTE 'INSERT INTO _config (KEY, VALUE) VALUES ($1, $2);' USING key, value; +END +$$ LANGUAGE PLPGSQL VOLATILE; + + +CREATE OR REPLACE FUNCTION _config_remove(key text) +RETURNS VOID AS $$ +BEGIN + EXECUTE 'DELETE FROM _config WHERE KEY = $1;' USING key; +END +$$ LANGUAGE PLPGSQL VOLATILE; + +CREATE OR REPLACE FUNCTION _config_get(key text) + RETURNS JSON AS $$ +DECLARE + value JSON; +BEGIN + EXECUTE 'SELECT VALUE FROM _config WHERE KEY = $1;' INTO value USING key; + RETURN value; +END +$$ LANGUAGE PLPGSQL STABLE; diff --git a/client/sql/0.0.1/10_admin0.sql b/client/sql/0.0.1/10_admin0.sql new file mode 100644 index 0000000..6b7c157 --- /dev/null +++ b/client/sql/0.0.1/10_admin0.sql @@ -0,0 +1,29 @@ +-- +-- Public geocoder API function +-- +-- These are the only ones with permissions to publicuser role +-- and should also be the only ones with SECURITY DEFINER + +CREATE OR REPLACE FUNCTION geocode_admin0_polygons(country_name text) +RETURNS Geometry AS $$ +DECLARE + db_connection_str text; + ret Geometry; +BEGIN + SELECT _config_get('db_connection_str') INTO db_connection_str; + SELECT _geocode_admin0_polygons(session_user, txid_current(), db_connection_str, country_name) INTO ret; + RETURN ret; +END; +$$ LANGUAGE 'plpgsql' SECURITY DEFINER; + + +-- TODO: review all permissions stuff [I'd explicitly grant permissions to the public functions] + +-------------------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION _geocode_admin0_polygons(user_id name, tx_id bigint, db_connection_str text, country_name text) +RETURNS Geometry AS $$ + -- TODO check if we can move the config to its own function + CONNECT db_connection_str; + SELECT geocode_admin0(user_id, tx_id, country_name); +$$ LANGUAGE plproxy; diff --git a/client/sql/00_installation_test.sql b/client/sql/00_installation_test.sql new file mode 100644 index 0000000..5f2080e --- /dev/null +++ b/client/sql/00_installation_test.sql @@ -0,0 +1,6 @@ +-- Install dependencies +CREATE EXTENSION postgis; +CREATE EXTENSION plproxy; + +-- Install the extension +CREATE EXTENSION cdb_geocoder_client; diff --git a/client/sql/10_admin0_test.sql b/client/sql/10_admin0_test.sql new file mode 100644 index 0000000..e69de29