_CDB_Conf_Cache SD-based

This commit is contained in:
Juan Ignacio Sánchez Lara 2015-08-18 10:44:10 +02:00
parent 1217b4e4a4
commit a75a337296
3 changed files with 43 additions and 11 deletions

View File

@ -19,6 +19,7 @@ CREATE OR REPLACE
FUNCTION cartodb.CDB_Conf_RemoveConf(param text)
RETURNS void AS $$
BEGIN
PERFORM cartodb._CDB_Conf_Cache('remove', param);
EXECUTE 'DELETE FROM cartodb.CDB_CONF WHERE PARAM = $1;' USING param;
END
$$ LANGUAGE PLPGSQL VOLATILE;
@ -29,7 +30,29 @@ FUNCTION cartodb.CDB_Conf_GetConf(param text)
DECLARE
conf TEXT;
BEGIN
EXECUTE 'SELECT CONF FROM cartodb.CDB_CONF WHERE PARAM = $1;' INTO conf USING param;
EXECUTE 'select cartodb._CDB_Conf_Cache(''get'', $1) as conf;' INTO conf USING param;
RETURN conf;
END
$$ LANGUAGE PLPGSQL STABLE;
-- Single cache function allowing SD private dict usage
CREATE OR REPLACE
FUNCTION cartodb._CDB_Conf_Cache(operation text, param text)
RETURNS TEXT AS
$$
if 'conf' not in SD:
SD['conf'] = dict()
if operation == 'remove':
SD['conf'][param] = None
elif operation == 'get':
if param not in SD['conf']:
value = None
response = plpy.execute("SELECT conf FROM cartodb.CDB_CONF WHERE PARAM = '%s'" % param);
if len(response) > 0:
value = response[0]['conf']
SD['conf'][param] = value
return SD['conf'][param]
else:
raise Exception('Unknown operation: %s' % operation)
$$ LANGUAGE 'plpythonu' VOLATILE;

View File

@ -10,7 +10,7 @@ $$
url = '/api/v1/databases/%s/groups' % database_name
body = '{ "name": "%s", "database_role": "%s" }' % (group_name, group_role)
query = "select cartodb._CDB_Group_API_Request('POST', '%s', '%s') as response_status" % (url, body)
plpy.execute(query)[0]['response_status']
plpy.execute(query)
$$ LANGUAGE 'plpythonu' VOLATILE;
CREATE OR REPLACE
@ -21,7 +21,7 @@ $$
url = '/api/v1/databases/%s/groups/%s' % (database_name, group_name)
query = "select cartodb._CDB_Group_API_Request('DELETE', '%s', '') as response_status" % url
plpy.execute(query)[0]['response_status']
plpy.execute(query)
$$ LANGUAGE 'plpythonu' VOLATILE;
CREATE OR REPLACE
@ -33,7 +33,7 @@ $$
url = '/api/v1/databases/%s/groups/%s' % (database_name, old_group_name)
body = '{ "name": "%s", "database_role": "%s" }' % (new_group_name, new_group_role)
query = "select cartodb._CDB_Group_API_Request('PUT', '%s', '%s') as response_status" % (url, body)
plpy.execute(query)[0]['response_status']
plpy.execute(query)
$$ LANGUAGE 'plpythonu' VOLATILE;
CREATE OR REPLACE
@ -45,7 +45,7 @@ $$
url = '/api/v1/databases/%s/groups/%s/users' % (database_name, group_name)
body = '{ "username": "%s" }' % username
query = "select cartodb._CDB_Group_API_Request('POST', '%s', '%s') as response_status" % (url, body)
plpy.execute(query)[0]['response_status']
plpy.execute(query)
$$ LANGUAGE 'plpythonu' VOLATILE;
CREATE OR REPLACE
@ -56,7 +56,7 @@ $$
url = '/api/v1/databases/%s/groups/%s/users/%s' % (database_name, group_name, username)
query = "select cartodb._CDB_Group_API_Request('DELETE', '%s', '') as response_status" % url
plpy.execute(query)[0]['response_status']
plpy.execute(query)
$$ LANGUAGE 'plpythonu' VOLATILE;
DO LANGUAGE 'plpgsql' $$
@ -129,4 +129,6 @@ $$
if last_err is not None:
plpy.error('Fatal Group API error: ' + str(last_err))
raise last_err
return None
$$ LANGUAGE 'plpythonu' VOLATILE;

View File

@ -173,8 +173,8 @@ function setup() {
sql "SELECT cartodb.CDB_Organization_AddAdmin('cdb_org_admin');"
create_role_and_schema cdb_testmember_1
create_role_and_schema cdb_testmember_2
#publicuser# sql "CREATE ROLE publicuser LOGIN;"
#publicuser# sql "GRANT CONNECT ON DATABASE \"${DATABASE}\" TO publicuser;"
sql "CREATE ROLE publicuser LOGIN;"
sql "GRANT CONNECT ON DATABASE \"${DATABASE}\" TO publicuser;"
create_table cdb_testmember_1 foo
sql cdb_testmember_1 'INSERT INTO cdb_testmember_1.foo VALUES (1), (2), (3), (4), (5);'
@ -217,12 +217,12 @@ function tear_down() {
sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM cdb_testmember_1;"
sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM cdb_testmember_2;"
#publicuser# sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM publicuser;"
sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM publicuser;"
sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM cdb_org_admin;"
sql 'DROP ROLE cdb_testmember_1;'
sql 'DROP ROLE cdb_testmember_2;'
#publicuser# sql 'DROP ROLE publicuser;'
sql 'DROP ROLE publicuser;'
sql 'DROP ROLE cdb_org_admin;'
${CMD} -c "DROP DATABASE ${DATABASE}"
@ -397,7 +397,7 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset
sql postgres "select * from CDB_QueryTables('select * from cdb_testmember_1.foo, cdb_testmember_2.bar, plainto_tsquery(''foo'')');" should "{cdb_testmember_1.foo,cdb_testmember_2.bar}"
}
function xtest_cdb_usertables_should_work_with_orgusers() {
function test_cdb_usertables_should_work_with_orgusers() {
# This test validates the changes proposed in https://github.com/CartoDB/cartodb/pull/5021
@ -529,10 +529,17 @@ function test_administrator_name_generation() {
function test_conf() {
sql postgres "SELECT cartodb.CDB_Conf_GetConf('test_conf')" should ''
sql postgres "SELECT cartodb.CDB_Conf_GetConf('test_conf_2')" should ''
sql postgres "SELECT cartodb.CDB_Conf_SetConf('test_conf', 'test_val')"
sql postgres "SELECT cartodb.CDB_Conf_GetConf('test_conf')" should 'test_val'
sql postgres "SELECT cartodb.CDB_Conf_GetConf('test_conf_2')" should ''
sql postgres "SELECT cartodb.CDB_Conf_RemoveConf('test_conf')"
sql postgres "SELECT cartodb.CDB_Conf_GetConf('test_conf')" should ''
sql postgres "SELECT cartodb.CDB_Conf_GetConf('test_conf_2')" should ''
}
#################################################### TESTS END HERE ####################################################