Liquibase 添加一个新列并用另一个现有列的值填充新列,仅当该列不存在时才执行操作
Liquibase add a new column and populates the new one with the value of another existing column, operation to be done only if the column does not exist
在同一个 table 中,我必须将列的值迁移到新列;
我想创建一个新专栏,
用另一列的值填充它,然后
删除已迁移的旧列,
只有在尚未创建新列时才会发生这种情况。
<changeSet author="xxxx" id="00021">
<preConditions onFail="MARK_RAN">
<not>
<columnExists schemaName="schema" tableName="talbee" columnName="new_table"/>
</not>
</preConditions>
<addColumn schemaName="schema" tableName="new_table">
<column name="new_column" type="text"/>
</addColumn>
<sql>UPDATE table_name SET new_column = old_column</sql>
<dropColumn
catalogName="cat"
columnName="old_column"
schemaName="schema"
tableName="table_name">
<column name="old_column"/>
</dropColumn>
</changeSet>
我删除旧列的方式是否正确?
重命名列是否更好?
是的,重命名现有列更合理。您可以使用 <columnExists>
前提条件,然后 <renameColumn>
更改。
<changeSet author="xxxx" id="00021">
<preConditions onFail="MARK_RAN">
<tableExists tableName="table_name"/>
<columnExists tableName="table_name" columnName="old_column"/>
<not>
<columnExists tableName="table_name" columnName="new_column"/>
</not>
</preConditions>
<renameColumn tableName="table_name" oldColumnName="old_column" newColumnName="new_column"
columnDataType="text"/>
</changeSet>
在同一个 table 中,我必须将列的值迁移到新列; 我想创建一个新专栏, 用另一列的值填充它,然后 删除已迁移的旧列, 只有在尚未创建新列时才会发生这种情况。
<changeSet author="xxxx" id="00021">
<preConditions onFail="MARK_RAN">
<not>
<columnExists schemaName="schema" tableName="talbee" columnName="new_table"/>
</not>
</preConditions>
<addColumn schemaName="schema" tableName="new_table">
<column name="new_column" type="text"/>
</addColumn>
<sql>UPDATE table_name SET new_column = old_column</sql>
<dropColumn
catalogName="cat"
columnName="old_column"
schemaName="schema"
tableName="table_name">
<column name="old_column"/>
</dropColumn>
</changeSet>
我删除旧列的方式是否正确?
重命名列是否更好?
是的,重命名现有列更合理。您可以使用 <columnExists>
前提条件,然后 <renameColumn>
更改。
<changeSet author="xxxx" id="00021">
<preConditions onFail="MARK_RAN">
<tableExists tableName="table_name"/>
<columnExists tableName="table_name" columnName="old_column"/>
<not>
<columnExists tableName="table_name" columnName="new_column"/>
</not>
</preConditions>
<renameColumn tableName="table_name" oldColumnName="old_column" newColumnName="new_column"
columnDataType="text"/>
</changeSet>