使用sql无法正常复制postgres上的架构

时间:2015-02-02 15:22:00

标签: sql postgresql multi-tenant

我在postgres上有多租户应用程序,对于创建帐户,我复制了一个架构"模板"并为帐户数据重命名。

我使用" sql" var to copy schema" _template"。 当我执行它时,一切正常,但没有替换具有DEFAULT属性的列。 但是当在pgAdmin中直接执行sql时工作正常。

实施例:     " id bigint NOT NULL DEFAULT nextval(' _template.table_id_seq ' :: regclass)"     这个_template.table_id_seq ...应该是 [schemaname] .table_id_seq

String sql = new String(
"DO " +
"$do$ " +
"DECLARE " +
"   objeto text; " +
"   buffer text; " +
"   source_schema text; " +
"   dest_schema text; " +
"   default_ text; " +
"   column_ text; " +
"BEGIN " +
" " +
"   source_schema := '_template'; " +
"   dest_schema   := '" + schema + "'; " +
" " +
"EXECUTE 'CREATE SCHEMA ' || dest_schema ; " +
" " +
"   FOR objeto IN " +
"       SELECT sequence_name::text FROM information_schema.SEQUENCES WHERE sequence_schema = source_schema " +
"   LOOP " +
"EXECUTE 'CREATE SEQUENCE ' || dest_schema || '.' || objeto; " +
"   END LOOP; " +
" " +
" " +
"   FOR objeto IN " +
"       SELECT table_name::text FROM information_schema.TABLES WHERE table_schema = source_schema " +
"   LOOP         " +
"       buffer := dest_schema || '.' || objeto; " +
"       EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || objeto || ' INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING DEFAULTS)'; " +
"FOR column_, default_ IN " +
"           SELECT column_name::text, REPLACE( column_default::text, source_schema, dest_schema ) FROM " +
"               information_schema.COLUMNS WHERE table_schema = source_schema AND table_name = objeto AND column_default LIKE 'nextval(%' || source_schema || '%)' " +
"       LOOP " +
"EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || column_ || ' SET DEFAULT ' || default_; " +
"       END LOOP; " +
" " +
"   END LOOP; " +
"END " +
"$do$    " 
);

这里是一个sql var输出的例子

DO $do$ 
DECLARE     
    objeto text;    
    buffer text;    
    source_schema text;     
    dest_schema text;   
    default_ text;  
    column_ text; 
BEGIN  
    source_schema := '_template';   
    dest_schema   := 'schemacopied';  

    EXECUTE 'CREATE SCHEMA ' || dest_schema ;   

    FOR objeto IN       
        SELECT sequence_name::text FROM information_schema.SEQUENCES WHERE sequence_schema = source_schema 

    LOOP 
        EXECUTE 'CREATE SEQUENCE ' || dest_schema || '.' || objeto;     
    END LOOP;       

    FOR objeto IN       
        SELECT table_name::text FROM information_schema.TABLES WHERE table_schema = source_schema   
    LOOP                
        buffer := dest_schema || '.' || objeto;         
        EXECUTE 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || objeto || ' INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING DEFAULTS)'; 

        FOR column_, default_ IN            
            SELECT column_name::text, REPLACE( column_default::text, source_schema, dest_schema ) FROM 
            information_schema.COLUMNS WHERE table_schema = dest_schema AND table_name = objeto AND column_default LIKE 'nextval(%' || source_schema || '%)'        

        LOOP 
            EXECUTE 'ALTER TABLE ' || buffer || ' ALTER COLUMN ' || column_ || ' SET DEFAULT ' || default_;         
        END LOOP;   
    END LOOP; 
END $do$

1 个答案:

答案 0 :(得分:0)

动态生成nextval(...)中引用的information_schema.COLUMNS.column_default个表达式,以包含或排除模式名称,具体取决于当前search_path

如果序列的模式不在search path中,则模式显示为序列名称的前缀,否则不显示。

因此该条款的结果为真或假:

column_default LIKE 'nextval(%' || source_schema || '%)'

真的取决于search_path,这可以解释为什么它在某些情况下(你在pgAdmin中测试)而不在其他情况下工作。

要不依赖它,您可以在阻止期间强制search_path,如:

DO $$
BEGIN
  SET LOCAL search_path TO public;
  ...
  ...
END
$$ LANGUAGE plpgsql;

或许您可以将search_path设置为源架构,在这种情况下,这些与架构相关的default子句根本不需要任何后处理。