Liquibase – 如何使用SQL格式运行Java代码变更集?

huangapple go评论85阅读模式
英文:

Liquibase - How to run a Java code changeset using SQL Format?

问题

使用Liquibase的SQL格式,您可以触发自定义更改的方式是:

英文:

With Liquibase XML you can trigger Java code to execute for a change with:

    <customChange class="my.java.Class">
        <param name="id" value="2" />
    </customChange>

How can you trigger a custom change with Liquibase SQL Format?

答案1

得分: 1

为什么您不使用 sqlFile 标签来执行自定义 SQL 脚本呢?
类似于这样:

<changeSet author="me" id="1">
    <sqlFile path="my_script.sql"/>
</changeSet>

甚至可以像这样:

<changeSet author="me" id="2">
    <sql>
        INSERT INTO my_table (columnAA, columnBB) VALUES ('valueAA', 'valueBB');
    </sql>
</changeSet>

更新:
好的,抱歉,我认为您可以使用 <change> 标签并将 customChangeClass 属性设置为您自定义 Java 类的完全限定名称,不要忘记在 Liquibase 的类路径中包含您自定义更改所需的 Java 类和依赖项!

<changeSet author="yourname" id="yourid">
    <comment>Custom change using SQL format</comment>
    <change customChangeClass="com.example.MyCustomChange">
        <param name="id" value="2"/>
    </change>
</changeSet>
英文:

why you don't use sqlFile tag to execute custom SQL scripts?
something like this :

&lt;changeSet author=&quot;me&quot; id=&quot;1&quot;&gt;
    &lt;sqlFile path=&quot;my_script.sql&quot;/&gt;
&lt;/changeSet&gt;

or even something like this :

&lt;changeSet author=&quot;me&quot; id=&quot;2&quot;&gt;
    &lt;sql&gt;
        INSERT INTO my_table (columnAA, columnBB) VALUES (&#39;valueAA&#39;, &#39;valueBB&#39;);
    &lt;/sql&gt;
&lt;/changeSet&gt;

update:<br>
ok sorry, I think you can use the `` tag and set the customChangeClass attribute to the fully qualified name of your custom Java class, and don't forget you will need to include the necessary Java classes and dependencies for your custom change in your Liquibase classpath!

&lt;changeSet author=&quot;yourname&quot; id=&quot;yourid&quot;&gt;
    &lt;comment&gt;Custom change using SQL format&lt;/comment&gt;
    &lt;change customChangeClass=&quot;com.example.MyCustomChange&quot;&gt;
        &lt;param name=&quot;id&quot; value=&quot;2&quot;/&gt;
    &lt;/change&gt;
&lt;/changeSet&gt;

答案2

得分: 1

TL;DR 你无法这样做。

定义自定义变更集的支持格式包括:

  1. XML
<changeSet id="21" author="nvoxland">
  <customChange class="liquibase.change.custom.ExampleCustomTaskChange">
    <param name="helloTo" value="world"/>
  </customChange>
</changeSet>
  1. YAML
- changeSet:
     id:  21
     author: nvoxland
     changes:
     - customChange: {
"class": "liquibase.change.custom.ExampleCustomTaskChange",
"helloTo": "world" }
  1. JSON
{
  "changeSet": {
    "id": "21",
    "author": "nvoxland",
    "changes": [
      {
        "customChange": {
          "class": "liquibase.change.custom.ExampleCustomTaskChange",
          "helloTo": "world"
        }
      }
    ]
  }
}

基于 https://docs.liquibase.com/change-types/custom-change.html

英文:

TL;DR You can't.

The supported formats for defining a custom change set are:

  1. XML
&lt;changeSet id=&quot;21&quot; author=&quot;nvoxland&quot;&gt;
  &lt;customChange class=&quot;liquibase.change.custom.ExampleCustomTaskChange&quot;&gt;
    &lt;param name=&quot;helloTo&quot; value=&quot;world&quot;/&gt;
  &lt;/customChange&gt;
&lt;/changeSet&gt;
  1. YAML
-  changeSet:
     id:  21
     author: nvoxland
     changes:
     -  customChange:  {
&quot;class&quot;: &quot;liquibase.change.custom.ExampleCustomTaskChange&quot;,
&quot;helloTo&quot;: &quot;world&quot; }
  1. JSON
{
  &quot;changeSet&quot;: {
    &quot;id&quot;: &quot;21&quot;,
    &quot;author&quot;: &quot;nvoxland&quot;,
    &quot;changes&quot;: [
      {
        &quot;customChange&quot;: {
          &quot;class&quot;: &quot;liquibase.change.custom.ExampleCustomTaskChange&quot;,
          &quot;helloTo&quot;: &quot;world&quot;
        }
      }
    ]
  }
}

Based on https://docs.liquibase.com/change-types/custom-change.html

答案3

得分: 0

如果您正在寻找SQL变更集,请查看以下链接:https://docs.liquibase.com/change-types/sql-file.html

使用SQL格式的XML变更集如下所示:

<changeSet author="liquibase-docs" id="sqlFile-example">
    <sqlFile 
        path="my/path/file.sql"
        relativeToChangelogFile="true"
        splitStatements="true"
        stripComments="true"/>
</changeSet>

路径从资源文件开始,这意味着顶级变更集中的“my”是资源文件夹下的一个文件夹。


您还可以使用Java自定义变更中的database.getConnection()来构建一个JDBCTemplate对象,当您具有动态数据需要更新数据库时,这将非常方便。

public class MyCustomDbChange implements CustomTaskChange {
    @Override
    public void execute(Database database) {
        JdbcConnection jdbcConnection = (JdbcConnection) database.getConnection();
        Connection connection = jdbcConnection.getWrappedConnection();
        SingleConnectionDataSource dataSource = new SingleConnectionDataSource(connection, false);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        //然后您可以使用JDBCTemplate方法来执行查询

        // 示例选择
        List<Map<String, Object>> map = jdbcTemplate.queryForList("select * from user");

        // 示例单个更新
        jdbcTemplate.update("update user set name='Test' where id='1'");

        // 示例使用预处理语句查询的批量更新
        jdbcTemplate.batchUpdate(
            "INSERT INTO user (id, name) VALUES (?, ?)",
            new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int idx) throws SQLException {
                    String id = UUID.randomUUID().toString();

                    ps.setString(1, id);
                    ps.setString(2, "dummy name " + id);
                }

                @Override
                public int getBatchSize() {
                    return 100;
                }
            }
        );

        // .... 其他重写的方法
    }
}
英文:

