Third iteration, expect a viable cartodb_id

This commit is contained in:
Carla Iriberri 2016-03-02 14:58:53 +01:00
parent 0d5f83b3c4
commit a18cbeb2cd

View File

@ -466,33 +466,20 @@ END;
$$ LANGUAGE 'plpgsql';
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = '_cdb_has_usable_primary_id_record') THEN
CREATE TYPE _cdb_has_usable_primary_id_record
AS (has_usable_primary_key boolean,
text_key_column boolean);
END IF;
END$$;
-- Find out if the table already has a usable primary key
-- If the table has both a usable key and usable geometry
-- we can no-op on the table copy and just ensure that the
-- indexes and triggers are in place
CREATE OR REPLACE FUNCTION cartodb._CDB_Has_Usable_Primary_ID(reloid REGCLASS)
RETURNS _cdb_has_usable_primary_id_record
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Primary_ID(reloid REGCLASS)
RETURNS BOOLEAN
AS $$
DECLARE
rec RECORD;
const RECORD;
idc RECORD;
i INTEGER;
sql TEXT;
useable_key BOOLEAN = false;
has_usable_primary_key BOOLEAN = false;
-- In case cartodb_id is a text column
text_key_column BOOLEAN = false;
BEGIN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', 'entered function';
@ -514,14 +501,10 @@ BEGIN
-- Found something named right...
IF FOUND THEN
-- And it's an integer or varchar column...
IF rec.atttypid IN (20,21,23,1043) THEN
-- And it's a unique primary key! Done!
IF (rec.indisprimary OR rec.indisunique) AND rec.attnotnull THEN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('found good ''%s''', const.pkey);
SELECT true as has_usable_primary_key, text_key_column INTO idc;
RETURN idc;
RETURN true;
-- Check and see if the column values are unique and not null,
-- if they are, we can use this column...
@ -529,28 +512,6 @@ BEGIN
-- Assume things are OK until proven otherwise...
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('SELECT %I::integer FROM %s', rec.attname, reloid::text);
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): Running %', sql;
EXECUTE sql;
text_key_column := true
EXCEPTION
WHEN invalid_text_representation THEN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): Column % of type text is not a valid integer column', rec.attname;
useable_key := false;
WHEN others THEN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): Exception %', SQLSTATE;
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;
@ -568,7 +529,7 @@ BEGIN
WHEN others THEN
PERFORM _CDB_Error(sql, '_CDB_Has_Usable_Primary_ID');
END;
END IF;
-- Clean up test constraint
IF useable_key THEN
PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_pk', reloid::text, const.pkey));
@ -587,28 +548,14 @@ BEGIN
END IF;
SELECT useable_key as has_usable_primary_key, text_key_column INTO idc;
RETURN idc;
END IF;
-- It's not an integer column, we have to rename it
ELSE
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
Format('found non-integer ''%s'', renaming it', const.pkey);
PERFORM _CDB_SQL(
Format('ALTER TABLE %s RENAME COLUMN %s TO %I',
reloid::text, rec.attname, cartodb._CDB_Unique_Column_Identifier(NULL, const.pkey, NULL, reloid)),
'_CDB_Has_Usable_Primary_ID');
RETURN useable_key;
END IF;
-- There's no column there named pkey
ELSE
-- Is there another suitable primary key already?
-- Is there another integer suitable primary key already?
SELECT a.attname
INTO rec
FROM pg_class c
@ -621,8 +568,7 @@ BEGIN
-- Yes! Ok, rename it.
IF FOUND THEN
PERFORM _CDB_SQL(Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, rec.attname, const.pkey),'_CDB_Has_Usable_Primary_ID');
SELECT true as has_usable_primary_key, text_key_column INTO idc;
RETURN idc;
RETURN true;
ELSE
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
Format('found no useful column for ''%s''', const.pkey);
@ -633,8 +579,7 @@ BEGIN
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', 'function complete';
-- Didn't find re-usable key, so return FALSE
SELECT false as has_usable_primary_key, text_key_column INTO idc;
RETURN idc;
RETURN false;
END;
$$ LANGUAGE 'plpgsql';
@ -837,7 +782,7 @@ $$ LANGUAGE 'plpgsql';
-- a "good" one, and the same for the geometry columns. If all the required
-- columns are in place already, it no-ops and just renames the table to
-- the destination if necessary.
CREATE OR REPLACE FUNCTION cartodb._CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT DEFAULT NULL)
CREATE OR REPLACE FUNCTION _CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT DEFAULT NULL)
RETURNS BOOLEAN
AS $$
DECLARE
@ -858,13 +803,13 @@ DECLARE
rec RECORD;
const RECORD;
idc RECORD;
gc RECORD;
sql TEXT;
str TEXT;
table_srid INTEGER;
geom_srid INTEGER;
has_usable_primary_key BOOLEAN;
has_usable_pk_sequence BOOLEAN;
BEGIN
@ -888,16 +833,15 @@ BEGIN
-- See if there is a primary key column we need to carry along to the
-- new table. If this is true, it implies there is an indexed
-- primary key of integer type named (by default) cartodb_id
SELECT has_usable_primary_key, text_key_column
FROM _CDB_Has_Usable_Primary_ID(reloid)
INTO STRICT idc;
SELECT _CDB_Has_Usable_Primary_ID(reloid)
INTO STRICT has_usable_primary_key;
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): has_usable_primary_key %', idc.has_usable_primary_key;
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): has_usable_primary_key %', has_usable_primary_key;
-- See if the candidate primary key column has a sequence for default
-- values. No usable pk implies has_usable_pk_sequence = false.
has_usable_pk_sequence := false;
IF idc.has_usable_primary_key THEN
IF has_usable_primary_key THEN
SELECT _CDB_Has_Usable_PK_Sequence(reloid)
INTO STRICT has_usable_pk_sequence;
END IF;
@ -932,16 +876,9 @@ BEGIN
-- We can only avoid a rewrite if both the key and
-- geometry are usable
-- If cartodb_id column is of type text, cast it
IF idc.text_key_column THEN
sql := Format('ALTER TABLE %s ALTER cartodb_id TYPE int USING %I::integer', reloid::text, 'cartodb_id');
PERFORM _CDB_SQL(sql,'_CDB_Rewrite_Table');
END IF;
RAISE DEBUG 'idc %', idc;
-- No table re-write is required, BUT a rename is required to
-- a destination schema, so do that now
IF idc.has_usable_primary_key AND has_usable_pk_sequence AND gc.has_usable_geoms THEN
IF has_usable_primary_key AND has_usable_pk_sequence AND gc.has_usable_geoms THEN
IF destschema != relschema THEN
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): perfect table needs to be moved to schema (%)', destschema;
@ -979,8 +916,8 @@ BEGIN
sql := Format('CREATE TABLE %s AS SELECT ', copyname);
-- Add cartodb ID!
IF idc.has_usable_primary_key THEN
sql := sql || const.pkey;
IF has_usable_primary_key THEN
sql := sql || const.pkey || '::bigint ';
ELSE
sql := sql || 'nextval(''' || destseq || ''') AS ' || const.pkey;
END IF;