From 7b48c2765e19340852907a164e1f1735ae55381e Mon Sep 17 00:00:00 2001 From: Carla Iriberri Date: Tue, 8 Mar 2016 14:06:58 +0100 Subject: [PATCH] Fix error detection and fix tests --- scripts-available/CDB_CartodbfyTable.sql | 16 +++++++++++----- test/CDB_CartodbfyTableTest_expect | 16 ++++++++-------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index a1130bd..149d7c0 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -515,6 +515,7 @@ BEGIN useable_key := true; BEGIN sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_pk PRIMARY KEY (%s)', reloid::text, const.pkey, const.pkey); + sql := sql || ', ' || Format('ADD CONSTRAINT %s_integer CHECK (%s::integer >=0);', const.pkey, const.pkey); RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', sql; EXECUTE sql; EXCEPTION @@ -526,22 +527,27 @@ BEGIN WHEN not_null_violation THEN RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s contains nulls', const.pkey); useable_key := false; + -- Failed integer check... + WHEN invalid_text_representation THEN + RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('invalid input syntax for integer %s', const.pkey); + useable_key := false; -- Other fatal error WHEN others THEN - PERFORM _CDB_Error(sql, Format('_CDB_Has_Usable_Primary_ID: not valid %s', SQLERRM)); + PERFORM _CDB_Error(sql, Format('_CDB_Has_Usable_Primary_ID: %s', SQLERRM)); END; -- Clean up test constraint IF useable_key THEN PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_pk', reloid::text, const.pkey)); + PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_integer', reloid::text, const.pkey)); - -- Move non-unique column out of the way + -- Move non-valid column out of the way ELSE RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', - Format('found non-unique ''%s''', const.pkey); + Format('found non-valid ''%s''', const.pkey); - PERFORM _CDB_Error(sql, Format('_CDB_Has_Usable_Primary_ID: non-unique %s', const.pkey)); + PERFORM _CDB_Error(sql, Format('_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, %s', const.pkey)); END IF; @@ -1076,7 +1082,7 @@ BEGIN -- Run it! PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table'); - + -- Set up the primary key sequence -- If we copied the primary key from the original data, we need -- to set the sequence to the maximum value of that key diff --git a/test/CDB_CartodbfyTableTest_expect b/test/CDB_CartodbfyTableTest_expect index 89eb1b9..de2e9ae 100644 --- a/test/CDB_CartodbfyTableTest_expect +++ b/test/CDB_CartodbfyTableTest_expect @@ -7,9 +7,9 @@ single non-geometrical column cartodbfied fine DROP TABLE SELECT 1 check function idempotence cartodbfied fine -1|0101000020E6100000000000000000F0BF000000000000F0BF|0101000020110F0000DB0B4ADA772DFBC046432E49D22DFBC0|this is a sentence +1|0101000020E6100000000000000000F0BF000000000000F0BF|0101000020110F0000DB0B4ADA772DFBC077432E49D22DFBC0|this is a sentence check function idempotence cartodbfied fine -1|0101000020E6100000000000000000F0BF000000000000F0BF|0101000020110F0000DB0B4ADA772DFBC046432E49D22DFBC0|this is a sentence +1|0101000020E6100000000000000000F0BF000000000000F0BF|0101000020110F0000DB0B4ADA772DFBC077432E49D22DFBC0|this is a sentence DROP TABLE SELECT 1 srid-unconstrained the_geom cartodbfied fine @@ -36,10 +36,10 @@ text cartodb_id cartodbfied fine 5 DROP TABLE SELECT 1 -ERROR: CDB(_CDB_Rewrite_Table:22P02:invalid input syntax for integer: "nan"): CREATE TABLE public.t_0 AS SELECT cartodb_id::bigint ,NULL::geometry(Geometry,4326) AS the_geom,NULL::geometry(Geometry,3857) AS the_geom_webmercator FROM t +ERROR: CDB(_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, cartodb_id): ALTER TABLE t ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id), ADD CONSTRAINT cartodb_id_integer CHECK (cartodb_id::integer >=0); DROP TABLE SELECT 1 -ERROR: CDB(_CDB_Has_Usable_Primary_ID: non-unique cartodb_id): ALTER TABLE t ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id) +ERROR: CDB(_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, cartodb_id): ALTER TABLE t ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id), ADD CONSTRAINT cartodb_id_integer CHECK (cartodb_id::integer >=0); DROP TABLE SELECT 1 @@ -92,11 +92,11 @@ Table with both the_geom and wkb_geometry #141 cartodbfied fine DROP TABLE CREATE TABLE INSERT 0 1 -ERROR: CDB(_CDB_Has_Usable_Primary_ID: not valid multiple primary keys for table "many_colliding_columns" are not allowed): ALTER TABLE many_colliding_columns ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id) +ERROR: CDB(_CDB_Has_Usable_Primary_ID: multiple primary keys for table "many_colliding_columns" are not allowed): ALTER TABLE many_colliding_columns ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id), ADD CONSTRAINT cartodb_id_integer CHECK (cartodb_id::integer >=0); DROP TABLE CREATE TABLE INSERT 0 4 -ERROR: CDB(_CDB_Has_Usable_Primary_ID: non-unique cartodb_id): ALTER TABLE test ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id) +ERROR: CDB(_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, cartodb_id): ALTER TABLE test ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id), ADD CONSTRAINT cartodb_id_integer CHECK (cartodb_id::integer >=0); 1 2 @@ -104,14 +104,14 @@ ERROR: CDB(_CDB_Has_Usable_Primary_ID: non-unique cartodb_id): ALTER TABLE test DROP TABLE CREATE TABLE INSERT 0 3 -ERROR: CDB(_CDB_Has_Usable_Primary_ID: non-unique cartodb_id): ALTER TABLE test ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id) +ERROR: CDB(_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, cartodb_id): ALTER TABLE test ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id), ADD CONSTRAINT cartodb_id_integer CHECK (cartodb_id::integer >=0); 1 2 2 DROP TABLE CREATE TABLE INSERT 0 4 -ERROR: CDB(_CDB_Has_Usable_Primary_ID: non-unique cartodb_id): ALTER TABLE test ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id) +ERROR: CDB(_CDB_Has_Usable_Primary_ID: Error: invalid cartodb_id, cartodb_id): ALTER TABLE test ADD CONSTRAINT cartodb_id_pk PRIMARY KEY (cartodb_id), ADD CONSTRAINT cartodb_id_integer CHECK (cartodb_id::integer >=0); 1 2