If you are seeking the SQL changset you can check following link https://docs.liquibase.com/change-types/sql-file.html

Changset for xml format using SQL Fromat would as follows:

&lt;changeSet author=&quot;liquibase-docs&quot; id=&quot;sqlFile-example&quot;&gt;
     &lt;sqlFile 
        path=&quot;my/path/file.sql&quot;
        relativeToChangelogFile=&quot;true&quot;
        splitStatements=&quot;true&quot;
        stripComments=&quot;true&quot;/&gt;
&lt;/changeSet&gt;

the path satrts from the resources file so, that means that my in the top changset is a folder under resources folder


You can also construct a JDBCTemplate object using the database.getConnection() in the Java custom change this is handy when you have dynamic data, you want to update DB with

public class MyCustomDbChange implements CustomTaskChange {
    @Override
    public void execute(Database database) {
        JdbcConnection jdbcConnection = (JdbcConnection) database.getConnection();
        Connection connection = jdbcConnection.getWrappedConnection();
        SingleConnectionDataSource dataSource = new SingleConnectionDataSource(connection, false);
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        //Then you can use JDBCTemplate methods to execute queries
       
        // Example select
        List&lt;Map&lt;String, Object&gt;&gt; map = jdbcTemplate.queryForList(&quot;select * from user&quot;);

        // Example single update
        jdbcTemplate.update(&quot;update user set name=&#39;Test&#39; where id=&#39;1&#39;&quot;);

        // Example batch update using prepared stmt query
        jdbcTemplate.batchUpdate(
            &quot;INSERT INTO user (id, name) VALUES (?, ?)&quot;,
            new BatchPreparedStatementSetter() {
                @Override
                public void setValues(PreparedStatement ps, int idx) throws SQLException {
                    String id = UUID.randomUUID().toString();
                  
                    ps.setString(1, id);
                    ps.setString(2, &quot;dummy name &quot; + id);
                }

                @Override
                public int getBatchSize() {
                    return 100;
                }
            }
        );

       // .... other overridden methods
    }

huangapple
  • 本文由 发表于 2023年6月22日 16:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定