Merge pull request #25 from CartoDB/add-config-table-and-funcs
Config table and functions for server extension
This commit is contained in:
commit
05d75c82ee
21
server/extension/expected/10_config_test.out
Normal file
21
server/extension/expected/10_config_test.out
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
-- Create a conf table
|
||||||
|
SELECT cdb_geocoder_server._config_set('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
|
||||||
|
_config_set
|
||||||
|
-------------
|
||||||
|
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
-- Test key retrieval
|
||||||
|
SELECT cdb_geocoder_server._config_get('cds');
|
||||||
|
_config_get
|
||||||
|
----------------------------------------------------------------------------
|
||||||
|
{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}
|
||||||
|
(1 row)
|
||||||
|
|
||||||
|
-- Test returns NULL if key doesn't exist
|
||||||
|
SELECT cdb_geocoder_server._config_get('no existe');
|
||||||
|
_config_get
|
||||||
|
-------------
|
||||||
|
|
||||||
|
(1 row)
|
||||||
|
|
@ -1,15 +0,0 @@
|
|||||||
-- Create a conf table
|
|
||||||
CREATE TABLE conf (key TEXT PRIMARY KEY, values_json TEXT NOT NULL);
|
|
||||||
INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
|
|
||||||
-- Test key retrieval
|
|
||||||
SELECT cdb_geocoder_server._get_conf('cds');
|
|
||||||
_get_conf
|
|
||||||
----------------------------------------------------------------------------
|
|
||||||
{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}
|
|
||||||
(1 row)
|
|
||||||
|
|
||||||
-- Test no key exception
|
|
||||||
SELECT cdb_geocoder_server._get_conf('no existe');
|
|
||||||
ERROR: Missing key 'no existe' in conf table
|
|
||||||
-- Drop conf table
|
|
||||||
DROP TABLE conf;
|
|
38
server/extension/sql/0.0.1/10_config.sql
Normal file
38
server/extension/sql/0.0.1/10_config.sql
Normal file
@ -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/geocoder.
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS cdb_geocoder_server._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('cdb_geocoder_server._config', '');
|
||||||
|
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._config_set(key text, value JSON)
|
||||||
|
RETURNS VOID AS $$
|
||||||
|
BEGIN
|
||||||
|
PERFORM cdb_geocoder_server._config_remove(key);
|
||||||
|
EXECUTE 'INSERT INTO cdb_geocoder_server._config (KEY, VALUE) VALUES ($1, $2);' USING key, value;
|
||||||
|
END
|
||||||
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||||
|
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._config_remove(key text)
|
||||||
|
RETURNS VOID AS $$
|
||||||
|
BEGIN
|
||||||
|
EXECUTE 'DELETE FROM cdb_geocoder_server._config WHERE KEY = $1;' USING key;
|
||||||
|
END
|
||||||
|
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._config_get(key text)
|
||||||
|
RETURNS JSON AS $$
|
||||||
|
DECLARE
|
||||||
|
value JSON;
|
||||||
|
BEGIN
|
||||||
|
EXECUTE 'SELECT VALUE FROM cdb_geocoder_server._config WHERE KEY = $1;' INTO value USING key;
|
||||||
|
RETURN value;
|
||||||
|
END
|
||||||
|
$$ LANGUAGE PLPGSQL STABLE;
|
@ -1,16 +0,0 @@
|
|||||||
-- Get values_json for provided key from conf table
|
|
||||||
CREATE OR REPLACE FUNCTION cdb_geocoder_server._get_conf(_key TEXT)
|
|
||||||
RETURNS text
|
|
||||||
AS $$
|
|
||||||
DECLARE
|
|
||||||
rec RECORD;
|
|
||||||
BEGIN
|
|
||||||
SELECT INTO rec values_json FROM conf WHERE conf.key = _key;
|
|
||||||
|
|
||||||
IF NOT FOUND THEN
|
|
||||||
RAISE EXCEPTION 'Missing key ''%'' in conf table', _key;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
RETURN rec.values_json;
|
|
||||||
END
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
8
server/extension/sql/10_config_test.sql
Normal file
8
server/extension/sql/10_config_test.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
-- Create a conf table
|
||||||
|
SELECT cdb_geocoder_server._config_set('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
|
||||||
|
|
||||||
|
-- Test key retrieval
|
||||||
|
SELECT cdb_geocoder_server._config_get('cds');
|
||||||
|
|
||||||
|
-- Test returns NULL if key doesn't exist
|
||||||
|
SELECT cdb_geocoder_server._config_get('no existe');
|
@ -1,12 +0,0 @@
|
|||||||
-- Create a conf table
|
|
||||||
CREATE TABLE conf (key TEXT PRIMARY KEY, values_json TEXT NOT NULL);
|
|
||||||
INSERT INTO conf VALUES ('cds', '{"Manolo Escobar": {"El Limonero":"En stock", "Viva el vino":"Sin stock"}}');
|
|
||||||
|
|
||||||
-- Test key retrieval
|
|
||||||
SELECT cdb_geocoder_server._get_conf('cds');
|
|
||||||
|
|
||||||
-- Test no key exception
|
|
||||||
SELECT cdb_geocoder_server._get_conf('no existe');
|
|
||||||
|
|
||||||
-- Drop conf table
|
|
||||||
DROP TABLE conf;
|
|
Loading…
Reference in New Issue
Block a user