From 90d0b23441219130d72c1d2a9237a45a87f470a3 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 15:43:02 +0200 Subject: [PATCH 1/3] Use CDB_QueryTablesText instead of CDB_QueryTables This avoids trouble with len(schema.table_name) > 63 See https://github.com/CartoDB/cartodb-postgresql/issues/86 --- lib/cartodb/api/query_tables_api.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/cartodb/api/query_tables_api.js b/lib/cartodb/api/query_tables_api.js index fc0e2932..829d82cd 100644 --- a/lib/cartodb/api/query_tables_api.js +++ b/lib/cartodb/api/query_tables_api.js @@ -14,7 +14,7 @@ module.exports = QueryTablesApi; QueryTablesApi.prototype.getAffectedTablesInQuery = function (username, sql, callback) { - var query = 'SELECT CDB_QueryTables($windshaft$' + prepareSql(sql) + '$windshaft$)'; + var query = 'SELECT CDB_QueryTablesText($windshaft$' + prepareSql(sql) + '$windshaft$)'; this.pgQueryRunner.run(username, query, handleAffectedTablesInQueryRows, callback); }; @@ -25,9 +25,9 @@ function handleAffectedTablesInQueryRows(err, rows, callback) { callback(new Error('could not fetch source tables: ' + msg)); return; } - var qtables = rows[0].cdb_querytables; - var tableNames = qtables.split(/^\{(.*)\}$/)[1]; - tableNames = tableNames ? tableNames.split(',') : []; + + // This is an Array, so no need to split into parts + var tableNames = rows[0].cdb_querytablestext; callback(null, tableNames); } @@ -35,7 +35,7 @@ QueryTablesApi.prototype.getAffectedTablesAndLastUpdatedTime = function (usernam var query = [ 'WITH querytables AS (', - 'SELECT * FROM CDB_QueryTables($windshaft$' + prepareSql(sql) + '$windshaft$) as tablenames', + 'SELECT * FROM CDB_QueryTablesText($windshaft$' + prepareSql(sql) + '$windshaft$) as tablenames', ')', 'SELECT (SELECT tablenames FROM querytables), EXTRACT(EPOCH FROM max(updated_at)) as max', 'FROM CDB_TableMetadata m', @@ -54,8 +54,8 @@ function handleAffectedTablesAndLastUpdatedTimeRows(err, rows, callback) { var result = rows[0]; - var tableNames = result.tablenames.split(/^\{(.*)\}$/)[1]; - tableNames = tableNames ? tableNames.split(',') : []; + // This is an Array, so no need to split into parts + var tableNames = result.tablenames; var lastUpdatedTime = result.max || 0; From 2715f47a224cf21b94867f5fba1abd9e2f40c63b Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 24 Jun 2015 19:07:41 +0200 Subject: [PATCH 2/3] Points CDB_QueryTables script to the branch with CDB_QueryTablesText --- test/support/prepare_db.sh | 2 +- test/support/sql/CDB_QueryTables.sql | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/test/support/prepare_db.sh b/test/support/prepare_db.sh index 8185da4d..3b827f40 100755 --- a/test/support/prepare_db.sh +++ b/test/support/prepare_db.sh @@ -80,7 +80,7 @@ if test x"$PREPARE_PGSQL" = xyes; then psql -c "CREATE EXTENSION plpythonu;" ${TEST_DB} curl -L -s https://github.com/CartoDB/cartodb-postgresql/raw/cdb/scripts-available/CDB_QueryStatements.sql -o sql/CDB_QueryStatements.sql - curl -L -s https://github.com/CartoDB/cartodb-postgresql/raw/cdb/scripts-available/CDB_QueryTables.sql -o sql/CDB_QueryTables.sql + curl -L -s https://github.com/CartoDB/cartodb-postgresql/raw/86-CDB_QueryTables-fix-long-names/scripts-available/CDB_QueryTables.sql -o sql/CDB_QueryTables.sql cat sql/CDB_QueryStatements.sql sql/CDB_QueryTables.sql | psql -v ON_ERROR_STOP=1 ${TEST_DB} || exit 1 diff --git a/test/support/sql/CDB_QueryTables.sql b/test/support/sql/CDB_QueryTables.sql index cd8b51b4..ac61281d 100644 --- a/test/support/sql/CDB_QueryTables.sql +++ b/test/support/sql/CDB_QueryTables.sql @@ -2,12 +2,12 @@ -- -- Requires PostgreSQL 9.x+ -- -CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) -RETURNS name[] +CREATE OR REPLACE FUNCTION CDB_QueryTablesText(query text) +RETURNS text[] AS $$ DECLARE exp XML; - tables NAME[]; + tables text[]; rec RECORD; rec2 RECORD; BEGIN @@ -41,11 +41,11 @@ BEGIN xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x, xpath('//x:Relation-Name/../x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s ) - SELECT unnest(x)::name as p, unnest(s)::name as sc from inp + SELECT unnest(x) as p, unnest(s) as sc from inp LOOP -- RAISE DEBUG 'tab: %', rec2.p; -- RAISE DEBUG 'sc: %', rec2.sc; - tables := array_append(tables, (rec2.sc || '.' || rec2.p)::name); + tables := array_append(tables, (rec2.sc || '.' || rec2.p)); END LOOP; -- RAISE DEBUG 'Tables: %', tables; @@ -65,3 +65,14 @@ BEGIN return tables; END $$ LANGUAGE 'plpgsql' VOLATILE STRICT; + + +-- Keep CDB_QueryTables with same signature for backwards compatibility. +-- It should probably be removed in the future. +CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) +RETURNS name[] +AS $$ +BEGIN + RETURN CDB_QueryTablesText(query)::name[]; +END +$$ LANGUAGE 'plpgsql' VOLATILE STRICT; From 9e30f05e7d0133de3fa7aa349915d8a8f1587c2d Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 29 Jun 2015 16:46:07 +0200 Subject: [PATCH 3/3] Reverts to use cdb branch as is already published --- test/support/prepare_db.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/support/prepare_db.sh b/test/support/prepare_db.sh index 3b827f40..8185da4d 100755 --- a/test/support/prepare_db.sh +++ b/test/support/prepare_db.sh @@ -80,7 +80,7 @@ if test x"$PREPARE_PGSQL" = xyes; then psql -c "CREATE EXTENSION plpythonu;" ${TEST_DB} curl -L -s https://github.com/CartoDB/cartodb-postgresql/raw/cdb/scripts-available/CDB_QueryStatements.sql -o sql/CDB_QueryStatements.sql - curl -L -s https://github.com/CartoDB/cartodb-postgresql/raw/86-CDB_QueryTables-fix-long-names/scripts-available/CDB_QueryTables.sql -o sql/CDB_QueryTables.sql + curl -L -s https://github.com/CartoDB/cartodb-postgresql/raw/cdb/scripts-available/CDB_QueryTables.sql -o sql/CDB_QueryTables.sql cat sql/CDB_QueryStatements.sql sql/CDB_QueryTables.sql | psql -v ON_ERROR_STOP=1 ${TEST_DB} || exit 1