You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* Case OPF: copy from db.study.<table> to db.<service>.<table> (easy, no problem to migrate)
70
68
* Case localdev & Azure: copy from study.public.<table> to <service>.public.<table> (need to copy between databases...)
71
69
* [0A000] cross-database references are not implemented: "ref.to.remote.table"
72
70
*/
73
-
if exists(SELECT nspname FROMpg_catalog.pg_namespacewhere nspname =fn.study_name) then
71
+
if exists(SELECT nspname FROMpg_catalog.pg_namespacewhere nspname =fn.study_schema) then
74
72
raise notice 'multi schemas structure detected';
75
-
if current_schema() !=study_name then
76
-
raise exception 'Invalid current schema "%"', current_schema() using hint='Assuming script launch at "'||study_name||'.*"', errcode='invalid_schema_name';
73
+
if current_schema() !=study_schema then
74
+
raise exception 'Invalid current schema "%"', current_schema() using hint='Assuming script launch at "'||study_schema||'.*"', errcode='invalid_schema_name';
77
75
end if;
78
76
remote_databases := false;
79
-
elsif exists(SELECT datname FROMpg_catalog.pg_databaseWHERE datistemplate = false and datname =fn.study_name) then
77
+
elsif exists(SELECT datname FROMpg_catalog.pg_databaseWHERE datistemplate = false and datname =fn.study_schema) then
raise exception 'Invalid current database "%"', current_database() using hint='Assuming script launch at "'||study_name||'.*.*"', errcode='invalid_database_definition';
79
+
if current_database() !=study_schema then
80
+
raise exception 'Invalid current database "%"', current_database() using hint='Assuming script launch at "'||study_schema||'.*.*"', errcode='invalid_database_definition';
83
81
end if;
84
82
remote_databases := true;
85
83
create extension if not exists postgres_fdw;
86
84
else
87
85
raise exception 'Can''t detect type of database' using
88
86
hint='Is it the good database?',
89
87
errcode='invalid_database_definition',
90
-
detail='Can''t find schema nor database "study" from current session, use to determine the database structure.';
88
+
detail='Can''t find schema nor database "'||study_schema||'" from current session, use to determine the database structure.';
91
89
end if;
92
90
93
91
foreach params in array migrate loop
@@ -122,7 +120,7 @@ BEGIN
122
120
into insert_columns; --[...] ; there isn't oid cast for foreign tables
123
121
--the create&import commands don't raise an exception if something isn't right, so insert_column result can be null
execute format('select string_agg(attname, '','') from pg_attribute where attrelid = %L::regclass and attnum >=1', path_to)
128
126
into insert_columns; --columns order may be different between src & dst tables
@@ -131,7 +129,7 @@ BEGIN
131
129
if insert_columns is null then
132
130
raise exception 'A silent problem seem to happen during the connection to the remote database' using errcode ='fdw_error', hint='Check if the server is created and the table imported';
get current diagnostics rows_affected = row_count;
145
-
raise info 'Moved % IDs in % from % to %', rows_affected, study_name, params->>'from_old_id', params->>'from_new_uuid';
143
+
raise info 'Moved % IDs in % from % to %', rows_affected, concat(fn.study_schema, '.', quote_ident('study')), params->>'from_old_id', params->>'from_new_uuid';
0 commit comments