Add CDB_ApplyQueriesSafe to apply CDB_GetTableQueries safely

This commit is contained in:
Raúl Marín 2020-11-24 13:21:43 +01:00
parent 26e49dce69
commit 67139ef1e6
4 changed files with 68 additions and 2 deletions

View File

@ -2,6 +2,7 @@
* Raised minimum PG version to 11.
* Add `CDB_RegenerateTable` function to regenerate a table.
* Add `CDB_GetTableQueries` to get the queries of a table (constraints, indices, triggers...).
* Add `CDB_ApplyQueriesSafe` to apply the queries of `CDB_GetTableQueries` discarding any exceptions.
* Deprecate creation of new overview tables.
* _cdb_has_usable_geom_record: Check only the extension schema.

View File

@ -88,6 +88,28 @@ END
$$
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
-- Helper function to apply the result of CDB_GetTableQueries catching and discarding any exceptions
CREATE OR REPLACE FUNCTION @extschema@.CDB_ApplyQueriesSafe(queries TEXT[])
RETURNS void
AS
$$
DECLARE
i INTEGER;
BEGIN
IF array_length(queries, 1) > 0 THEN
FOR i IN 1 .. array_upper(queries, 1)
LOOP
BEGIN
EXECUTE queries[i];
EXCEPTION WHEN OTHERS THEN
CONTINUE;
END;
END LOOP;
END IF;
END
$$
LANGUAGE PLPGSQL STRICT VOLATILE PARALLEL UNSAFE;
-- Regenerates a table
CREATE OR REPLACE FUNCTION @extschema@.CDB_RegenerateTable(tableoid OID)
RETURNS void

View File

@ -251,18 +251,37 @@ COMMIT;
\echo '## Test transaction with delete'
BEGIN;
DELETE FROM testtable WHERE true;
DELETE FROM testtable;
SELECT CDB_RegenerateTable('public.testtable'::regclass);
COMMIT;
\echo '## Test transaction with delete + cartodbfy'
BEGIN;
INSERT INTO testtable(stable,c1,c2,c3,c4) VALUES (1,2,3,4,5), (2,3,4,5,6), (3,4,5,6,7);
DELETE FROM testtable WHERE true;
DELETE FROM testtable;
SELECT CDB_RegenerateTable('public.testtable'::regclass);
SELECT CDB_CartodbfyTable('public'::TEXT, 'public.testtable'::REGCLASS);
COMMIT;
\echo '## Test replacement in import (drop c3 and c4 columns)'
CREATE INDEX testtable_c4_idx ON testtable (c4 NULLS FIRST);
\d testtable
SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename = 'testtable' ORDER BY tablename, indexname;
DO $$
DECLARE
queries TEXT[] := CDB_GetTableQueries_TestHelper('testtable'::regclass, true);
BEGIN
DROP TABLE testtable;
CREATE TABLE testtable (stable integer, c1 integer, c2 integer);
PERFORM CDB_CartodbfyTable('public.testtable');
PERFORM CDB_ApplyQueriesSafe(queries);
END$$;
\d testtable
SELECT tablename, indexname, indexdef FROM pg_indexes WHERE tablename = 'testtable' ORDER BY tablename, indexname;
\echo '## teardown'
DROP TABLE measurement CASCADE;

View File

@ -195,6 +195,30 @@ DELETE 3
testtable
COMMIT
## Test replacement in import (drop c3 and c4 columns)
CREATE INDEX
cartodb_id|bigint||not null|nextval('testtable_cartodb_id_seq'::regclass)
the_geom|geometry(Geometry,4326)|||
the_geom_webmercator|geometry(Geometry,3857)|||
stable|integer|||
c1|integer|||
c2|integer|||
c3|integer|||
c4|integer|||
testtable|testtable_c4_idx|CREATE INDEX testtable_c4_idx ON public.testtable USING btree (c4 NULLS FIRST)
testtable|testtable_pkey|CREATE UNIQUE INDEX testtable_pkey ON public.testtable USING btree (cartodb_id)
testtable|testtable_the_geom_idx|CREATE INDEX testtable_the_geom_idx ON public.testtable USING gist (the_geom)
testtable|testtable_the_geom_webmercator_idx|CREATE INDEX testtable_the_geom_webmercator_idx ON public.testtable USING gist (the_geom_webmercator)
DO
cartodb_id|bigint||not null|nextval('testtable_cartodb_id_seq'::regclass)
the_geom|geometry(Geometry,4326)|||
the_geom_webmercator|geometry(Geometry,3857)|||
stable|integer|||
c1|integer|||
c2|integer|||
testtable|testtable_pkey|CREATE UNIQUE INDEX testtable_pkey ON public.testtable USING btree (cartodb_id)
testtable|testtable_the_geom_idx|CREATE INDEX testtable_the_geom_idx ON public.testtable USING gist (the_geom)
testtable|testtable_the_geom_webmercator_idx|CREATE INDEX testtable_the_geom_webmercator_idx ON public.testtable USING gist (the_geom_webmercator)
## teardown
DROP TABLE
DROP TABLE