CDB_FederatedServerTables: Improve permission handling and error messages

This commit is contained in:
Raúl Marín
2019-11-14 13:10:21 +01:00
parent e231500c46
commit 4920029560
2 changed files with 18 additions and 9 deletions
@@ -227,7 +227,7 @@ BEGIN
EXECUTE FORMAT('IMPORT FOREIGN SCHEMA %I LIMIT TO (%I) FROM SERVER %I INTO %I;', EXECUTE FORMAT('IMPORT FOREIGN SCHEMA %I LIMIT TO (%I) FROM SERVER %I INTO %I;',
remote_schema, remote_table, server_internal, local_schema); remote_schema, remote_table, server_internal, local_schema);
EXCEPTION WHEN OTHERS THEN EXCEPTION WHEN OTHERS THEN
RAISE EXCEPTION 'Could not import schema "%" of server "%"', remote_schema, server; RAISE EXCEPTION 'Could not import schema "%" of server "%": %', remote_schema, server, SQLERRM;
END; END;
BEGIN BEGIN
@@ -284,6 +284,10 @@ BEGIN
webmercator_expression webmercator_expression
]; ];
-- To create the view we switch to the caller role to make sure we have permissions
-- to write in the destination schema
RESET ROLE;
-- Create a view with homogeneous CDB fields -- Create a view with homogeneous CDB fields
BEGIN BEGIN
EXECUTE format( EXECUTE format(
@@ -294,12 +298,17 @@ BEGIN
array_to_string(carto_columns_expression || rest_of_cols, ','), array_to_string(carto_columns_expression || rest_of_cols, ','),
src_table src_table
); );
EXCEPTION EXCEPTION WHEN OTHERS THEN
WHEN insufficient_privilege THEN IF EXISTS (SELECT to_regclass(local_name)) THEN
RAISE EXCEPTION 'Could not import table "%" as "%": "%" already exists', remote_table, local_name, local_name; RAISE EXCEPTION 'Could not import table "%" as "%" already exists: %', remote_table, local_name, SQLERRM;
WHEN OTHERS THEN ELSE
RAISE EXCEPTION 'Could not import table "%" as "%": %', remote_table, local_name, SQLERRM; RAISE EXCEPTION 'Could not import table "%" as "%": %', remote_table, local_name, SQLERRM;
END IF;
END; END;
EXECUTE format('ALTER VIEW %1$I OWNER TO %I',
local_name,
cartodb.__CDB_FS_Generate_Server_Role_Name(server_internal));
END END
$$ $$
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE; LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
+3 -3
View File
@@ -27,7 +27,7 @@ ERROR: Server "Does not exist" does not exist
## Registering a table: NULL server fails ## Registering a table: NULL server fails
ERROR: Server name cannot be NULL ERROR: Server name cannot be NULL
## Registering a table: Invalid schema fails ## Registering a table: Invalid schema fails
ERROR: Could not import schema "Does not exist" of server "loopback" ERROR: Could not import schema "Does not exist" of server "loopback": schema "Does not exist" is not present on foreign server "cdb_fs_loopback"
## Registering a table: NULL schema fails ## Registering a table: NULL schema fails
ERROR: Schema name cannot be NULL ERROR: Schema name cannot be NULL
## Registering a table: Invalid table fails ## Registering a table: Invalid table fails
@@ -50,10 +50,10 @@ ERROR: non geometry column "Does not exists"
## Target conflict is handled nicely: Table ## Target conflict is handled nicely: Table
CREATE TABLE CREATE TABLE
ERROR: Could not import table "remote_geom" as "localtable": "localtable" already exists ERROR: Could not import table "remote_geom" as "localtable" already exists: "localtable" is not a view
## Target conflict is handled nicely: View ## Target conflict is handled nicely: View
CREATE VIEW CREATE VIEW
ERROR: Could not import table "remote_geom" as "localtable2": "localtable2" already exists ERROR: Could not import table "remote_geom" as "localtable2" already exists: cannot change name of view column "a" to "cartodb_id"
DROP VIEW DROP VIEW
DROP TABLE DROP TABLE
## Registering tables does not work without permissions ## Registering tables does not work without permissions