Refactor: extract __CDB_FS_Import_If_Not_Exists

This commit is contained in:
Rafa de la Torre 2019-11-08 20:16:26 +01:00
parent 511e24a40e
commit 6700d24232

View File

@ -3,19 +3,14 @@
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- --
-- Get the version of a remote PG server -- Import a foreign table if it does not exist
-- --
CREATE OR REPLACE FUNCTION @extschema@.__CDB_FS_Foreign_Server_Version_PG(server_internal name) CREATE OR REPLACE FUNCTION @extschema@.__CDB_FS_Import_If_Not_Exists(server_internal name, remote_schema name, remote_table name)
RETURNS text RETURNS void
AS $$ AS $$
DECLARE DECLARE
-- Import pg_settings from pg_catalog
remote_schema name := 'pg_catalog';
remote_table name := 'pg_settings';
local_schema name := @extschema@.__CDB_FS_Create_Schema(server_internal, remote_schema); local_schema name := @extschema@.__CDB_FS_Create_Schema(server_internal, remote_schema);
remote_server_version text;
BEGIN BEGIN
-- Import the foreign pg_settings table
IF NOT EXISTS ( IF NOT EXISTS (
SELECT * FROM pg_class SELECT * FROM pg_class
WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = local_schema) WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = local_schema)
@ -24,6 +19,23 @@ 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);
END IF; END IF;
END
$$
LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;
--
-- Get the version of a remote PG server
--
CREATE OR REPLACE FUNCTION @extschema@.__CDB_FS_Foreign_Server_Version_PG(server_internal name)
RETURNS text
AS $$
DECLARE
remote_schema name := 'pg_catalog';
remote_table name := 'pg_settings';
local_schema name := @extschema@.__CDB_FS_Create_Schema(server_internal, remote_schema);
remote_server_version text;
BEGIN
PERFORM @extschema@.__CDB_FS_Import_If_Not_Exists(server_internal, remote_schema, remote_table);
BEGIN BEGIN
EXECUTE format(' EXECUTE format('
@ -47,21 +59,12 @@ CREATE OR REPLACE FUNCTION @extschema@.__CDB_FS_Foreign_PostGIS_Version_PG(serve
RETURNS text RETURNS text
AS $$ AS $$
DECLARE DECLARE
-- Import pg_settings from pg_catalog
remote_schema name := 'pg_catalog'; remote_schema name := 'pg_catalog';
remote_table name := 'pg_extension'; remote_table name := 'pg_extension';
local_schema name := @extschema@.__CDB_FS_Create_Schema(server_internal, remote_schema); local_schema name := @extschema@.__CDB_FS_Create_Schema(server_internal, remote_schema);
remote_postgis_version text; remote_postgis_version text;
BEGIN BEGIN
-- Import the foreign pg_extension table PERFORM @extschema@.__CDB_FS_Import_If_Not_Exists(server_internal, remote_schema, remote_table);
IF NOT EXISTS (
SELECT * FROM pg_class
WHERE relnamespace = (SELECT oid FROM pg_namespace WHERE nspname = local_schema)
AND relname = remote_table
) THEN
EXECUTE format('IMPORT FOREIGN SCHEMA %I LIMIT TO (%I) FROM SERVER %I INTO %I',
remote_schema, remote_table, server_internal, local_schema);
END IF;
BEGIN BEGIN
EXECUTE format(' EXECUTE format('