diff --git a/expected/test_ddl_triggers.out b/expected/test_ddl_triggers.out index 53fb677..14e4b04 100644 --- a/expected/test_ddl_triggers.out +++ b/expected/test_ddl_triggers.out @@ -67,6 +67,8 @@ NOTICE: trigger "update_updated_at_trigger" for table "c.t4" does not exist, sk NOTICE: trigger "test_quota" for table "c.t4" does not exist, skipping NOTICE: trigger "test_quota_per_row" for table "c.t4" does not exist, skipping NOTICE: Column cartodb_id already exists +NOTICE: Existing cartodb_id field does not have an associated sequence, renaming +NOTICE: Trying to recover data from _cartodb_id0 coumn NOTICE: event trigger "cdb_on_relation_create" does not exist, skipping NOTICE: event trigger "cdb_on_relation_drop" does not exist, skipping NOTICE: event trigger "cdb_on_alter_column" does not exist, skipping diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index 8c02e1c..72cd4ad 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -89,13 +89,21 @@ BEGIN IF had_column THEN + SELECT pg_catalog.pg_get_serial_sequence(reloid::text, 'cartodb_id') + AS seq INTO rec2; + -- Check data type is an integer - SELECT t.typname, t.oid, a.attnotnull FROM pg_type t, pg_attribute a + SELECT + pg_catalog.pg_get_serial_sequence(reloid::text, 'cartodb_id') as seq, + t.typname, t.oid, a.attnotnull FROM pg_type t, pg_attribute a WHERE a.atttypid = t.oid AND a.attrelid = reloid AND NOT a.attisdropped AND a.attname = 'cartodb_id' INTO STRICT rec; - IF rec.oid NOT IN (20,21,23) THEN -- int2, int4, int8 { + -- 20=int2, 21=int4, 23=int8 + IF rec.oid NOT IN (20,21,23) THEN RAISE NOTICE 'Existing cartodb_id field is of invalid type % (need int2, int4 or int8), renaming', rec.typname; + ELSIF rec.seq IS NULL THEN + RAISE NOTICE 'Existing cartodb_id field does not have an associated sequence, renaming'; ELSE -- }{ sql := 'ALTER TABLE ' || reloid::text || ' ALTER COLUMN cartodb_id SET NOT NULL'; IF NOT EXISTS ( SELECT c.conname FROM pg_constraint c, pg_attribute a diff --git a/test/CDB_CartodbfyTableTest.sql b/test/CDB_CartodbfyTableTest.sql index a467903..8102fc2 100644 --- a/test/CDB_CartodbfyTableTest.sql +++ b/test/CDB_CartodbfyTableTest.sql @@ -182,18 +182,24 @@ SELECT CDB_CartodbfyTableCheck('t', 'cartodbfied with view'); DROP VIEW v; DROP TABLE t; --- table with existing cartodb_id field ot type text +-- table with existing cartodb_id field of type text CREATE TABLE t AS SELECT 10::text as cartodb_id; SELECT CDB_CartodbfyTableCheck('t', 'text cartodb_id'); select cartodb_id/2 FROM t; DROP TABLE t; --- table with existing cartodb_id field ot type text not casting +-- table with existing cartodb_id field of type text not casting CREATE TABLE t AS SELECT 'nan' as cartodb_id; SELECT CDB_CartodbfyTableCheck('t', 'uncasting text cartodb_id'); select cartodb_id,_cartodb_id0 FROM t; DROP TABLE t; +-- table with existing cartodb_id field of type int4 not sequenced +CREATE TABLE t AS SELECT 1::int4 as cartodb_id; +SELECT CDB_CartodbfyTableCheck('t', 'unsequenced cartodb_id'); +select cartodb_id FROM t; +DROP TABLE t; + -- TODO: table with existing custom-triggered the_geom DROP FUNCTION CDB_CartodbfyTableCheck(regclass, text); diff --git a/test/CDB_CartodbfyTableTest_expect b/test/CDB_CartodbfyTableTest_expect index bee3b23..4a3d2b0 100644 --- a/test/CDB_CartodbfyTableTest_expect +++ b/test/CDB_CartodbfyTableTest_expect @@ -40,5 +40,9 @@ SELECT 1 uncasting text cartodb_id cartodbfied fine 1|nan DROP TABLE +SELECT 1 +unsequenced cartodb_id cartodbfied fine +1 +DROP TABLE DROP FUNCTION DROP FUNCTION