postgresql 运行相互依赖的sql脚本

bxgwgixi  于 2023-02-08  发布在  PostgreSQL
关注(0)|答案(1)|浏览(211)

我有一个在后端使用postgres数据库的项目,我正在创建一个Dockerfile,它可以自动设置数据库的本地开发示例。数据库包括大量函数,这些函数过去一直存储在上下文适当的sql文件中,例如users.sql、companies.sql。这一直很好,因为无论何时进行更改,我可以简单地执行相关的sql文件,其中REPLACE所有视图和删除/重新创建所有函数。
然而,当尝试在一个新的postgres示例上运行这些脚本时(在CREATE ing所有表之后),它们会失败,因为大多数视图/函数引用了其他尚未定义的视图函数。
我已经开始调查是否有一个特定的顺序,我可以运行脚本,以避免这个问题,但由于他们没有设计的目的,这可能是不可能的,并有大量的实体在发挥作用,所以这很可能是一个不平凡的任务。
除了重组脚本之外,还有什么方法可以实现这一点?

blmhpbnm

blmhpbnm1#

您可以编写一个简单的递归查询,从所有不依赖于其他视图的视图开始,递归地添加依赖于这些视图的视图。然后按正确的顺序输出这些视图的视图定义,您就得到了脚本:

WITH RECURSIVE viewids AS (
   /* all views that don't depend on other views */
   SELECT t.oid, 1 as level
   FROM pg_class t
      JOIN pg_rewrite AS r ON r.ev_class = t.oid
   WHERE r.rulename = '_RETURN'
     AND t.relkind = 'v'
     AND t.relnamespace NOT IN ('pg_catalog'::regnamespace,
                                'information_schema'::regnamespace,
                                'pg_toast'::regnamespace)
     AND NOT EXISTS (
            /* depends on a view */
            SELECT 1
            FROM pg_depend AS d
               JOIN pg_class AS t2 ON d.refobjid = t2.oid
            WHERE d.objid = r.oid
              AND d.classid = 'pg_rewrite'::regclass
              AND d.refclassid = 'pg_class'::regclass
              AND d.deptype = 'n'
              AND d.refobjsubid <> 0
              AND t2.relkind = 'v'
         )
     AND NOT EXISTS (
            /* depends on an extension */
            SELECT 1
            FROM pg_depend
            WHERE objid = t.oid
              AND classid = 'pg_class'::regclass
              AND refclassid = 'pg_extension'::regclass
              AND deptype = 'e'
         )
UNION ALL
   /* all views that depend on these views */
   SELECT t.oid, viewids.level + 1
   FROM pg_class AS t
      JOIN pg_rewrite AS r ON r.ev_class = t.oid
      JOIN pg_depend AS d ON d.objid = r.oid
      JOIN viewids ON viewids.oid = d.refobjid
   WHERE t.relkind = 'v'
     AND r.rulename = '_RETURN'
     AND d.classid = 'pg_rewrite'::regclass                            
     AND d.refclassid = 'pg_class'::regclass
     AND d.deptype = 'n'
     AND d.refobjsubid <> 0
)
/* order the views by level, eliminating duplicates */
SELECT format('CREATE VIEW %s AS%s',
              oid::regclass,
              pg_get_viewdef(oid::regclass))
FROM viewids
GROUP BY oid
ORDER BY max(level);

相关问题