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

View File

@ -227,7 +227,7 @@ BEGIN
EXECUTE FORMAT('IMPORT FOREIGN SCHEMA %I LIMIT TO (%I) FROM SERVER %I INTO %I;',
remote_schema, remote_table, server_internal, local_schema);
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;
BEGIN
@ -284,6 +284,10 @@ BEGIN
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
BEGIN
EXECUTE format(
@ -294,12 +298,17 @@ BEGIN
array_to_string(carto_columns_expression || rest_of_cols, ','),
src_table
);
EXCEPTION
WHEN insufficient_privilege THEN
RAISE EXCEPTION 'Could not import table "%" as "%": "%" already exists', remote_table, local_name, local_name;
WHEN OTHERS THEN
RAISE EXCEPTION 'Could not import table "%" as "%": %', remote_table, local_name, SQLERRM;
EXCEPTION WHEN OTHERS THEN
IF EXISTS (SELECT to_regclass(local_name)) THEN
RAISE EXCEPTION 'Could not import table "%" as "%" already exists: %', remote_table, local_name, SQLERRM;
ELSE
RAISE EXCEPTION 'Could not import table "%" as "%": %', remote_table, local_name, SQLERRM;
END IF;
END;
EXECUTE format('ALTER VIEW %1$I OWNER TO %I',
local_name,
cartodb.__CDB_FS_Generate_Server_Role_Name(server_internal));
END
$$
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;

View File

@ -27,7 +27,7 @@ ERROR: Server "Does not exist" does not exist
## Registering a table: NULL server fails
ERROR: Server name cannot be NULL
## 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
ERROR: Schema name cannot be NULL
## Registering a table: Invalid table fails
@ -50,10 +50,10 @@ ERROR: non geometry column "Does not exists"
## Target conflict is handled nicely: 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
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 TABLE
## Registering tables does not work without permissions