Add support to detect string cartodb_id columns

This commit is contained in:
Carla Iriberri
2016-03-01 15:24:40 +01:00
parent 40a163f885
commit 32307ceef0
+20 -3
View File
@@ -500,8 +500,8 @@ BEGIN
-- Found something named right... -- Found something named right...
IF FOUND THEN IF FOUND THEN
-- And it's an integer column... -- And it's an integer or varchar column...
IF rec.atttypid IN (20,21,23) THEN IF rec.atttypid IN (20,21,23,1043) THEN
-- And it's a unique primary key! Done! -- And it's a unique primary key! Done!
IF (rec.indisprimary OR rec.indisunique) AND rec.attnotnull THEN IF (rec.indisprimary OR rec.indisunique) AND rec.attnotnull THEN
@@ -515,6 +515,23 @@ BEGIN
-- Assume things are OK until proven otherwise... -- Assume things are OK until proven otherwise...
useable_key := true; useable_key := true;
-- If the column is varchar, try to cast it to integer
IF rec.atttypid IN (1043) THEN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): Found text column %', rec.atttypid;
BEGIN
sql := Format('ALTER TABLE %s ALTER cartodb_id TYPE int USING %I::integer', reloid::text, rec.attname);
RAISE DEBUG 'Running %', sql;
EXECUTE sql;
EXCEPTION
WHEN others THEN
RAISE DEBUG 'Column % of type text is not a valid integer column', rec.attname;
useable_key := false;
END;
END IF;
IF useable_key THEN
BEGIN BEGIN
sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_pk PRIMARY KEY (%s)', reloid::text, const.pkey, const.pkey); sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_pk PRIMARY KEY (%s)', reloid::text, const.pkey, const.pkey);
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', sql; RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', sql;
@@ -532,7 +549,7 @@ BEGIN
WHEN others THEN WHEN others THEN
PERFORM _CDB_Error(sql, '_CDB_Has_Usable_Primary_ID'); PERFORM _CDB_Error(sql, '_CDB_Has_Usable_Primary_ID');
END; END;
END IF;
-- Clean up test constraint -- Clean up test constraint
IF useable_key THEN 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_pk', reloid::text, const.pkey));