diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index 849238e..4d44c22 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -152,12 +152,12 @@ BEGIN -- Copy existing values to new field -- NOTE: using ALTER is a workaround to a PostgreSQL bug and is also known to be faster for tables with many rows -- See http://www.postgresql.org/message-id/20140530143150.GA11051@localhost - sql := Format('ALTER TABLE %s ALTER cartodb_id TYPE int USING %I', reloid::text, new_name); + sql := Format('ALTER TABLE %s ALTER cartodb_id TYPE int USING %I::integer', reloid::text, new_name); RAISE DEBUG 'Running %', sql; EXECUTE sql; -- Find max value - sql := Format('SELECT max(cartodb_id) FROM %s', reloid::text); + sql := Format('SELECT coalesce(max(cartodb_id), 0) as max FROM %s', reloid::text); RAISE DEBUG 'Running %', sql; EXECUTE sql INTO rec; @@ -166,7 +166,7 @@ BEGIN AS seq INTO rec2; -- Reset sequence name - sql := Format('ALTER SEQUENCE %s RESTART WITH %d', rec2.seq::text, rec.max + 1); + sql := Format('ALTER SEQUENCE %s RESTART WITH %s', rec2.seq::text, rec.max + 1); RAISE DEBUG 'Running %', sql; EXECUTE sql; diff --git a/test/CDB_CartodbfyTableTest.sql b/test/CDB_CartodbfyTableTest.sql index fc6bd8a..76e62b0 100644 --- a/test/CDB_CartodbfyTableTest.sql +++ b/test/CDB_CartodbfyTableTest.sql @@ -319,6 +319,23 @@ SELECT CDB_CartodbfyTableCheck('test', 'Table with non unique and null cartodb_i SELECT cartodb_id, cartodb_id_0 from test; DROP TABLE test; +SET client_min_messages TO notice; +-- _CDB_create_cartodb_id_column with cartodb_id integer already present +CREATE TABLE test (cartodb_id integer); + +SELECT _CDB_Create_Cartodb_ID_Column('test'::regclass); +SELECT column_name FROM information_schema.columns WHERE table_name = 'test' AND column_name = '_cartodb_id0'; + +DROP TABLE test; + +-- _CDB_create_cartodb_id_column with cartodb_id text already present +CREATE TABLE test (cartodb_id text); + +SELECT _CDB_Create_Cartodb_ID_Column('test'::regclass); +SELECT column_name FROM information_schema.columns WHERE table_name = 'test' AND column_name = '_cartodb_id0'; + +DROP TABLE test; +SET client_min_messages TO error; -- TODO: table with existing custom-triggered the_geom diff --git a/test/CDB_CartodbfyTableTest_expect b/test/CDB_CartodbfyTableTest_expect index 6103dc8..e63dbd3 100644 --- a/test/CDB_CartodbfyTableTest_expect +++ b/test/CDB_CartodbfyTableTest_expect @@ -102,5 +102,19 @@ Table with non unique and null cartodb_id #148 cartodbfied fine 3| 4|2 DROP TABLE +SET +CREATE TABLE +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 column + +DROP TABLE +CREATE TABLE +NOTICE: Column cartodb_id already exists +NOTICE: Existing cartodb_id field is of invalid type text (need int2, int4 or int8), renaming +NOTICE: Trying to recover data from _cartodb_id0 column + +DROP TABLE +SET DROP FUNCTION DROP FUNCTION