2014-05-06 00:48:48 +08:00
|
|
|
SET client_min_messages TO error;
|
2015-02-17 22:40:26 +08:00
|
|
|
\set VERBOSITY default
|
2014-05-06 00:48:48 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION CDB_CartodbfyTableCheck(tabname regclass, label text)
|
|
|
|
RETURNS text AS
|
|
|
|
$$
|
|
|
|
DECLARE
|
|
|
|
sql TEXT;
|
|
|
|
id INTEGER;
|
|
|
|
rec RECORD;
|
|
|
|
lag INTERVAL;
|
|
|
|
tmp INTEGER;
|
|
|
|
ogc_geom geometry_columns; -- old the_geom record in geometry_columns
|
|
|
|
ogc_merc geometry_columns; -- old the_geom_webmercator record in geometry_columns
|
2015-08-14 06:59:45 +08:00
|
|
|
tabtext TEXT;
|
2014-05-06 00:48:48 +08:00
|
|
|
BEGIN
|
|
|
|
|
|
|
|
-- Save current constraints on geometry columns, if any
|
|
|
|
ogc_geom = ('','','','',0,0,'GEOMETRY');
|
|
|
|
ogc_merc = ogc_geom;
|
|
|
|
sql := 'SELECT gc.* FROM geometry_columns gc, pg_class c, pg_namespace n '
|
|
|
|
|| 'WHERE c.oid = ' || tabname::oid || ' AND n.oid = c.relnamespace'
|
|
|
|
|| ' AND gc.f_table_schema = n.nspname AND gc.f_table_name = c.relname'
|
|
|
|
|| ' AND gc.f_geometry_column IN ( ' || quote_literal('the_geom')
|
|
|
|
|| ',' || quote_literal('the_geom_webmercator') || ')';
|
|
|
|
FOR rec IN EXECUTE sql LOOP
|
|
|
|
IF rec.f_geometry_column = 'the_geom' THEN
|
|
|
|
ogc_geom := rec;
|
|
|
|
ELSE
|
|
|
|
ogc_merc := rec;
|
|
|
|
END IF;
|
|
|
|
END LOOP;
|
|
|
|
|
2015-08-14 06:59:45 +08:00
|
|
|
tabtext := Format('%s.%s','public',tabname);
|
|
|
|
RAISE NOTICE 'CARTODBFYING % !!!!', tabtext;
|
2014-07-04 21:55:08 +08:00
|
|
|
PERFORM CDB_CartodbfyTable('public', tabname);
|
2015-08-14 06:59:45 +08:00
|
|
|
tabname := tabtext::regclass;
|
2014-05-06 00:48:48 +08:00
|
|
|
|
|
|
|
sql := 'INSERT INTO ' || tabname::text || '(the_geom) values ( CDB_LatLng(2,1) ) RETURNING cartodb_id';
|
|
|
|
EXECUTE sql INTO STRICT id;
|
2015-08-12 01:52:34 +08:00
|
|
|
sql := 'SELECT the_geom_webmercator FROM '
|
2014-05-06 00:48:48 +08:00
|
|
|
|| tabname::text || ' WHERE cartodb_id = ' || id;
|
|
|
|
EXECUTE sql INTO STRICT rec;
|
|
|
|
|
|
|
|
-- Check the_geom_webmercator trigger
|
|
|
|
IF round(st_x(rec.the_geom_webmercator)) != 111319 THEN
|
|
|
|
RAISE EXCEPTION 'the_geom_webmercator X is % (expecting 111319)', round(st_x(rec.the_geom_webmercator));
|
|
|
|
END IF;
|
|
|
|
IF round(st_y(rec.the_geom_webmercator)) != 222684 THEN
|
|
|
|
RAISE EXCEPTION 'the_geom_webmercator Y is % (expecting 222684)', round(st_y(rec.the_geom_webmercator));
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Check CDB_TableMetadata entry
|
|
|
|
sql := 'SELECT * FROM CDB_TableMetadata WHERE tabname = ' || tabname::oid;
|
|
|
|
EXECUTE sql INTO STRICT rec;
|
|
|
|
lag = rec.updated_at - now();
|
|
|
|
IF lag > '1 second' THEN
|
|
|
|
RAISE EXCEPTION 'updated_at in CDB_TableMetadata not set to now() after insert [ valued % ago ]', lag;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Check geometry_columns entries
|
|
|
|
tmp := 0;
|
|
|
|
FOR rec IN
|
|
|
|
SELECT
|
|
|
|
CASE WHEN gc.f_geometry_column = 'the_geom' THEN 4326
|
|
|
|
ELSE 3857 END as expsrid,
|
|
|
|
CASE WHEN gc.f_geometry_column = 'the_geom' THEN ogc_geom.type
|
|
|
|
ELSE ogc_merc.type END as exptype, gc.*
|
|
|
|
FROM geometry_columns gc, pg_class c, pg_namespace n
|
|
|
|
WHERE c.oid = tabname::oid AND n.oid = c.relnamespace
|
|
|
|
AND gc.f_table_schema = n.nspname AND gc.f_table_name = c.relname
|
|
|
|
AND gc.f_geometry_column IN ( 'the_geom', 'the_geom_webmercator')
|
|
|
|
LOOP
|
|
|
|
tmp := tmp + 1;
|
|
|
|
-- Check SRID constraint
|
|
|
|
IF rec.srid != rec.expsrid THEN
|
|
|
|
RAISE EXCEPTION 'SRID of % in geometry_columns is %, expected %',
|
|
|
|
rec.f_geometry_column, rec.srid, rec.expsrid;
|
|
|
|
END IF;
|
|
|
|
-- Check TYPE constraint didn't change
|
2015-08-14 19:40:10 +08:00
|
|
|
IF (rec.type != 'GEOMETRY') AND (rec.type != 'POINT') THEN
|
2014-05-06 00:48:48 +08:00
|
|
|
RAISE EXCEPTION 'type of % in geometry_columns is %, expected %',
|
|
|
|
rec.f_geometry_column, rec.type, rec.exptype;
|
|
|
|
END IF;
|
|
|
|
-- check coord_dimension ?
|
|
|
|
END LOOP;
|
|
|
|
IF tmp != 2 THEN
|
|
|
|
RAISE EXCEPTION '% entries found for table % in geometry_columns, expected 2', tmp, tabname;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
-- Check GiST index
|
|
|
|
sql := 'SELECT a.attname, count(ri.relname) FROM'
|
|
|
|
|| ' pg_index i, pg_class c, pg_class ri, pg_attribute a, pg_opclass o'
|
|
|
|
|| ' WHERE i.indrelid = c.oid AND ri.oid = i.indexrelid'
|
|
|
|
|| ' AND a.attrelid = ri.oid AND o.oid = i.indclass[0]'
|
|
|
|
|| ' AND a.attname IN ( ' || quote_literal('the_geom')
|
|
|
|
|| ',' || quote_literal('the_geom_webmercator') || ')'
|
|
|
|
|| ' AND ri.relnatts = 1 AND o.opcname = '
|
|
|
|
|| quote_literal('gist_geometry_ops_2d')
|
|
|
|
|| ' AND c.oid = ' || tabname::oid
|
|
|
|
|| ' GROUP BY a.attname';
|
|
|
|
RAISE NOTICE 'sql: %', sql;
|
|
|
|
EXECUTE sql;
|
|
|
|
GET DIAGNOSTICS tmp = ROW_COUNT;
|
|
|
|
IF tmp != 2 THEN
|
|
|
|
RAISE EXCEPTION '% gist indices found on the_geom and the_geom_webmercator, expected 2', tmp;
|
|
|
|
END IF;
|
|
|
|
|
2015-08-12 23:58:30 +08:00
|
|
|
-- Check null constraint on cartodb_id, created_at, updated_at
|
|
|
|
SELECT count(*) FROM pg_attribute a, pg_class c WHERE c.oid = tabname::oid
|
|
|
|
AND a.attrelid = c.oid AND NOT a.attisdropped AND a.attname in
|
|
|
|
( 'cartodb_id' )
|
|
|
|
AND NOT a.attnotnull INTO strict tmp;
|
|
|
|
IF tmp > 0 THEN
|
|
|
|
RAISE EXCEPTION 'cartodb_id is missing not-null constraint';
|
|
|
|
END IF;
|
|
|
|
|
2014-05-06 00:48:48 +08:00
|
|
|
-- Cleanup
|
|
|
|
sql := 'DELETE FROM ' || tabname::text || ' WHERE cartodb_id = ' || id;
|
|
|
|
EXECUTE sql;
|
|
|
|
|
|
|
|
RETURN label || ' cartodbfied fine';
|
|
|
|
END;
|
|
|
|
$$
|
|
|
|
LANGUAGE 'plpgsql';
|
|
|
|
|
|
|
|
-- table with single non-geometrical column
|
2015-08-14 06:59:45 +08:00
|
|
|
CREATE TABLE t AS SELECT 1::int as a;
|
2015-08-14 18:22:59 +08:00
|
|
|
SELECT CDB_CartodbfyTable('public', 't'); -- should fail
|
|
|
|
SELECT CDB_SetUserQuotaInBytes(0); -- Set user quota to infinite
|
2014-05-06 00:48:48 +08:00
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'single non-geometrical column');
|
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with existing srid-unconstrained (but type-constrained) the_geom
|
|
|
|
CREATE TABLE t AS SELECT ST_SetSRID(ST_MakePoint(0,0),4326)::geometry(point) as the_geom;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'srid-unconstrained the_geom');
|
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with mixed-srid the_geom values
|
|
|
|
CREATE TABLE t AS SELECT ST_SetSRID(ST_MakePoint(-1,-1),4326) as the_geom
|
|
|
|
UNION ALL SELECT ST_SetSRID(ST_MakePoint(0,0),3857);
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'mixed-srid the_geom');
|
2014-06-06 00:20:04 +08:00
|
|
|
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)) FROM t;
|
2014-05-06 00:48:48 +08:00
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with wrong srid-constrained the_geom values
|
|
|
|
CREATE TABLE t AS SELECT 'SRID=3857;LINESTRING(222638.981586547 222684.208505545, 111319.490793274 111325.142866385)'::geometry(geometry,3857) as the_geom;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'wrong srid-constrained the_geom');
|
2014-06-06 00:21:52 +08:00
|
|
|
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)),ST_Extent(ST_SnapToGrid(the_geom_webmercator,1)) FROM t;
|
2014-05-06 00:48:48 +08:00
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with wrong srid-constrained the_geom_webmercator values (and no the_geom!)
|
|
|
|
CREATE TABLE t AS SELECT 'SRID=4326;LINESTRING(1 1,2 2)'::geometry(geometry,4326) as the_geom_webmercator;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'wrong srid-constrained the_geom_webmercator');
|
|
|
|
-- expect the_geom to be populated from the_geom_webmercator
|
2014-06-06 00:20:04 +08:00
|
|
|
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)) FROM t;
|
2014-05-06 00:48:48 +08:00
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with existing triggered the_geom
|
|
|
|
CREATE TABLE t AS SELECT 'SRID=4326;LINESTRING(1 1,2 2)'::geometry(geometry) as the_geom;
|
|
|
|
CREATE TRIGGER update_the_geom_webmercator_trigger BEFORE UPDATE OF the_geom ON t
|
2014-06-04 23:03:17 +08:00
|
|
|
FOR EACH ROW EXECUTE PROCEDURE _CDB_update_the_geom_webmercator();
|
2014-05-06 00:48:48 +08:00
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'trigger-protected the_geom');
|
2014-06-06 00:20:04 +08:00
|
|
|
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)) FROM t;
|
2014-05-06 00:48:48 +08:00
|
|
|
DROP TABLE t;
|
|
|
|
|
2015-08-14 19:45:57 +08:00
|
|
|
-- INFO: disabled because cartodbfy does not longer consider text columns for primary ID
|
|
|
|
-- -- table with existing cartodb_id field of type text
|
|
|
|
-- CREATE TABLE t AS SELECT 10::text as cartodb_id;
|
|
|
|
-- SELECT CDB_CartodbfyTableCheck('t', 'text cartodb_id');
|
|
|
|
-- select cartodb_id/2 FROM t;
|
|
|
|
-- DROP TABLE t;
|
|
|
|
|
|
|
|
-- INFO: disabled because cartodbfy does not longer consider text columns for primary ID
|
|
|
|
-- -- table with existing cartodb_id field of type text not casting
|
|
|
|
-- CREATE TABLE t AS SELECT 'nan' as cartodb_id;
|
|
|
|
-- SELECT CDB_CartodbfyTableCheck('t', 'uncasting text cartodb_id');
|
|
|
|
-- select cartodb_id,_cartodb_id0 FROM t;
|
|
|
|
-- DROP TABLE t;
|
2014-05-22 20:11:09 +08:00
|
|
|
|
2014-05-28 16:44:23 +08:00
|
|
|
-- table with existing cartodb_id field of type int4 not sequenced
|
|
|
|
CREATE TABLE t AS SELECT 1::int4 as cartodb_id;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'unsequenced cartodb_id');
|
2015-08-22 04:10:05 +08:00
|
|
|
SELECT cartodb_id FROM t;
|
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with text geometry column
|
|
|
|
CREATE TABLE t AS SELECT 'SRID=4326;POINT(1 1)'::text AS the_geom, 1::int4 as cartodb_id;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'text the_geom column');
|
|
|
|
SELECT cartodb_id FROM t;
|
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with text geometry column, no SRS
|
|
|
|
CREATE TABLE t AS SELECT 'POINT(1 1)'::text AS the_geom, 1::int4 as cartodb_id;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'text the_geom column, no srs');
|
|
|
|
SELECT cartodb_id FROM t;
|
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with text geometry column, unusual SRS
|
|
|
|
CREATE TABLE t AS SELECT 'SRID=26910;POINT(1 1)'::text AS the_geom, 1::int4 as cartodb_id;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'text the_geom column, srs = 26819');
|
|
|
|
SELECT cartodb_id FROM t;
|
|
|
|
DROP TABLE t;
|
|
|
|
|
|
|
|
-- table with text unparseable geometry column
|
|
|
|
CREATE TABLE t AS SELECT 'SRID=26910;PONT(1 1)'::text AS the_geom, 1::int4 as cartodb_id;
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'text the_geom column, unparseable content');
|
|
|
|
SELECT cartodb_id FROM t;
|
2014-05-28 16:44:23 +08:00
|
|
|
DROP TABLE t;
|
|
|
|
|
2014-06-12 01:21:19 +08:00
|
|
|
-- table with existing cartodb_id serial primary key
|
|
|
|
CREATE TABLE t ( cartodb_id serial primary key );
|
|
|
|
SELECT CDB_CartodbfyTableCheck('t', 'cartodb_id serial primary key');
|
|
|
|
SELECT c.conname, a.attname FROM pg_constraint c, pg_attribute a
|
|
|
|
WHERE c.conrelid = 't'::regclass and a.attrelid = c.conrelid
|
|
|
|
AND c.conkey[1] = a.attnum AND NOT a.attisdropped;
|
|
|
|
DROP TABLE t;
|
|
|
|
|
2015-08-27 22:33:46 +08:00
|
|
|
-- tables can be renamed and there's no index name clashing #123
|
|
|
|
CREATE TABLE original();
|
|
|
|
SELECT CDB_CartodbfyTable('original');
|
|
|
|
ALTER TABLE original RENAME TO original_renamed;
|
|
|
|
CREATE TABLE original();
|
|
|
|
SELECT CDB_CartodbfyTable('original');
|
|
|
|
DROP TABLE original_renamed;
|
|
|
|
DROP TABLE original;
|
|
|
|
|
2015-09-10 00:27:29 +08:00
|
|
|
-- Table always have a default seq value after cartodbfy #138
|
2015-09-07 22:28:35 +08:00
|
|
|
CREATE TABLE bug_empty_table_no_seq (
|
|
|
|
cartodb_id integer,
|
|
|
|
the_geom geometry(Geometry,4326),
|
|
|
|
the_geom_webmercator geometry(Geometry,3857),
|
|
|
|
name text,
|
|
|
|
description text
|
|
|
|
);
|
2015-09-10 00:27:29 +08:00
|
|
|
SELECT CDB_CartodbfyTableCheck('bug_empty_table_no_seq', 'Table always have a default seq value after cartodbfy #138');
|
2015-09-07 22:28:35 +08:00
|
|
|
INSERT INTO bug_empty_table_no_seq DEFAULT VALUES;
|
|
|
|
DROP TABLE bug_empty_table_no_seq;
|
|
|
|
|
2015-09-10 00:27:29 +08:00
|
|
|
-- Existing cartodb_id values are respected
|
|
|
|
CREATE table existing_cartodb_id (
|
|
|
|
cartodb_id integer,
|
|
|
|
the_geom geometry(Geometry,4326),
|
|
|
|
the_geom_webmercator geometry(Geometry,3857),
|
|
|
|
name text,
|
|
|
|
description text
|
|
|
|
);
|
|
|
|
INSERT INTO existing_cartodb_id (cartodb_id, description) VALUES
|
2015-09-11 00:18:13 +08:00
|
|
|
(10, 'a'),
|
|
|
|
(20, 'b'),
|
|
|
|
(30, 'c');
|
2015-09-10 00:27:29 +08:00
|
|
|
SELECT CDB_CartodbfyTableCheck('existing_cartodb_id', 'Existing cartodb_id values are respected #138');
|
|
|
|
SELECT * from existing_cartodb_id;
|
|
|
|
DROP TABLE existing_cartodb_id;
|
|
|
|
|
2015-09-11 22:16:54 +08:00
|
|
|
-- Table with both the_geom and wkb_geometry
|
|
|
|
CREATE TABLE many_geometry_columns (
|
|
|
|
the_geom geometry,
|
2015-09-14 23:55:30 +08:00
|
|
|
wkb_geometry geometry(MultiPoint,4326),
|
|
|
|
description varchar
|
2015-09-11 22:16:54 +08:00
|
|
|
);
|
|
|
|
INSERT INTO many_geometry_columns (the_geom, wkb_geometry) VALUES
|
|
|
|
('0104000020E61000000100000001010000007108B023698052C03CEEA53A2E5D4440', '0104000020E61000000100000001010000007108B023698052C03CEEA53A2E5D4440'),
|
|
|
|
('0104000020E6100000010000000101000000864C9E57618052C0994F0C7F3C5B4440', '0104000020E6100000010000000101000000864C9E57618052C0994F0C7F3C5B4440');
|
|
|
|
SELECT CDB_CartodbfyTableCheck('many_geometry_columns', 'Table with both the_geom and wkb_geometry #141');
|
|
|
|
SELECT * FROM many_geometry_columns;
|
|
|
|
DROP TABLE many_geometry_columns;
|
2015-09-10 00:27:29 +08:00
|
|
|
|
2015-09-16 18:09:32 +08:00
|
|
|
-- Many colliding geom columns
|
|
|
|
CREATE TABLE many_colliding_columns (
|
|
|
|
the_geom varchar,
|
|
|
|
the_geom_webmercator varchar,
|
|
|
|
my_geom geometry,
|
|
|
|
my_mercgeom geometry(Point, 3857),
|
|
|
|
cartodb_id varchar,
|
|
|
|
my_pk integer primary key
|
|
|
|
);
|
|
|
|
INSERT INTO many_colliding_columns VALUES (
|
|
|
|
'foo', 'bar', 'SRID=4326;POINT(0 0)', 'SRID=3857;POINT(0 0)', 'nerf', 1
|
|
|
|
);
|
|
|
|
SELECT CDB_CartodbfyTableCheck('many_colliding_columns', 'Many colliding columns #141');
|
|
|
|
DROP TABLE many_colliding_columns;
|
|
|
|
|
2015-09-10 00:27:29 +08:00
|
|
|
|
2014-05-06 00:48:48 +08:00
|
|
|
-- TODO: table with existing custom-triggered the_geom
|
|
|
|
|
|
|
|
DROP FUNCTION CDB_CartodbfyTableCheck(regclass, text);
|
2014-05-12 19:07:47 +08:00
|
|
|
DROP FUNCTION _CDB_UserQuotaInBytes();
|