我正在寻找一种采用 PostgreSQL 表并以 wiki 格式(最好是 Confluence)输出数据字典的工具。似乎大多数工具都需要大量手动工作/多个工具来完成这项任务(IE> SchemaSpy、DB Visual Architect、Confluence 插件来获取输出的 HTML DD 并转换为 Confluence)。我正在寻找一个工具,它可以扫描我的 Postgres 表并输出一个 wiki 友好的数据字典,该字典将允许在数据库更改时进行无缝维护,而无需在另一个工具中更新我的数据库和数据库架构。
2 回答
Bob Swift 的Confluence SQL Plugin允许您在 Confluence 页面中显示源自 SQL 查询的数据.. 例如作为表格.. 也许值得一看?
目前支持Confluence 版本3.1.x - 4.9.x ...
该插件是免费的,可以从 Atlassian 的 Plugin Exchange 下载:https ://plugins.atlassian.com/plugins/org.swift.confluence.sql
可以在此处找到有关该插件的其他信息: https ://studio.plugins.atlassian.com/wiki/display/SQL/Confluence+SQL+Plugin
我认为您必须自己编写脚本,但这非常简单有趣。我将在这里假设 Python。
我喜欢 Confluence XML-RPC 接口。为此,请参阅http://goo.gl/KCt3z。您关心的远程方法可能是 login、getPage、setPage 和/或 updatePage。这个骨架看起来像:
import xmlrpclib
server = xmlrpclib.Server(opts.url)
conn = server.confluence1
token = conn.login(opts.username, opts.password)
page = conn.getPage(token,'PageSpace',page_title)
page = page + table
page = conn.updatePage(token,page,update_options)
table
这是来自 PG 表的数据。我们将在下面构建它。
为了从 PostgreSQL 中提取简单数据,我最常使用 psycopg2(也可以考虑 SQLSoup)。无论您如何获取数据,您最终都会得到一个行列表作为字典。数据库部分可能如下所示:
import psycopg2, psycopg2.extras
conn = psycopg2.connect("dbname=reece")
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute('SELECT * FROM sometable')
rows = cur.fetchall()
现在您需要格式化数据。对于简单的东西, print= 语句将起作用。对于更复杂的格式,可以考虑像 jinja2 (http://jinja.pocoo.org/) 这样的模板引擎。渲染代码可能如下所示:
from jinja2 import Template
template = Template(open(template_path).read())
table = template.render( rows = rows )
文件 template_path 将包含格式模板,可能如下所示:
<table>
<tr>
<th>col header 1</th>
<th>col header 2</th>
</tr>
{% for row in rows|sort -%}
<tr>
<td>{{row.col1}}</td>
<td>{{row.col2}}</td>
</tr>
{% endfor %}
</table>
注意:Confluence 默认不再使用 wiki 标记。您应该编写 HTML。
最后,如果要为所有表制作一个页面,可以查看information_schema,其中包含有关数据库的信息作为表。例如:
select table_name from information_schema.tables where table_schema = current_schema();