【问题标题】:Liquibase check table name length before executing changsetLiquibase 在执行变更集之前检查表名长度
【发布时间】:2017-02-23 18:10:42
【问题描述】:

我正在使用 Java 代码中的 Liquibase 更新方法将一些实体持久保存在 PostgreSQL 数据库中。

有没有办法在 Liquibase 在数据库中创建表和列名称之前检查它们的长度?诸如前置条件 SQL 检查或 Java 代码之类的东西?

【问题讨论】:

  • 您使用的是 XML 变更日志吗?使用 XML 上的 xpath 查询可能很容易做到这一点。
  • 是的,我正在使用 xml 你能解释更多吗?我不熟悉 xml xpath 查询

标签: java sql database liquibase


【解决方案1】:

我看到了两种可能的解决方案:

仅使用 Java

您可以使用 Liquibase 类加载更改日志,遍历所有 createTable 标记并检查表名和列名的长度。

代码的大致轮廓:

File changeLogFile = new File("...");
FileSystemResourceAccessor accessor = new FileSystemResourceAccessor(changeLogFile.getAbsoluteFile().getParent());

DatabaseConnection db = new JdbcConnection(...);
Liquibase lb = new Liquibase(changeLogFile.getAbsolutePath(), accessor, db);

DatabaseChangeLog changeLog = lb.getDatabaseChangeLog();

List<ChangeSet> changeSets = changeLog.getChangeSets();

for (ChangeSet cs : changeSets) {
  for (Change change : changeSet.getChanges()) {
      if (change instanceof CreateTableChange) {
          // check the table that is created
      }
  }
}

获取变更集列表的另一种方法是使用ChangeLogIteratorShouldRunChangeSetFilter。这样你就需要ChangeSetVisitor 来处理变更集。

使用 XSLT

以下 XSLT 提取所有长度超过 20 个字符的表名并将其写入 HTML 文件。

您可以调整它以过滤不同的长度:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns:lb="http://www.liquibase.org/xml/ns/dbchangelog">
  <xsl:output method="html"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>Tablenames</title>
      </head>
      <body>
        <table border="1" class="change-table">
          <tr>
            <td class="table-heading">ChangeSet</td>
            <td class="table-heading">Tablename</td>
            <td class="table-heading">Length of name</td>
          </tr>

          <xsl:apply-templates select="//lb:createTable"/>

        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="lb:createTable">
    <xsl:if test="string-length(@tableName) &gt; 20">
      <tr>
        <td class="table-row change-set"><xsl:value-of select="../@author"/><xsl:text>::</xsl:text><xsl:value-of select="../@id"/></td>
        <td class="table-row object-name"><xsl:value-of select="@tableName"/></td>
        <td class="table-row object-name"><xsl:value-of select="string-length(@tableName)"/></td>
      </tr>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 2019-09-12
    • 2020-07-14
    • 1970-01-01
    相关资源
    最近更新 更多