简体   繁体   English

使用 Aws SCT 工具将 Oracle 包转换为 PostgreSQL 的命名问题

[英]Naming issues on Oracle Packages conversion to PostgreSQL using Aws SCT tool

I had migrated Oracle db to Aurora postgreSQL with the help of AWS SCT tool.我在 AWS SCT 工具的帮助下将 Oracle db 迁移到了 Aurora postgreSQL。 All packages and triggers are converted as functions in PostgreSQL.所有包和触发器在 PostgreSQL 中都被转换为函数。 My issue here is all the names are converted with a $ (dollar) symbol.我的问题是所有名称都用$ (美元)符号转换。

for example, A package and associated stored proc in Oracle pk_audit.sp_get_audit converted to postgreSQL as pk_audit$sp_get_audit with a $ symbol.例如,Oracle pk_audit.sp_get_audit一个包和关联的存储pk_audit.sp_get_audit转换为 postgreSQL 为带有 $ 符号的pk_audit$sp_get_audit but, In the middleware db object name is pk_audit.sp_get_audit .但是,在中间件 db 对象名称是pk_audit.sp_get_audit In order to minimise the effort on the middleware, I need to convert all the functions from pk_audit$sp_get_audit to pk_audit.sp_get_audit .为了尽量减少中间件的工作量,我需要将所有函数从pk_audit$sp_get_auditpk_audit.sp_get_audit

I've more than 1500 functions converted with $ symbol.我已经用 $ 符号转换了 1500 多个函数。 Need a script to alter all the User Defined Functions names .需要一个脚本来改变所有用户定义的函数名称。 I've created a script to build the alter scripts.我创建了一个脚本来构建更改脚本。

`select a.alter_statement|| replace(a.rename_statement,'$','.')
From
(
SELECT format('ALTER %s %I.%I(%s)'
            , 'FUNCTION'
              ,CONCAT('"',n.nspname,'"') 
            , p.proname
            , pg_catalog.pg_get_function_identity_arguments(p.oid)
             ) AS alter_statement,
             format('RENAME %s %I.%I(%s);'
            , 'TO' 
            , CONCAT('"',n.nspname,'"') 
            , p.proname
            , pg_catalog.pg_get_function_identity_arguments(p.oid)
             ) AS rename_statement
FROM   pg_catalog.pg_proc p
JOIN   pg_catalog.pg_namespace n ON n.oid = p.pronamespace
and     n.nspname = 'my_schema' ORDER  BY 1
) a;`

But the result is throwing error.但结果是抛出错误。 Kindly help thanks请帮忙谢谢

Try this:尝试这个:

select a.alter_statement|| replace(a.rename_statement,'$','.')
From
(
SELECT format('ALTER FUNCTION %s.%s(%s) '
              ,CONCAT(n.nspname) 
              , p.proname
              , pg_catalog.pg_get_function_identity_arguments(p.oid)
             ) AS alter_statement,
             format('RENAME TO %I'
             ,p.proname
             ) AS rename_statement
FROM   pg_catalog.pg_proc p
JOIN   pg_catalog.pg_namespace n ON n.oid = p.pronamespace
and     n.nspname = 'newschema' ORDER  BY 1
) a;

Example:例子:

select a.alter_statement|| replace(a.rename_statement,'$','.')
From
(
SELECT format('ALTER FUNCTION %s.%s(%s) '
              ,CONCAT(n.nspname) 
              , p.proname
              , pg_catalog.pg_get_function_identity_arguments(p.oid)
             ) AS alter_statement,
             format('RENAME TO %I'
             ,p.proname
             ) AS rename_statement
FROM   pg_catalog.pg_proc p
JOIN   pg_catalog.pg_namespace n ON n.oid = p.pronamespace
and     n.nspname = 'newschema' ORDER  BY 1
) a;
                                    ?column?                                     
---------------------------------------------------------------------------------
 ALTER FUNCTION newschema.package$function(integer) RENAME TO "package.function"
(1 row)

This will not work.这是行不通的。 Even if your generated statement has a valid syntax it will fail, unless you have created a Postgres schema for each Oracle package.即使您生成的语句具有有效的语法,它也会失败,除非您为每个 Oracle 包创建了 Postgres 模式。 Oracle packages gather multiple procedures and into a single database object. Oracle 包将多个过程收集到一个数据库对象中。 Unfortunately Postgres has no such concept so each procedure within the package becomes an independent function is Postgres.不幸的是 Postgres 没有这样的概念,所以包内的每个程序成为一个独立的函数是 Postgres。 This results in a naming structure issue beyond the $ in the name.这会导致名称中 $ 之外的命名结构问题。 In Oracle the format reference to package_name.procedure_name says to run procedure name that is within the package name, the same line of code in Postgres is interpreted as schema_name.function_name .在 Oracle 中,对package_name.procedure_name的格式引用表示运行包名称内的过程名称,Postgres 中的同一行代码被解释为schema_name.function_name That is why the conversion routine replaces it with package_name$procedure_name which is still a valid Postgres within name the same schema.这就是为什么转换例程用package_name$procedure_name替换它的原因,它仍然是名称相同模式内的有效 Postgres。 (Subject to translated name length; what does the translation routine do when combined length of package_name + procedure_name + 1 is over the name length limit in Postgres (63)? ) That aspect may make it easier on an overall system effort to update the middleware. (取决于翻译的名称长度;当 package_name + procedure_name + 1 的组合长度超过 Postgres (63) 中的名称长度限制时,翻译例程会做什么?)这方面可能会使整个系统更容易更新中间件. And that is just the beginnings of your package conversion issues.这只是您的包转换问题的开始。 What about:关于什么:

  • Package level types, cursors, variables, collections, etc defined only in the package spec.仅在包规范中定义的包级别类型、游标、变量、集合等。
  • The same as above defined only in the package body but NOT within any procedure.与上述相同,仅在包体中定义,但不在任何过程中。
  • References to either of the above, from within the resulting Poatgres functions.从生成的 Poatgres 函数中引用上述任何一个。
  • Other functionality of packages in Oracle not directly translatable to Postgres. Oracle 中包的其他功能不能直接转换为 Postgres。 All of these must be looked at and may perhaps require modifications to either or both of the Postgres functions and your middleware.所有这些都必须查看,并且可能需要对 Postgres 函数和中间件中的一个或两个进行修改。 So basically running a conversion script is just the first step in a process of many steps for your conversion.因此,基本上运行转换脚本只是转换过程的许多步骤中的第一步。

    Good Luck!祝你好运!
    NOTE: Reference in above to procedure refers to both procedures and functions.注意:上面提到的过程是指过程和功能。

The easiest solution is to use the \\gexec feature of psql :最简单的解决方案是使用psql\\gexec功能:

SELECT format(
          'ALTER FUNCTION %s RENAME TO %I',
          oid::regprocedure,
          replace(proname, '$', '.')
       )
FROM pg_proc
WHERE pronamespace::regnamespace::text = 'my_schema' \gexec

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM