From 081ed36aae65e3fb78499378b28a77a9b279570a Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Mon, 2 Nov 2015 11:47:14 +0100 Subject: [PATCH] Fixes NULL max in rec 155 --- scripts-available/CDB_CartodbfyTable.sql | 4 ++-- test/CDB_CartodbfyTableTest.sql | 10 +++++++++- test/CDB_CartodbfyTableTest_expect | 3 +++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/scripts-available/CDB_CartodbfyTable.sql b/scripts-available/CDB_CartodbfyTable.sql index 172c372..44127c7 100644 --- a/scripts-available/CDB_CartodbfyTable.sql +++ b/scripts-available/CDB_CartodbfyTable.sql @@ -157,7 +157,7 @@ BEGIN 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 %', 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 280e8c5..367f31a 100644 --- a/test/CDB_CartodbfyTableTest.sql +++ b/test/CDB_CartodbfyTableTest.sql @@ -319,7 +319,7 @@ SELECT CDB_CartodbfyTableCheck('test', 'Table with non unique and null cartodb_i SELECT cartodb_id, cartodb_id_0 from test; DROP TABLE test; --- _CDB_create_cartodb_id_column with cartodb_id already present +-- _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); @@ -327,6 +327,14 @@ SELECT column_name FROM information_schema.columns WHERE table_name = 'test' AND 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; + -- 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 0e866e9..6317b33 100644 --- a/test/CDB_CartodbfyTableTest_expect +++ b/test/CDB_CartodbfyTableTest_expect @@ -104,6 +104,9 @@ Table with non unique and null cartodb_id #148 cartodbfied fine DROP TABLE CREATE TABLE +DROP TABLE +CREATE TABLE + _cartodb_id0 DROP TABLE DROP FUNCTION