Add support to detect string cartodb_id columns
This commit is contained in:
@@ -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,24 +515,41 @@ BEGIN
|
|||||||
-- 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
|
|
||||||
-- 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;
|
|
||||||
|
|
||||||
|
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
|
||||||
|
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));
|
||||||
|
|||||||
Reference in New Issue
Block a user