将 liquibase-changeSet 标记为已运行,如果上下文不匹配

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

Mark liquibase-changeSet as run if context doesn't match

问题

I manage my database schema with liquibase and I also want to manage demo-data with liquibase. And now I'm looking for a good strategy for having both together.

我的数据库架构是由liquibase管理的,我也希望使用liquibase来管理演示数据。现在我正在寻找一个能够同时管理两者的良好策略。

My plan is to add a "demo-data"-context to changesets that should insert demo-data. This works. They are ignored (as expected) if I run it with another context.

我的计划是将“demo-data”上下文添加到应该插入演示数据的变更集中。这可以正常工作。如果我使用另一个上下文运行它,它们将被忽略(如预期的那样)。

But what I would like to have is, that they are also marked as run instead of just ignored. In general this is possible with preconditions. But I didn't find a way to make preconditions depending on the context or label.

但我希望它们被标记为已运行,而不仅仅是被忽略。通常情况下,这可以通过前提条件实现。但我没有找到根据上下文或标签设置前提条件的方法。

Do you have an idea how to do that?

您有没有想法如何做到这一点?

Example-Changeset:

示例变更集:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
    <changeSet  author="me"  id="1">
        <createTable tableName="users">
            <column  name="id"  type="Integer">
                <constraints  nullable="false"  primaryKey="true"  unique="true"/>
            </column>
            <column  name="firstname"  type="VARCHAR(255)">
                <constraints nullable="false" />
            </column>
            <column  name="lastname"  type="VARCHAR(255)">
                <constraints nullable="false" />
            </column>
        </createTable>
    </changeSet>

	<changeSet  author="me"  id="2" context="demo-data">  
       <preConditions  onFail="MARK_RAN">
			context = demo-data
        </preConditions>

		<insert  tableName="users">  
			<column  name="id"  value="1"/>  
			<column name="firstname" value="Phil"/>
			<column name="lastname" value="Harmony"/>
		</insert>  
	</changeSet>
</databaseChangeLog>

If I run the following two commands I don't want to have demo-data in the database because it is possible, that the database-schema is changed between these two runs. And I also want to avoid to add old demo-data by accident at any point

如果我运行以下两个命令,我不希望在数据库中有演示数据,因为在这两次运行之间可能会更改数据库架构。而且我也希望在任何时候都不会意外添加旧的演示数据。

liquibase update --contexts="any-non-existing-context" // Workaround for "everything except demo-data"
liquibase update --contexts="demo-data"
英文:

I manage my database schema with liquibase and I also want to manage demo-data with liquibase. And now I'm looking for a good strategy for having both together.

My plan is to add a "demo-data"-context to changesets that should insert demo-data. This works. They are ignored (as expected) if I run it with another context.

But what I would like to have is, that they are also marked as run instead of just ignored. In general this is possible with preconditions. But I didn't find a way to make preconditions depending on the context or label.

Do you have an idea how to do that?

Example-Changeset:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
    <changeSet  author="me"  id="1">
        <createTable tableName="users">
            <column  name="id"  type="Integer">
                <constraints  nullable="false"  primaryKey="true"  unique="true"/>
            </column>
            <column  name="firstname"  type="VARCHAR(255)">
                <constraints nullable="false" />
            </column>
            <column  name="lastname"  type="VARCHAR(255)">
                <constraints nullable="false" />
            </column>
        </createTable>
    </changeSet>

	<changeSet  author="me"  id="2" context="demo-data">  
       <preConditions  onFail="MARK_RAN">
			context = demo-data
        </preConditions>

		<insert  tableName="users">  
			<column  name="id"  value="1"/>  
			<column name="firstname" value="Phil"/>
			<column name="lastname" value="Harmony"/>
		</insert>  
	</changeSet>
</databaseChangeLog>

If I run the following two commands I don't want to have demo-data in the database because it is possible, that the database-schema is changed between these two runs. And I also want to avoid to add old demo-data by accident at any point

liquibase update --contexts="any-non-existing-context" // Workaround for "everything except demo-data"
liquibase update --contexts="demo-data"

答案1

得分: 0

True,contex不在支持的前提条件列表中,但是您可以编写自己的自定义前提条件,该前提条件将对名为context的参数执行检查,并提供一个值:

<preConditions>
    <customPrecondition className="com.example.ContextPrecondition">
        <param name="context" value="demo-data"/>
    </customPrecondition>
</preConditions>

请查看Liquibase文档中的示例,了解如何实现自定义前提条件的详细信息 https://contribute.liquibase.com/extensions-integrations/extension-guides/add-a-precondition/

英文:

True, contex is not in the list of supported preconditions, but you can write your own custom precondition that would execute a check on parameter named context with a provided value:

<preConditions>
    <customPrecondition className="com.example.ContextPrecondition">
        <param name="context" value="demo-data"/>
    </customPrecondition>
</preConditions>

Take a look at this example from Liquibase docs on how to implement a custom precondition https://contribute.liquibase.com/extensions-integrations/extension-guides/add-a-precondition/

huangapple
  • 本文由 发表于 2023年5月11日 16:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225739.html
匿名

发表评论

匿名网友

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

确定