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 diff --git a/test/CDB_CartodbfyTableTest.sql b/test/CDB_CartodbfyTableTest.sql index b35540b..dc4bab7 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_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 +CREATE TABLE test ( + cartodb_id integer +); +INSERT INTO test VALUES + (1), + (2), + (2); +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 +CREATE TABLE test ( + cartodb_id integer +); +INSERT INTO test VALUES + (1), + (2), + (NULL), + (2); +SELECT CDB_CartodbfyTableCheck('test', 'Table with non unique and null cartodb_id #148'); +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..6103dc8 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 +Table with null cartodb_id #148 cartodbfied fine +1|1 +2|2 +3| +4|3 +DROP TABLE +CREATE TABLE +INSERT 0 3 +Table with non unique cartodb_id #148 cartodbfied fine +1|1 +2|2 +3|2 +DROP TABLE +CREATE TABLE +INSERT 0 4 +Table with non unique and null cartodb_id #148 cartodbfied fine +1|1 +2|2 +3| +4|2 +DROP TABLE DROP FUNCTION DROP FUNCTION