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
+41 -24
View File
@@ -499,40 +499,57 @@ 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
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('found good ''%s''', const.pkey); RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('found good ''%s''', const.pkey);
RETURN true; RETURN true;
-- Check and see if the column values are unique and not null, -- Check and see if the column values are unique and not null,
-- if they are, we can use this column... -- if they are, we can use this column...
ELSE ELSE
-- Assume things are OK until proven otherwise... -- Assume things are OK until proven otherwise...
useable_key := true; useable_key := true;
BEGIN -- If the column is varchar, try to cast it to integer
sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_pk PRIMARY KEY (%s)', reloid::text, const.pkey, const.pkey); IF rec.atttypid IN (1043) THEN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', sql; RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): Found text column %', rec.atttypid;
EXECUTE sql;
EXCEPTION BEGIN
-- Failed unique check... sql := Format('ALTER TABLE %s ALTER cartodb_id TYPE int USING %I::integer', reloid::text, rec.attname);
WHEN unique_violation THEN RAISE DEBUG 'Running %', sql;
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s is not unique', const.pkey); EXECUTE sql;
useable_key := false; EXCEPTION
-- Failed not null check... WHEN others THEN
WHEN not_null_violation THEN RAISE DEBUG 'Column % of type text is not a valid integer column', rec.attname;
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s contains nulls', const.pkey); useable_key := false;
useable_key := false; END;
-- Other fatal error
WHEN others THEN END IF;
PERFORM _CDB_Error(sql, '_CDB_Has_Usable_Primary_ID');
END; IF useable_key THEN
BEGIN
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;
EXECUTE sql;
EXCEPTION
-- Failed unique check...
WHEN unique_violation THEN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s is not unique', const.pkey);
useable_key := false;
-- Failed not null check...
WHEN not_null_violation THEN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s contains nulls', const.pkey);
useable_key := false;
-- Other fatal error
WHEN others THEN
PERFORM _CDB_Error(sql, '_CDB_Has_Usable_Primary_ID');
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));