|
|
@ -692,6 +692,45 @@ $$ LANGUAGE PLPGSQL;
|
|
|
|
-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
|
|
-- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Columns(OUT pkey TEXT, OUT geomcol TEXT, OUT mercgeomcol TEXT)
|
|
|
|
|
|
|
|
RETURNS record
|
|
|
|
|
|
|
|
AS $$
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pkey := 'cartodb_id';
|
|
|
|
|
|
|
|
geomcol := 'the_geom';
|
|
|
|
|
|
|
|
mercgeomcol := 'the_geom_webmercator';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Error(message TEXT, funcname TEXT DEFAULT '_CDB_Error')
|
|
|
|
|
|
|
|
RETURNS void
|
|
|
|
|
|
|
|
AS $$
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'CDB(%): %', funcname, message;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_SQL(sql TEXT, funcname TEXT DEFAULT '_CDB_SQL')
|
|
|
|
|
|
|
|
RETURNS void
|
|
|
|
|
|
|
|
AS $$
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'CDB(%): %', funcname, sql;
|
|
|
|
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EXCEPTION
|
|
|
|
|
|
|
|
WHEN others THEN
|
|
|
|
|
|
|
|
RAISE EXCEPTION 'CDB(%:%:%): %', funcname, SQLSTATE, SQLERRM, sql;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
END;
|
|
|
|
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Find a unique relation name in the given schema, starting from the
|
|
|
|
-- Find a unique relation name in the given schema, starting from the
|
|
|
|
-- template given. If the template is already unique, just return it;
|
|
|
|
-- template given. If the template is already unique, just return it;
|
|
|
@ -724,7 +763,7 @@ BEGIN
|
|
|
|
newrelname := relationname || '_' || i;
|
|
|
|
newrelname := relationname || '_' || i;
|
|
|
|
|
|
|
|
|
|
|
|
IF i > 100 THEN
|
|
|
|
IF i > 100 THEN
|
|
|
|
RAISE EXCEPTION '_CDB_Unique_Relation_Name looping too far';
|
|
|
|
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Relation_Name');
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
END LOOP;
|
|
|
|
END LOOP;
|
|
|
@ -766,7 +805,7 @@ BEGIN
|
|
|
|
newcolname := columnname || '_' || i;
|
|
|
|
newcolname := columnname || '_' || i;
|
|
|
|
|
|
|
|
|
|
|
|
IF i > 100 THEN
|
|
|
|
IF i > 100 THEN
|
|
|
|
RAISE EXCEPTION '_CDB_Unique_Column_Name looping too far';
|
|
|
|
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Column_Name');
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
END LOOP;
|
|
|
|
END LOOP;
|
|
|
@ -775,7 +814,8 @@ END;
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Return the geometry SRID of the very first entry in a given column.
|
|
|
|
-- Return the geometry SRID from the column metadata or
|
|
|
|
|
|
|
|
-- the geometry of the very first entry in a given column.
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Geometry_SRID(reloid REGCLASS, columnname TEXT)
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Geometry_SRID(reloid REGCLASS, columnname TEXT)
|
|
|
|
RETURNS INTEGER
|
|
|
|
RETURNS INTEGER
|
|
|
|
AS $$
|
|
|
|
AS $$
|
|
|
@ -783,16 +823,16 @@ DECLARE
|
|
|
|
rec RECORD;
|
|
|
|
rec RECORD;
|
|
|
|
BEGIN
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Geometry_SRID, entered';
|
|
|
|
RAISE DEBUG 'CDB(%): %', '_CDB_Geometry_SRID', 'entered function';
|
|
|
|
|
|
|
|
|
|
|
|
EXECUTE Format('SELECT ST_SRID(%s) AS srid FROM %s LIMIT 1', columnname, reloid::text)
|
|
|
|
EXECUTE Format('SELECT ST_SRID(%s) AS srid FROM %s LIMIT 1', columnname, reloid::text)
|
|
|
|
INTO rec;
|
|
|
|
INTO rec;
|
|
|
|
|
|
|
|
|
|
|
|
IF FOUND THEN
|
|
|
|
IF FOUND THEN
|
|
|
|
RETURN rec.srid;
|
|
|
|
RETURN rec.srid;
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
RETURN 0;
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RETURN 0;
|
|
|
|
|
|
|
|
|
|
|
|
END;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
@ -802,17 +842,21 @@ $$ LANGUAGE 'plpgsql';
|
|
|
|
-- If the table has both a usable key and usable geometry
|
|
|
|
-- If the table has both a usable key and usable geometry
|
|
|
|
-- we can no-op on the table copy and just ensure that the
|
|
|
|
-- we can no-op on the table copy and just ensure that the
|
|
|
|
-- indexes and triggers are in place
|
|
|
|
-- indexes and triggers are in place
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Primary_ID(reloid REGCLASS, keyname TEXT)
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Primary_ID(reloid REGCLASS)
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
AS $$
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
DECLARE
|
|
|
|
rec RECORD;
|
|
|
|
rec RECORD;
|
|
|
|
|
|
|
|
const RECORD;
|
|
|
|
i INTEGER;
|
|
|
|
i INTEGER;
|
|
|
|
sql TEXT;
|
|
|
|
sql TEXT;
|
|
|
|
useable_key BOOLEAN = false;
|
|
|
|
useable_key BOOLEAN = false;
|
|
|
|
BEGIN
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'Entered _CDB_Has_Usable_Primary_ID';
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', 'entered function';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Read in the names of the CartoDB columns
|
|
|
|
|
|
|
|
const := _CDB_Columns();
|
|
|
|
|
|
|
|
|
|
|
|
-- Do we already have a properly named column?
|
|
|
|
-- Do we already have a properly named column?
|
|
|
|
SELECT a.attname, i.indisprimary, i.indisunique, a.attnotnull, a.atttypid
|
|
|
|
SELECT a.attname, i.indisprimary, i.indisunique, a.attnotnull, a.atttypid
|
|
|
@ -823,7 +867,7 @@ BEGIN
|
|
|
|
LEFT JOIN pg_index i ON c.oid = i.indrelid AND a.attnum = ANY(i.indkey)
|
|
|
|
LEFT JOIN pg_index i ON c.oid = i.indrelid AND a.attnum = ANY(i.indkey)
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
AND NOT a.attisdropped
|
|
|
|
AND NOT a.attisdropped
|
|
|
|
AND a.attname = keyname;
|
|
|
|
AND a.attname = const.pkey;
|
|
|
|
|
|
|
|
|
|
|
|
-- Found something named right...
|
|
|
|
-- Found something named right...
|
|
|
|
IF FOUND THEN
|
|
|
|
IF FOUND THEN
|
|
|
@ -833,7 +877,7 @@ BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
-- And it's a unique primary key! Done!
|
|
|
|
-- And it's a unique primary key! Done!
|
|
|
|
IF rec.indisprimary AND rec.indisunique AND rec.attnotnull THEN
|
|
|
|
IF rec.indisprimary AND rec.indisunique AND rec.attnotnull THEN
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID found good ''%''', keyname;
|
|
|
|
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,
|
|
|
|
-- Check and see if the column values are unique,
|
|
|
@ -844,48 +888,55 @@ BEGIN
|
|
|
|
useable_key := true;
|
|
|
|
useable_key := true;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
BEGIN
|
|
|
|
sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_unique UNIQUE (%s)', reloid::text, keyname, keyname);
|
|
|
|
sql := Format('ALTER TABLE %s ADD CONSTRAINT %s_unique UNIQUE (%s)', reloid::text, const.pkey, const.pkey);
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID: %', sql;
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', sql;
|
|
|
|
EXECUTE sql;
|
|
|
|
EXECUTE sql;
|
|
|
|
EXCEPTION
|
|
|
|
EXCEPTION
|
|
|
|
-- Failed unique check...
|
|
|
|
-- Failed unique check...
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
WHEN unique_violation THEN
|
|
|
|
RAISE NOTICE '_CDB_Has_Usable_Primary_ID column % is not unique', keyname;
|
|
|
|
RAISE NOTICE 'CDB(_CDB_Has_Usable_Primary_ID): %', Format('column %s is not unique', const.pkey);
|
|
|
|
useable_key := false;
|
|
|
|
useable_key := false;
|
|
|
|
-- Other fatal error
|
|
|
|
-- Other fatal error
|
|
|
|
WHEN others THEN
|
|
|
|
WHEN others THEN
|
|
|
|
RAISE EXCEPTION 'Cartodbfying % (%s): % (%)', reloid::text, keyname, SQLERRM, SQLSTATE;
|
|
|
|
PERFORM _CDB_Error(sql, '_CDB_Has_Usable_Primary_ID');
|
|
|
|
END;
|
|
|
|
END;
|
|
|
|
|
|
|
|
|
|
|
|
-- Clean up test constraint
|
|
|
|
-- Clean up test constraint
|
|
|
|
IF useable_key THEN
|
|
|
|
IF useable_key THEN
|
|
|
|
EXECUTE Format('ALTER TABLE %s DROP CONSTRAINT %s_unique', reloid::text, keyname);
|
|
|
|
PERFORM _CDB_SQL(Format('ALTER TABLE %s DROP CONSTRAINT %s_unique', reloid::text, const.pkey));
|
|
|
|
|
|
|
|
|
|
|
|
-- Move non-unique column out of the way
|
|
|
|
-- Move non-unique column out of the way
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID found non-unique ''%'', renaming it', keyname;
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
|
|
|
|
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %s',
|
|
|
|
Format('found non-unique ''%s'', renaming it', const.pkey);
|
|
|
|
reloid::text, rec.attname, _CDB_Unique_Column_Name(reloid, keyname));
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID: %', sql;
|
|
|
|
PERFORM _CDB_SQL(
|
|
|
|
EXECUTE sql;
|
|
|
|
Format('ALTER TABLE %s RENAME COLUMN %s TO %s',
|
|
|
|
|
|
|
|
reloid::text, rec.attname,
|
|
|
|
|
|
|
|
_CDB_Unique_Column_Name(reloid, const.pkey)),
|
|
|
|
|
|
|
|
'_CDB_Has_Usable_Primary_ID');
|
|
|
|
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return useable_key;
|
|
|
|
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- It's not an integer column, we have to rename it
|
|
|
|
-- It's not an integer column, we have to rename it
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID found non-integer ''%'', renaming it', keyname;
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
|
|
|
|
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %s',
|
|
|
|
Format('found non-integer ''%s'', renaming it', const.pkey);
|
|
|
|
reloid::text, rec.attname, _CDB_Unique_Column_Name(reloid, keyname));
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID: %', sql;
|
|
|
|
PERFORM _CDB_SQL(
|
|
|
|
EXECUTE sql;
|
|
|
|
Format('ALTER TABLE %s RENAME COLUMN %s TO %s',
|
|
|
|
|
|
|
|
reloid::text, rec.attname, _CDB_Unique_Column_Name(reloid, const.pkey)),
|
|
|
|
|
|
|
|
'_CDB_Has_Usable_Primary_ID');
|
|
|
|
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- There's no column there named keyname
|
|
|
|
-- There's no column there named pkey
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
|
|
|
|
-- Is there another suitable primary key already?
|
|
|
|
-- Is there another suitable primary key already?
|
|
|
@ -900,37 +951,43 @@ BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
-- Yes! Ok, rename it.
|
|
|
|
-- Yes! Ok, rename it.
|
|
|
|
IF FOUND THEN
|
|
|
|
IF FOUND THEN
|
|
|
|
EXECUTE Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, rec.attname, keyname);
|
|
|
|
PERFORM _CDB_SQL(Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, rec.attname, const.pkey),'_CDB_Has_Usable_Primary_ID');
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID found acceptable primary key ''%s'', renaming to ''%''', rec.attname, keyname;
|
|
|
|
|
|
|
|
RETURN true;
|
|
|
|
RETURN true;
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID found no useful column for ''%''', keyname;
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %',
|
|
|
|
|
|
|
|
Format('found no useful column for ''%s''', const.pkey);
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Primary_ID completed';
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Primary_ID): %', 'function complete';
|
|
|
|
|
|
|
|
|
|
|
|
-- Didn't fine re-usable key, so return FALSE
|
|
|
|
-- Didn't find re-usable key, so return FALSE
|
|
|
|
RETURN false;
|
|
|
|
RETURN false;
|
|
|
|
|
|
|
|
|
|
|
|
END;
|
|
|
|
END;
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
$$ LANGUAGE 'plpgsql';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Geom(reloid REGCLASS, geom_name TEXT, mercgeom_name TEXT)
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Has_Usable_Geom(reloid REGCLASS)
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
AS $$
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
DECLARE
|
|
|
|
r1 RECORD;
|
|
|
|
r1 RECORD;
|
|
|
|
r2 RECORD;
|
|
|
|
r2 RECORD;
|
|
|
|
found_geom BOOLEAN := false;
|
|
|
|
const RECORD;
|
|
|
|
has_geom BOOLEAN := false;
|
|
|
|
has_geom BOOLEAN := false;
|
|
|
|
has_mercgeom BOOLEAN := false;
|
|
|
|
has_mercgeom BOOLEAN := false;
|
|
|
|
|
|
|
|
has_geom_name TEXT;
|
|
|
|
|
|
|
|
has_mercgeom_name TEXT;
|
|
|
|
str TEXT;
|
|
|
|
str TEXT;
|
|
|
|
|
|
|
|
sql TEXT;
|
|
|
|
BEGIN
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'Entered _CDB_Has_Usable_Geom';
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', 'entered function';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Read in the names of the CartoDB columns
|
|
|
|
|
|
|
|
const := _CDB_Columns();
|
|
|
|
|
|
|
|
|
|
|
|
-- Do we have a column we can use?
|
|
|
|
-- Do we have a column we can use?
|
|
|
|
FOR r1 IN
|
|
|
|
FOR r1 IN
|
|
|
@ -942,7 +999,7 @@ BEGIN
|
|
|
|
FROM pg_class c
|
|
|
|
FROM pg_class c
|
|
|
|
JOIN pg_attribute a ON a.attrelid = c.oid
|
|
|
|
JOIN pg_attribute a ON a.attrelid = c.oid
|
|
|
|
JOIN pg_type t ON a.atttypid = t.oid,
|
|
|
|
JOIN pg_type t ON a.atttypid = t.oid,
|
|
|
|
(VALUES (geom_name, 4326), (mercgeom_name, 3857) ) as f(desired_attname, desired_srid)
|
|
|
|
(VALUES (const.geomcol, 4326), (const.mercgeomcol, 3857) ) as f(desired_attname, desired_srid)
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
AND a.attnum > 0
|
|
|
|
AND a.attnum > 0
|
|
|
|
AND NOT a.attisdropped
|
|
|
|
AND NOT a.attisdropped
|
|
|
@ -950,14 +1007,15 @@ BEGIN
|
|
|
|
ORDER BY t.oid ASC
|
|
|
|
ORDER BY t.oid ASC
|
|
|
|
LOOP
|
|
|
|
LOOP
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Geom, checking ''%''', r1.attname;
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', Format('checking column ''%s''', r1.attname);
|
|
|
|
found_geom := false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Name collision: right name but wrong type, rename it!
|
|
|
|
-- Name collision: right name but wrong type, rename it!
|
|
|
|
IF r1.typname != 'geometry' AND r1.attname = r1.desired_attname THEN
|
|
|
|
IF r1.typname != 'geometry' AND r1.attname = r1.desired_attname THEN
|
|
|
|
str := _CDB_Unique_Column_Name(reloid, r1.attname);
|
|
|
|
str := _CDB_Unique_Column_Name(reloid, r1.attname);
|
|
|
|
EXECUTE Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, r1.attname, str);
|
|
|
|
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, r1.attname, str);
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Geom: % is the wrong type, renamed to %', r1.attname, str;
|
|
|
|
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
|
|
|
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
|
|
|
|
|
|
|
|
Format('%s is the wrong type, renamed to %s', r1.attname, str);
|
|
|
|
|
|
|
|
|
|
|
|
-- Found a geometry column!
|
|
|
|
-- Found a geometry column!
|
|
|
|
ELSIF r1.typname = 'geometry' THEN
|
|
|
|
ELSIF r1.typname = 'geometry' THEN
|
|
|
@ -965,18 +1023,15 @@ BEGIN
|
|
|
|
-- If it's the right SRID, we can use it in place without
|
|
|
|
-- If it's the right SRID, we can use it in place without
|
|
|
|
-- transforming it!
|
|
|
|
-- transforming it!
|
|
|
|
IF r1.srid = r1.desired_srid OR _CDB_Geometry_SRID(reloid, r1.attname) = r1.desired_srid THEN
|
|
|
|
IF r1.srid = r1.desired_srid OR _CDB_Geometry_SRID(reloid, r1.attname) = r1.desired_srid THEN
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Geom found acceptable ''%''', r1.attname;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- If it's the wrong name, just rename it.
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %', Format('found acceptable ''%s''', r1.attname);
|
|
|
|
IF r1.attname != r1.desired_attname THEN
|
|
|
|
|
|
|
|
EXECUTE Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, r1.attname, r1.desired_attname);
|
|
|
|
|
|
|
|
RAISE DEBUG '_CDB_Has_Usable_Geom renamed % to %', r1.attname, r1.desired_attname;
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IF r1.desired_attname = geom_name THEN
|
|
|
|
IF r1.desired_attname = const.geomcol THEN
|
|
|
|
has_geom = true;
|
|
|
|
has_geom := true;
|
|
|
|
ELSIF r1.desired_attname = mercgeom_name THEN
|
|
|
|
has_geom_name := r1.attname;
|
|
|
|
has_mercgeom = true;
|
|
|
|
ELSIF r1.desired_attname = const.mercgeomcol THEN
|
|
|
|
|
|
|
|
has_mercgeom := true;
|
|
|
|
|
|
|
|
has_mercgeom_name := r1.attname;
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
@ -984,6 +1039,18 @@ BEGIN
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
END LOOP;
|
|
|
|
END LOOP;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- If geom is the wrong name, just rename it.
|
|
|
|
|
|
|
|
IF has_geom AND has_geom_name != const.geomcol THEN
|
|
|
|
|
|
|
|
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, has_geom_name, const.geomcol);
|
|
|
|
|
|
|
|
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- If mercgeom is the wrong name, just rename it.
|
|
|
|
|
|
|
|
IF has_mercgeom AND has_mercgeom_name != const.geomcol THEN
|
|
|
|
|
|
|
|
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %s', reloid::text, has_mercgeom_name, const.mercgeomcol);
|
|
|
|
|
|
|
|
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- If table is perfect (no transforms required), return TRUE!
|
|
|
|
-- If table is perfect (no transforms required), return TRUE!
|
|
|
|
RETURN has_geom AND has_mercgeom;
|
|
|
|
RETURN has_geom AND has_mercgeom;
|
|
|
@ -997,7 +1064,7 @@ $$ LANGUAGE 'plpgsql';
|
|
|
|
-- a "good" one, and the same for the geometry columns. If all the required
|
|
|
|
-- 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
|
|
|
|
-- columns are in place already, it no-ops and just renames the table to
|
|
|
|
-- the destination if necessary.
|
|
|
|
-- the destination if necessary.
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT, has_usable_primary_key BOOLEAN, has_usable_geoms BOOLEAN, geom_name TEXT, mercgeom_name TEXT, primary_key_name TEXT)
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Rewrite_Table(reloid REGCLASS, destschema TEXT, has_usable_primary_key BOOLEAN, has_usable_geoms BOOLEAN)
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
AS $$
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
DECLARE
|
|
|
@ -1018,16 +1085,21 @@ DECLARE
|
|
|
|
geom_column_source TEXT := '';
|
|
|
|
geom_column_source TEXT := '';
|
|
|
|
|
|
|
|
|
|
|
|
rec RECORD;
|
|
|
|
rec RECORD;
|
|
|
|
|
|
|
|
const RECORD;
|
|
|
|
sql TEXT;
|
|
|
|
sql TEXT;
|
|
|
|
str TEXT;
|
|
|
|
str TEXT;
|
|
|
|
|
|
|
|
table_srid INTEGER;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'Entered _CDB_Rewrite_Table';
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', 'entered function';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Read CartoDB standard column names in
|
|
|
|
|
|
|
|
const := _CDB_Columns();
|
|
|
|
|
|
|
|
|
|
|
|
-- Check calling convention
|
|
|
|
-- No-op if there is no rewrite to be done
|
|
|
|
IF has_usable_primary_key AND has_usable_geoms THEN
|
|
|
|
IF has_usable_primary_key AND has_usable_geoms THEN
|
|
|
|
RAISE EXCEPTION '_CDB_Rewrite_Table should not be called, it has good key and geoms';
|
|
|
|
RETURN true;
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- Save the raw schema/table names for later
|
|
|
|
-- Save the raw schema/table names for later
|
|
|
@ -1039,10 +1111,10 @@ BEGIN
|
|
|
|
-- Put the primary key sequence in the right schema
|
|
|
|
-- Put the primary key sequence in the right schema
|
|
|
|
-- If the new table is not moving, better ensure the sequence name
|
|
|
|
-- If the new table is not moving, better ensure the sequence name
|
|
|
|
-- is unique
|
|
|
|
-- is unique
|
|
|
|
destseq := relname || '_' || primary_key_name || '_seq';
|
|
|
|
destseq := relname || '_' || const.pkey || '_seq';
|
|
|
|
destseq := _CDB_Unique_Relation_Name(destschema, destseq);
|
|
|
|
destseq := _CDB_Unique_Relation_Name(destschema, destseq);
|
|
|
|
destseq := Format('%s.%s', destschema, destseq);
|
|
|
|
destseq := Format('%s.%s', destschema, destseq);
|
|
|
|
EXECUTE Format('CREATE SEQUENCE %s', destseq);
|
|
|
|
PERFORM _CDB_SQL(Format('CREATE SEQUENCE %s', destseq), '_CDB_Rewrite_Table');
|
|
|
|
|
|
|
|
|
|
|
|
-- Salt a temporary table name if we are re-writing in place
|
|
|
|
-- Salt a temporary table name if we are re-writing in place
|
|
|
|
IF destschema = relschema THEN
|
|
|
|
IF destschema = relschema THEN
|
|
|
@ -1056,14 +1128,14 @@ BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
-- Add cartodb ID!
|
|
|
|
-- Add cartodb ID!
|
|
|
|
IF has_usable_primary_key THEN
|
|
|
|
IF has_usable_primary_key THEN
|
|
|
|
sql := sql || primary_key_name;
|
|
|
|
sql := sql || const.pkey;
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
sql := sql || 'nextval(''' || destseq || ''') AS ' || primary_key_name;
|
|
|
|
sql := sql || 'nextval(''' || destseq || ''') AS ' || const.pkey;
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- Add the geometry columns!
|
|
|
|
-- Add the geometry columns!
|
|
|
|
IF has_usable_geoms THEN
|
|
|
|
IF has_usable_geoms THEN
|
|
|
|
sql := sql || ',' || geom_name || ',' || mercgeom_name;
|
|
|
|
sql := sql || ',' || const.geomcol || ',' || const.mercgeomcol;
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
|
|
|
|
-- This gets complicated: we have to make sure the
|
|
|
|
-- This gets complicated: we have to make sure the
|
|
|
@ -1091,6 +1163,8 @@ BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- table_srid = _CDB_Geometry_SRID(reloid, rec.attname);
|
|
|
|
|
|
|
|
|
|
|
|
EXECUTE Format('SELECT ST_SRID(%s) AS srid FROM %s LIMIT 1', rec.attname, reloid::text)
|
|
|
|
EXECUTE Format('SELECT ST_SRID(%s) AS srid FROM %s LIMIT 1', rec.attname, reloid::text)
|
|
|
|
INTO rec;
|
|
|
|
INTO rec;
|
|
|
|
|
|
|
|
|
|
|
@ -1105,19 +1179,20 @@ BEGIN
|
|
|
|
|| ',4326)::Geometry('
|
|
|
|
|| ',4326)::Geometry('
|
|
|
|
|| postgis_typmod_type(a.atttypmod)
|
|
|
|
|| postgis_typmod_type(a.atttypmod)
|
|
|
|
|| ', 4326) AS '
|
|
|
|
|| ', 4326) AS '
|
|
|
|
|| geom_name
|
|
|
|
|| const.geomcol
|
|
|
|
|| ', ST_Transform('
|
|
|
|
|| ', ST_Transform('
|
|
|
|
|| a.attname
|
|
|
|
|| a.attname
|
|
|
|
|| ',3857)::Geometry('
|
|
|
|
|| ',3857)::Geometry('
|
|
|
|
|| postgis_typmod_type(a.atttypmod)
|
|
|
|
|| postgis_typmod_type(a.atttypmod)
|
|
|
|
|| ', 3857) AS '
|
|
|
|
|| ', 3857) AS '
|
|
|
|
|| mercgeom_name,
|
|
|
|
|| const.mercgeomcol,
|
|
|
|
a.attname
|
|
|
|
a.attname
|
|
|
|
INTO geom_transform_sql, geom_column_source
|
|
|
|
INTO geom_transform_sql, geom_column_source
|
|
|
|
FROM pg_class c
|
|
|
|
FROM pg_class c
|
|
|
|
JOIN pg_attribute a ON a.attrelid = c.oid
|
|
|
|
JOIN pg_attribute a ON a.attrelid = c.oid
|
|
|
|
JOIN pg_type t ON a.atttypid = t.oid,
|
|
|
|
JOIN pg_type t ON a.atttypid = t.oid,
|
|
|
|
( SELECT rec.srid AS srid ) AS srid
|
|
|
|
( SELECT rec.srid AS srid ) AS srid
|
|
|
|
|
|
|
|
-- ( SELECT table_srid AS srid ) AS srid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
AND t.typname = 'geometry'
|
|
|
|
AND t.typname = 'geometry'
|
|
|
|
AND a.attnum > 0
|
|
|
|
AND a.attnum > 0
|
|
|
@ -1146,25 +1221,25 @@ BEGIN
|
|
|
|
JOIN pg_type t ON a.atttypid = t.oid
|
|
|
|
JOIN pg_type t ON a.atttypid = t.oid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
AND a.attnum > 0
|
|
|
|
AND a.attnum > 0
|
|
|
|
AND a.attname NOT IN (geom_name, mercgeom_name, primary_key_name, geom_column_source)
|
|
|
|
AND a.attname NOT IN (const.geomcol, const.mercgeomcol, const.pkey, geom_column_source)
|
|
|
|
AND NOT a.attisdropped;
|
|
|
|
AND NOT a.attisdropped;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- No non-cartodb columns? Possible, I guess.
|
|
|
|
-- No non-cartodb columns? Possible, I guess.
|
|
|
|
IF rec.count = 0 THEN
|
|
|
|
IF rec.count = 0 THEN
|
|
|
|
RAISE DEBUG '_CDB_Rewrite_Table found no extra columns';
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', 'found no extra columns';
|
|
|
|
column_name_sql := '';
|
|
|
|
column_name_sql := '';
|
|
|
|
ELSE
|
|
|
|
ELSE
|
|
|
|
RAISE DEBUG '_CDB_Rewrite_Table found extra columns columns %', rec.column_name_sql;
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', Format('found extra columns columns ''%s''', rec.column_name_sql);
|
|
|
|
column_name_sql := rec.column_name_sql;
|
|
|
|
column_name_sql := rec.column_name_sql;
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- Add the source table to the SQL
|
|
|
|
-- Add the source table to the SQL
|
|
|
|
sql := sql || column_name_sql || ' FROM ' || reloid::text;
|
|
|
|
sql := sql || column_name_sql || ' FROM ' || reloid::text;
|
|
|
|
RAISE DEBUG '_CDB_Rewrite_Table generated SQL: %', sql;
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Rewrite_Table): %', sql;
|
|
|
|
|
|
|
|
|
|
|
|
-- Run it!
|
|
|
|
-- Run it!
|
|
|
|
EXECUTE sql;
|
|
|
|
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
|
|
|
|
|
|
|
|
|
|
|
-- Set up the primary key sequence
|
|
|
|
-- Set up the primary key sequence
|
|
|
|
-- If we copied the primary key from the original data, we need
|
|
|
|
-- If we copied the primary key from the original data, we need
|
|
|
@ -1172,38 +1247,34 @@ BEGIN
|
|
|
|
IF has_usable_primary_key THEN
|
|
|
|
IF has_usable_primary_key THEN
|
|
|
|
|
|
|
|
|
|
|
|
EXECUTE Format('SELECT max(%s) FROM %s',
|
|
|
|
EXECUTE Format('SELECT max(%s) FROM %s',
|
|
|
|
primary_key_name, copyname)
|
|
|
|
const.pkey, copyname)
|
|
|
|
INTO destseqmax;
|
|
|
|
INTO destseqmax;
|
|
|
|
|
|
|
|
|
|
|
|
IF FOUND AND destseqmax IS NOT NULL THEN
|
|
|
|
IF FOUND AND destseqmax IS NOT NULL THEN
|
|
|
|
EXECUTE Format('SELECT setval(''%s'', %s)', destseq, destseqmax);
|
|
|
|
PERFORM _CDB_SQL(Format('SELECT setval(''%s'', %s)', destseq, destseqmax), '_CDB_Rewrite_Table');
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- Make the primary key use the sequence as its default value
|
|
|
|
-- Make the primary key use the sequence as its default value
|
|
|
|
sql := Format('ALTER TABLE %s ALTER COLUMN %I SET DEFAULT nextval(''%s'')',
|
|
|
|
sql := Format('ALTER TABLE %s ALTER COLUMN %I SET DEFAULT nextval(''%s'')',
|
|
|
|
copyname, primary_key_name, destseq);
|
|
|
|
copyname, const.pkey, destseq);
|
|
|
|
RAISE DEBUG '_CDB_Rewrite_Table: %', sql;
|
|
|
|
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Make the sequence owned by the table, so when the table drops,
|
|
|
|
-- Make the sequence owned by the table, so when the table drops,
|
|
|
|
-- the sequence does too
|
|
|
|
-- the sequence does too
|
|
|
|
sql := Format('ALTER SEQUENCE %s OWNED BY %s.%s', destseq, copyname, primary_key_name);
|
|
|
|
sql := Format('ALTER SEQUENCE %s OWNED BY %s.%s', destseq, copyname, const.pkey);
|
|
|
|
RAISE DEBUG '_CDB_Rewrite_Table: %', sql;
|
|
|
|
PERFORM _CDB_SQL(sql,'_CDB_Rewrite_Table');
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- We just made a copy, so we can drop the original now
|
|
|
|
-- We just made a copy, so we can drop the original now
|
|
|
|
sql := Format('DROP TABLE %s', reloid::text);
|
|
|
|
sql := Format('DROP TABLE %s', reloid::text);
|
|
|
|
RAISE DEBUG '_CDB_Rewrite_Table: %', sql;
|
|
|
|
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- If we used a temporary destination table
|
|
|
|
-- If we used a temporary destination table
|
|
|
|
-- we can now rename it into place
|
|
|
|
-- we can now rename it into place
|
|
|
|
IF destschema = relschema THEN
|
|
|
|
IF destschema = relschema THEN
|
|
|
|
sql := Format('ALTER TABLE %s RENAME TO %s', copyname, destname);
|
|
|
|
sql := Format('ALTER TABLE %s RENAME TO %s', copyname, destname);
|
|
|
|
RAISE DEBUG '_CDB_Rewrite_Table: %', sql;
|
|
|
|
PERFORM _CDB_SQL(sql, '_CDB_Rewrite_Table');
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
RETURN true;
|
|
|
|
RETURN true;
|
|
|
@ -1215,17 +1286,21 @@ $$ LANGUAGE 'plpgsql';
|
|
|
|
-- Assumes the table already has the right metadata columns
|
|
|
|
-- Assumes the table already has the right metadata columns
|
|
|
|
-- (primary key and two geometry columns) and adds primary key
|
|
|
|
-- (primary key and two geometry columns) and adds primary key
|
|
|
|
-- and geometry indexes if necessary.
|
|
|
|
-- and geometry indexes if necessary.
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Add_Indexes(reloid REGCLASS, geom_name TEXT, mercgeom_name TEXT, primary_key_name TEXT)
|
|
|
|
CREATE OR REPLACE FUNCTION _CDB_Add_Indexes(reloid REGCLASS)
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
RETURNS BOOLEAN
|
|
|
|
AS $$
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
DECLARE
|
|
|
|
rec RECORD;
|
|
|
|
rec RECORD;
|
|
|
|
|
|
|
|
const RECORD;
|
|
|
|
iname TEXT;
|
|
|
|
iname TEXT;
|
|
|
|
sql TEXT;
|
|
|
|
sql TEXT;
|
|
|
|
relname TEXT;
|
|
|
|
relname TEXT;
|
|
|
|
BEGIN
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'Entered _CDB_Add_Indexes';
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Add_Indexes): %', 'entered function';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Read CartoDB standard column names in
|
|
|
|
|
|
|
|
const := _CDB_Columns();
|
|
|
|
|
|
|
|
|
|
|
|
-- Extract just the relname to use for the index names
|
|
|
|
-- Extract just the relname to use for the index names
|
|
|
|
SELECT c.relname
|
|
|
|
SELECT c.relname
|
|
|
@ -1233,7 +1308,6 @@ BEGIN
|
|
|
|
FROM pg_class c
|
|
|
|
FROM pg_class c
|
|
|
|
WHERE c.oid = reloid;
|
|
|
|
WHERE c.oid = reloid;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Is there already a primary key on this table for
|
|
|
|
-- Is there already a primary key on this table for
|
|
|
|
-- a column other than our chosen primary key?
|
|
|
|
-- a column other than our chosen primary key?
|
|
|
|
SELECT ci.relname AS pkey
|
|
|
|
SELECT ci.relname AS pkey
|
|
|
@ -1244,14 +1318,15 @@ BEGIN
|
|
|
|
JOIN pg_class ci ON i.indexrelid = ci.oid
|
|
|
|
JOIN pg_class ci ON i.indexrelid = ci.oid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
WHERE c.oid = reloid
|
|
|
|
AND NOT a.attisdropped
|
|
|
|
AND NOT a.attisdropped
|
|
|
|
AND a.attname != primary_key_name
|
|
|
|
AND a.attname != const.pkey
|
|
|
|
AND i.indisprimary;
|
|
|
|
AND i.indisprimary;
|
|
|
|
|
|
|
|
|
|
|
|
-- Yes? Then drop it, we're adding our own PK to the column
|
|
|
|
-- Yes? Then drop it, we're adding our own PK to the column
|
|
|
|
-- we prefer.
|
|
|
|
-- we prefer.
|
|
|
|
IF FOUND THEN
|
|
|
|
IF FOUND THEN
|
|
|
|
EXECUTE Format('ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s', reloid::text, rec.pkey);
|
|
|
|
RAISE DEBUG 'CDB(_CDB_Add_Indexes): dropping unwanted primary key ''%''', rec.pkey;
|
|
|
|
RAISE DEBUG '_CDB_Add_Indexes dropping unwanted primary key ''%''', rec.pkey;
|
|
|
|
sql := Format('ALTER TABLE %s DROP CONSTRAINT IF EXISTS %s', reloid::text, rec.pkey);
|
|
|
|
|
|
|
|
PERFORM _CDB_SQL(sql, '_CDB_Add_Indexes');
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -1264,16 +1339,15 @@ BEGIN
|
|
|
|
JOIN pg_class ci ON ci.oid = i.indexrelid
|
|
|
|
JOIN pg_class ci ON ci.oid = i.indexrelid
|
|
|
|
WHERE attnum > 0
|
|
|
|
WHERE attnum > 0
|
|
|
|
AND c.oid = reloid
|
|
|
|
AND c.oid = reloid
|
|
|
|
AND a.attname = primary_key_name
|
|
|
|
AND a.attname = const.pkey
|
|
|
|
AND i.indisprimary
|
|
|
|
AND i.indisprimary
|
|
|
|
AND i.indisunique
|
|
|
|
AND i.indisunique
|
|
|
|
AND NOT attisdropped;
|
|
|
|
AND NOT attisdropped;
|
|
|
|
|
|
|
|
|
|
|
|
-- No primary key? Add one.
|
|
|
|
-- No primary key? Add one.
|
|
|
|
IF NOT FOUND THEN
|
|
|
|
IF NOT FOUND THEN
|
|
|
|
sql := Format('ALTER TABLE %s ADD PRIMARY KEY (%s)', reloid::text, primary_key_name);
|
|
|
|
sql := Format('ALTER TABLE %s ADD PRIMARY KEY (%s)', reloid::text, const.pkey);
|
|
|
|
RAISE DEBUG '_CDB_Add_Indexes: %', sql;
|
|
|
|
PERFORM _CDB_SQL(sql, '_CDB_Add_Indexes');
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- Add geometry indexes to all "special geometry columns" that
|
|
|
|
-- Add geometry indexes to all "special geometry columns" that
|
|
|
@ -1285,7 +1359,7 @@ BEGIN
|
|
|
|
JOIN pg_attribute a ON a.attrelid = c.oid AND attnum > 0
|
|
|
|
JOIN pg_attribute a ON a.attrelid = c.oid AND attnum > 0
|
|
|
|
LEFT JOIN pg_index i ON c.oid = i.indrelid AND a.attnum = ANY(i.indkey)
|
|
|
|
LEFT JOIN pg_index i ON c.oid = i.indrelid AND a.attnum = ANY(i.indkey)
|
|
|
|
WHERE NOT attisdropped
|
|
|
|
WHERE NOT attisdropped
|
|
|
|
AND a.attname IN (geom_name, mercgeom_name)
|
|
|
|
AND a.attname IN (const.geomcol, const.mercgeomcol)
|
|
|
|
AND c.oid = reloid
|
|
|
|
AND c.oid = reloid
|
|
|
|
AND i.indexrelid IS NULL
|
|
|
|
AND i.indexrelid IS NULL
|
|
|
|
UNION
|
|
|
|
UNION
|
|
|
@ -1297,13 +1371,12 @@ BEGIN
|
|
|
|
JOIN pg_class ci ON ci.oid = i.indexrelid
|
|
|
|
JOIN pg_class ci ON ci.oid = i.indexrelid
|
|
|
|
JOIN pg_am am ON ci.relam = am.oid
|
|
|
|
JOIN pg_am am ON ci.relam = am.oid
|
|
|
|
WHERE NOT attisdropped
|
|
|
|
WHERE NOT attisdropped
|
|
|
|
AND a.attname IN (geom_name, mercgeom_name)
|
|
|
|
AND a.attname IN (const.geomcol, const.mercgeomcol)
|
|
|
|
AND c.oid = reloid
|
|
|
|
AND c.oid = reloid
|
|
|
|
AND am.amname != 'gist'
|
|
|
|
AND am.amname != 'gist'
|
|
|
|
LOOP
|
|
|
|
LOOP
|
|
|
|
sql := Format('CREATE INDEX %s_%s_gix ON %s USING GIST (%s)', relname, rec.attname, reloid::text, rec.attname);
|
|
|
|
sql := Format('CREATE INDEX %s_%s_gix ON %s USING GIST (%s)', relname, rec.attname, reloid::text, rec.attname);
|
|
|
|
RAISE DEBUG '_CDB_Add_Indexes: %', sql;
|
|
|
|
PERFORM _CDB_SQL(sql, '_CDB_Add_Indexes');
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
END LOOP;
|
|
|
|
END LOOP;
|
|
|
|
|
|
|
|
|
|
|
|
RETURN true;
|
|
|
|
RETURN true;
|
|
|
@ -1316,10 +1389,6 @@ CREATE OR REPLACE FUNCTION CDB_CartodbfyTable2(reloid REGCLASS, destschema TEXT
|
|
|
|
RETURNS void
|
|
|
|
RETURNS void
|
|
|
|
AS $$
|
|
|
|
AS $$
|
|
|
|
DECLARE
|
|
|
|
DECLARE
|
|
|
|
-- Because we're going to change these some day, ha ha ha ha!
|
|
|
|
|
|
|
|
geom_name TEXT := 'the_geom';
|
|
|
|
|
|
|
|
mercgeom_name TEXT := 'the_geom_webmercator';
|
|
|
|
|
|
|
|
primary_key_name TEXT := 'cartodb_id';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
relname TEXT;
|
|
|
|
relname TEXT;
|
|
|
|
relschema TEXT;
|
|
|
|
relschema TEXT;
|
|
|
@ -1333,6 +1402,7 @@ DECLARE
|
|
|
|
rewrite BOOLEAN;
|
|
|
|
rewrite BOOLEAN;
|
|
|
|
index_success BOOLEAN;
|
|
|
|
index_success BOOLEAN;
|
|
|
|
rec RECORD;
|
|
|
|
rec RECORD;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN
|
|
|
|
BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
-- Save the raw schema/table names for later
|
|
|
|
-- Save the raw schema/table names for later
|
|
|
@ -1360,15 +1430,19 @@ BEGIN
|
|
|
|
-- See if there is a primary key column we need to carry along to the
|
|
|
|
-- 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
|
|
|
|
-- new table. If this is true, it implies there is an indexed
|
|
|
|
-- primary key of integer type named (by default) cartodb_id
|
|
|
|
-- primary key of integer type named (by default) cartodb_id
|
|
|
|
SELECT _CDB_Has_Usable_Primary_ID(reloid, primary_key_name) AS has_usable_primary_key
|
|
|
|
SELECT _CDB_Has_Usable_Primary_ID(reloid) AS has_usable_primary_key
|
|
|
|
INTO STRICT has_usable_primary_key;
|
|
|
|
INTO STRICT has_usable_primary_key;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): has_usable_primary_key %', has_usable_primary_key;
|
|
|
|
|
|
|
|
|
|
|
|
-- See if the geometry columns we need are already available
|
|
|
|
-- See if the geometry columns we need are already available
|
|
|
|
-- on the table. If they are, we don't need to do any bulk
|
|
|
|
-- on the table. If they are, we don't need to do any bulk
|
|
|
|
-- transformation of the table, we can just ensure proper
|
|
|
|
-- transformation of the table, we can just ensure proper
|
|
|
|
-- indexes are in place and apply a rename
|
|
|
|
-- indexes are in place and apply a rename
|
|
|
|
SELECT _CDB_Has_Usable_Geom(reloid, geom_name, mercgeom_name) AS has_usable_geoms
|
|
|
|
SELECT _CDB_Has_Usable_Geom(reloid) AS has_usable_geoms
|
|
|
|
INTO STRICT has_usable_geoms;
|
|
|
|
INTO STRICT has_usable_geoms;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): has_usable_geoms %', has_usable_geoms;
|
|
|
|
|
|
|
|
|
|
|
|
-- We can only avoid a rewrite if both the key and
|
|
|
|
-- We can only avoid a rewrite if both the key and
|
|
|
|
-- geometry are usable
|
|
|
|
-- geometry are usable
|
|
|
@ -1378,22 +1452,22 @@ BEGIN
|
|
|
|
-- a destination schema, so do that now
|
|
|
|
-- a destination schema, so do that now
|
|
|
|
IF NOT rewrite AND destschema != relschema THEN
|
|
|
|
IF NOT rewrite AND destschema != relschema THEN
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'perfect table needs to be moved to schema (%)', destschema;
|
|
|
|
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): perfect table needs to be moved to schema (%)', destschema;
|
|
|
|
EXECUTE Format('ALTER TABLE %s SET SCHEMA %s', reloid::text, destschema);
|
|
|
|
EXECUTE Format('ALTER TABLE %s SET SCHEMA %s', reloid::text, destschema);
|
|
|
|
|
|
|
|
|
|
|
|
-- Don't move anything, just make sure our destination information is set right
|
|
|
|
-- Don't move anything, just make sure our destination information is set right
|
|
|
|
ELSIF NOT rewrite AND destschema = relschema THEN
|
|
|
|
ELSIF NOT rewrite AND destschema = relschema THEN
|
|
|
|
|
|
|
|
|
|
|
|
RAISE DEBUG 'perfect table in the perfect place';
|
|
|
|
RAISE DEBUG 'CDB(CDB_CartodbfyTable2): perfect table in the perfect place';
|
|
|
|
|
|
|
|
|
|
|
|
-- We must rewrite, so here we go...
|
|
|
|
-- We must rewrite, so here we go...
|
|
|
|
ELSIF rewrite THEN
|
|
|
|
ELSIF rewrite THEN
|
|
|
|
|
|
|
|
|
|
|
|
SELECT _CDB_Rewrite_Table(reloid, destschema, has_usable_primary_key, has_usable_geoms, geom_name, mercgeom_name, primary_key_name)
|
|
|
|
SELECT _CDB_Rewrite_Table(reloid, destschema, has_usable_primary_key, has_usable_geoms)
|
|
|
|
INTO STRICT rewrite_success;
|
|
|
|
INTO STRICT rewrite_success;
|
|
|
|
|
|
|
|
|
|
|
|
IF NOT rewrite_success THEN
|
|
|
|
IF NOT rewrite_success THEN
|
|
|
|
RAISE EXCEPTION 'Cartodbfying % (rewriting table): % (%)', reloid, SQLERRM, SQLSTATE;
|
|
|
|
PERFORM _CDB_Error('rewriting table', 'CDB_CartodbfyTable2');
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
@ -1402,11 +1476,11 @@ BEGIN
|
|
|
|
destoid := (destschema || '.' || destname)::regclass;
|
|
|
|
destoid := (destschema || '.' || destname)::regclass;
|
|
|
|
|
|
|
|
|
|
|
|
-- Add indexes to the destination table, as necessary
|
|
|
|
-- Add indexes to the destination table, as necessary
|
|
|
|
SELECT _CDB_Add_Indexes(destoid, geom_name, mercgeom_name, primary_key_name)
|
|
|
|
SELECT _CDB_Add_Indexes(destoid)
|
|
|
|
INTO STRICT index_success;
|
|
|
|
INTO STRICT index_success;
|
|
|
|
|
|
|
|
|
|
|
|
IF NOT index_success THEN
|
|
|
|
IF NOT index_success THEN
|
|
|
|
RAISE EXCEPTION 'Cartodbfying % (indexing table): % (%)', destoid, SQLERRM, SQLSTATE;
|
|
|
|
PERFORM _CDB_Error('indexing table', 'CDB_CartodbfyTable2');
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
|
|
|
|
-- Add triggers to the destination table, as necessary
|
|
|
|
-- Add triggers to the destination table, as necessary
|
|
|
|