Skip to content

Commit 050df0d

Browse files
fix confusion between table and schema name (#390)
1 parent 1702edb commit 050df0d

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

migration/migrate_parameters_from_study.sql

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $body$
2222
<<fn>>
2323
DECLARE
2424
schema_prefix constant text := '';
25-
study_name text := 'study';
25+
study_schema constant text := quote_ident(concat(fn.schema_prefix, 'study'));
2626
migrate constant jsonb[] := array[
2727
/* Config format:
2828
* - from_table (string): source table name
@@ -61,33 +61,31 @@ DECLARE
6161
err_pg_exception_context text;
6262
BEGIN
6363
--lock table study in exclusive mode;
64-
raise log 'Script % starting at %', 'v1.7', now();
64+
raise log 'Script % starting at %', 'v1.8.1', now();
6565
raise log 'user=%, db=%, schema=%', current_user, current_database(), current_schema();
6666

67-
study_name := quote_ident(concat(fn.schema_prefix, fn.study_name));
68-
6967
/* Case OPF: copy from db.study.<table> to db.<service>.<table> (easy, no problem to migrate)
7068
* Case localdev & Azure: copy from study.public.<table> to <service>.public.<table> (need to copy between databases...)
7169
* [0A000] cross-database references are not implemented: "ref.to.remote.table"
7270
*/
73-
if exists(SELECT nspname FROM pg_catalog.pg_namespace where nspname = fn.study_name) then
71+
if exists(SELECT nspname FROM pg_catalog.pg_namespace where nspname = fn.study_schema) then
7472
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';
7775
end if;
7876
remote_databases := false;
79-
elsif exists(SELECT datname FROM pg_catalog.pg_database WHERE datistemplate = false and datname = fn.study_name) then
77+
elsif exists(SELECT datname FROM pg_catalog.pg_database WHERE datistemplate = false and datname = fn.study_schema) then
8078
raise notice 'separate databases structure detected';
81-
if current_database() != study_name then
82-
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';
8381
end if;
8482
remote_databases := true;
8583
create extension if not exists postgres_fdw;
8684
else
8785
raise exception 'Can''t detect type of database' using
8886
hint='Is it the good database?',
8987
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.';
9189
end if;
9290

9391
foreach params in array migrate loop
@@ -122,7 +120,7 @@ BEGIN
122120
into insert_columns; --[...] ; there isn't oid cast for foreign tables
123121
--the create&import commands don't raise an exception if something isn't right, so insert_column result can be null
124122
else
125-
path_from := concat(study_name, '.', quote_ident(additional_table->>'from_table'));
123+
path_from := concat(study_schema, '.', quote_ident(additional_table->>'from_table'));
126124
path_to := concat(quote_ident(to_schema_full), '.', quote_ident(additional_table->>'to_table'));
127125
execute format('select string_agg(attname, '','') from pg_attribute where attrelid = %L::regclass and attnum >=1', path_to)
128126
into insert_columns; --columns order may be different between src & dst tables
@@ -131,7 +129,7 @@ BEGIN
131129
if insert_columns is null then
132130
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';
133131
end if;
134-
raise debug 'table locations: study=% src="%" dst="%"', study_name, path_from, path_to;
132+
raise debug 'table locations: study=% src="%" dst="%"', study_schema||'.study', path_from, path_to;
135133

136134
raise notice 'copy data from % to %', path_from, path_to;
137135
execute format('insert into %s(%s) select %s from %s', path_to, insert_columns, insert_columns, path_from); --... on conflict do nothing/update
@@ -140,9 +138,9 @@ BEGIN
140138

141139
if additional_first then
142140
additional_first := false;
143-
execute format('update %s set %I=%I, %I=null', study_name, params->>'from_new_uuid', params->>'from_old_id', params->>'from_old_id');
141+
execute format('update %s set %I=%I, %I=null', concat(fn.study_schema, '.', quote_ident('study')), params->>'from_new_uuid', params->>'from_old_id', params->>'from_old_id');
144142
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';
146144
end if;
147145

148146
if remote_databases then
@@ -173,7 +171,7 @@ BEGIN
173171
err_pg_exception_detail = pg_exception_detail,
174172
err_pg_exception_hint = pg_exception_hint,
175173
err_pg_exception_context = pg_exception_context;
176-
raise warning using
174+
raise exception using
177175
message = err_message_text,
178176
detail = err_pg_exception_detail,
179177
errcode = err_returned_sqlstate,

0 commit comments

Comments
 (0)