From bc0966c46e813931cae243fefe1b3ff662ef6d94 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 11 Jun 2014 19:21:19 +0200 Subject: [PATCH] Do not add unique index on cartodb_id if already a primary key Closes #38 --- NEWS | 4 ++++ scripts-available/CDB_CartodbfyTable.sql | 2 +- test/CDB_CartodbfyTableTest.sql | 8 ++++++++ test/CDB_CartodbfyTableTest_expect | 4 ++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 497d9b1..960e027 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,10 @@ Enhancements: - Drop useless DEFAULT specification in plpgsql variable declarations - List plpythonu requirement first, so get pg_catalog scanned before public +Bug fixes: + + - Do not add unique index on cartodb_id if already a primary key (#38) + 0.2.0 - 2014-06-09 ------------------ diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index e69f187..ad56404 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -111,7 +111,7 @@ BEGIN AND a.attrelid = reloid AND NOT a.attisdropped AND a.attname = 'cartodb_id' - AND c.contype = 'u' ) -- unique + AND c.contype IN ( 'u', 'p' ) ) -- unique or pkey THEN sql := sql || ', ADD unique(cartodb_id)'; END IF; diff --git a/test/CDB_CartodbfyTableTest.sql b/test/CDB_CartodbfyTableTest.sql index e68cb8b..74ece18 100644 --- a/test/CDB_CartodbfyTableTest.sql +++ b/test/CDB_CartodbfyTableTest.sql @@ -200,6 +200,14 @@ SELECT CDB_CartodbfyTableCheck('t', 'unsequenced cartodb_id'); select cartodb_id FROM t; DROP TABLE t; +-- table with existing cartodb_id serial primary key +CREATE TABLE t ( cartodb_id serial primary key ); +SELECT CDB_CartodbfyTableCheck('t', 'cartodb_id serial primary key'); +SELECT c.conname, a.attname FROM pg_constraint c, pg_attribute a +WHERE c.conrelid = 't'::regclass and a.attrelid = c.conrelid +AND c.conkey[1] = a.attnum AND NOT a.attisdropped; +DROP TABLE t; + -- table with existing the_geom and created_at and containing null values -- Really, a test for surviving an longstanding PostgreSQL bug: -- http://www.postgresql.org/message-id/20140530143150.GA11051@localhost diff --git a/test/CDB_CartodbfyTableTest_expect b/test/CDB_CartodbfyTableTest_expect index c3dd05c..c5e79e6 100644 --- a/test/CDB_CartodbfyTableTest_expect +++ b/test/CDB_CartodbfyTableTest_expect @@ -45,6 +45,10 @@ unsequenced cartodb_id cartodbfied fine 1 DROP TABLE CREATE TABLE +cartodb_id serial primary key cartodbfied fine +t_pkey|cartodb_id +DROP TABLE +CREATE TABLE null geom and timestamp values cartodbfied fine DROP TABLE DROP FUNCTION