From 777e8c6e4cc648f2d66db2d4e2c1a7ba4d6551ea Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 29 Sep 2015 18:03:20 +0200 Subject: [PATCH 1/3] Adds test to check cartodbfy works with null cartodb_id --- test/CDB_CartodbfyTableTest.sql | 40 +++++++++++++++++++++++++++++- test/CDB_CartodbfyTableTest_expect | 23 +++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/test/CDB_CartodbfyTableTest.sql b/test/CDB_CartodbfyTableTest.sql index b35540b..42a23cf 100644 --- a/test/CDB_CartodbfyTableTest.sql +++ b/test/CDB_CartodbfyTableTest.sql @@ -250,7 +250,7 @@ INSERT INTO existing_cartodb_id (cartodb_id, description) VALUES (20, 'b'), (30, 'c'); SELECT CDB_CartodbfyTableCheck('existing_cartodb_id', 'Existing cartodb_id values are respected #138'); -SELECT * from existing_cartodb_id; +SELECT cartodb_id,the_geom,the_geom_webmercator,description,name from existing_cartodb_id; DROP TABLE existing_cartodb_id; -- Table with both the_geom and wkb_geometry @@ -281,6 +281,44 @@ INSERT INTO many_colliding_columns VALUES ( SELECT CDB_CartodbfyTableCheck('many_colliding_columns', 'Many colliding columns #141'); DROP TABLE many_colliding_columns; +-- table with null cartodb_id +CREATE TABLE test ( + cartodb_id integer +); +INSERT INTO test VALUES + (1), + (2), + (NULL), + (3); +SELECT CDB_CartodbfyTable('test'); +SELECT cartodb_id, cartodb_id_1 from test; +DROP TABLE test; + +-- table with non unique cartodb_id +CREATE TABLE test ( + cartodb_id integer +); +INSERT INTO test VALUES + (1), + (2), + (2); +SELECT CDB_CartodbfyTable('test'); +SELECT cartodb_id, cartodb_id_1 from test; +DROP TABLE test; + +-- table with non unique and null cartodb_id +CREATE TABLE test ( + cartodb_id integer +); +INSERT INTO test VALUES + (1), + (2), + (NULL), + (2); +SELECT CDB_CartodbfyTable('test'); +SELECT cartodb_id, cartodb_id_1 from test; +DROP TABLE test; + -- TODO: table with existing custom-triggered the_geom diff --git a/test/CDB_CartodbfyTableTest_expect b/test/CDB_CartodbfyTableTest_expect index 6287c3f..ab6c8e9 100644 --- a/test/CDB_CartodbfyTableTest_expect +++ b/test/CDB_CartodbfyTableTest_expect @@ -79,5 +79,28 @@ CREATE TABLE INSERT 0 1 Many colliding columns #141 cartodbfied fine DROP TABLE +CREATE TABLE +INSERT 0 4 +test +1|1 +2|2 +3| +4|3 +DROP TABLE +CREATE TABLE +INSERT 0 3 +test +1|1 +2|2 +3|2 +DROP TABLE +CREATE TABLE +INSERT 0 4 +test +1|1 +2|2 +3| +4|2 +DROP TABLE DROP FUNCTION DROP FUNCTION From 3429e93979dedc20ab1570c7beae519eeb74cfb4 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 29 Sep 2015 18:04:00 +0200 Subject: [PATCH 2/3] Makes cartodbfy check for null cartodb_id --- scripts-available/CDB_CartodbfyTable.sql | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index 2e44e9a..84a6d51 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -362,7 +362,7 @@ $$ LANGUAGE PLPGSQL; -- As before, this drops all the metadata and geom sync triggers -- -- (2) _CDB_Has_Usable_Primary_ID() --- Returns TRUE if it can find a unique integer primary key named +-- Returns TRUE if it can find a unique and not null integer primary key named -- 'cartodb_id' or can rename an existing key. -- Returns FALSE otherwise. -- @@ -551,7 +551,7 @@ BEGIN RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('found good ''%s''', const.pkey); RETURN true; - -- Check and see if the column values are unique, + -- Check and see if the column values are unique and not null, -- if they are, we can use this column... ELSE @@ -559,13 +559,17 @@ BEGIN useable_key := true; BEGIN - sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_unique UNIQUE (%s)', reloid::text, const.pkey, const.pkey); + sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_pk PRIMARY KEY (%s)', reloid::text, const.pkey, const.pkey); RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', sql; EXECUTE sql; EXCEPTION -- Failed unique check... WHEN unique_violation THEN - RAISE NOTICE 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s is not unique', const.pkey); + RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s is not unique', const.pkey); + useable_key := false; + -- Failed not null check... + WHEN not_null_violation THEN + RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s contains nulls', const.pkey); useable_key := false; -- Other fatal error WHEN others THEN @@ -574,7 +578,7 @@ BEGIN -- Clean up test constraint IF useable_key THEN - PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_unique', reloid::text, const.pkey)); + PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_pk', reloid::text, const.pkey)); -- Move non-unique column out of the way ELSE From 5de395a4a8639c6e3a3e3929ef2c5b78db5eb1bd Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Tue, 29 Sep 2015 19:01:30 +0200 Subject: [PATCH 3/3] Uses CDB_CartodbfyTableCheck() 148 --- test/CDB_CartodbfyTableTest.sql | 12 ++++++------ test/CDB_CartodbfyTableTest_expect | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/test/CDB_CartodbfyTableTest.sql b/test/CDB_CartodbfyTableTest.sql index 42a23cf..dc4bab7 100644 --- a/test/CDB_CartodbfyTableTest.sql +++ b/test/CDB_CartodbfyTableTest.sql @@ -281,7 +281,7 @@ INSERT INTO many_colliding_columns VALUES ( SELECT CDB_CartodbfyTableCheck('many_colliding_columns', 'Many colliding columns #141'); DROP TABLE many_colliding_columns; --- table with null cartodb_id +-- Table with null cartodb_id CREATE TABLE test ( cartodb_id integer ); @@ -290,11 +290,11 @@ INSERT INTO test VALUES (2), (NULL), (3); -SELECT CDB_CartodbfyTable('test'); +SELECT CDB_CartodbfyTableCheck('test', 'Table with null cartodb_id #148'); SELECT cartodb_id, cartodb_id_1 from test; DROP TABLE test; --- table with non unique cartodb_id +-- Table with non unique cartodb_id CREATE TABLE test ( cartodb_id integer ); @@ -302,11 +302,11 @@ INSERT INTO test VALUES (1), (2), (2); -SELECT CDB_CartodbfyTable('test'); +SELECT CDB_CartodbfyTableCheck('test', 'Table with non unique cartodb_id #148'); SELECT cartodb_id, cartodb_id_1 from test; DROP TABLE test; --- table with non unique and null cartodb_id +-- Table with non unique and null cartodb_id CREATE TABLE test ( cartodb_id integer ); @@ -315,7 +315,7 @@ INSERT INTO test VALUES (2), (NULL), (2); -SELECT CDB_CartodbfyTable('test'); +SELECT CDB_CartodbfyTableCheck('test', 'Table with non unique and null cartodb_id #148'); SELECT cartodb_id, cartodb_id_1 from test; DROP TABLE test; diff --git a/test/CDB_CartodbfyTableTest_expect b/test/CDB_CartodbfyTableTest_expect index ab6c8e9..6103dc8 100644 --- a/test/CDB_CartodbfyTableTest_expect +++ b/test/CDB_CartodbfyTableTest_expect @@ -81,7 +81,7 @@ Many colliding columns #141 cartodbfied fine DROP TABLE CREATE TABLE INSERT 0 4 -test +Table with null cartodb_id #148 cartodbfied fine 1|1 2|2 3| @@ -89,14 +89,14 @@ test DROP TABLE CREATE TABLE INSERT 0 3 -test +Table with non unique cartodb_id #148 cartodbfied fine 1|1 2|2 3|2 DROP TABLE CREATE TABLE INSERT 0 4 -test +Table with non unique and null cartodb_id #148 cartodbfied fine 1|1 2|2 3|