Liquibase 在不知道其名称的情况下掉落约束
我们使用 liquibase 来跟踪我们的数据库更改。 第一个changeSet包含这些行:
<column name="SHORT_ID" type="INTEGER">
<constraints unique="true" />
</column>
基本上这意味着SHORT_ID列具有唯一约束,但该约束的名称可以是任何名称,并且通常每次都不同(我们针对H2数据库运行一些集成测试,并且每次运行测试时都会创建新的基础)
所以..问题是:我无法更改第一个变更集,但现在我们必须摆脱这个唯一的约束。有什么想法如何通过使用 liquibase 来实现这一目标吗?
We use liquibase to keep track of our database changes..
First changeSet contains those lines:
<column name="SHORT_ID" type="INTEGER">
<constraints unique="true" />
</column>
Basically it means that SHORT_ID column has unique constraint but the name of this constraint can be whatever and usually is different each time (we run some integration tests against H2 databases and new bases are made each time we run tests)
So.. problem is: I can't change this first changeSet but now we have to get rid of this unique constraint. Any ideas how to achieve that by using liquibase?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Liquibase 提供了一种在不知道约束名称的情况下删除非空约束的实现。当这个问题被问到时它可能还不存在(我意识到它已经很老了)。
dropNotNullConstraint
A dropUniqueConstraint 存在,但您可能已经知道它,因为它需要约束名称。
Liquibase provides an implementation for dropping a not null constraint without knowing the constraint name. It may not have existed when this question was asked (I realise it's quite old).
dropNotNullConstraint
A dropUniqueConstraint exists but you probably already knew about it as it requires the constraint name.
H2 用于删除约束的 SQL 需要约束名称。我不记得 H2 中自动生成的约束名称是随机的还是跨数据库一致的。
如果它是恒定的,您可以使用普通的 liquibase 标签,它会正常工作。
如果它是随机的,您将必须从 information_schema 中获取约束名称。 H2 可能允许类似的操作:
如果没有,您可能需要创建自定义 liquibase 更改 (http://liquibase.org/extensions< /a> 为 2.0,http://www.liquibase.org/manual/custom_refactoring_class 为 1.9 ) 进行调用并放弃约束。
H2's SQL for dropping constraints requires a constraint name. I don't remember if the auto-generated constraint name in H2 is random or would be consistent across databases.
If it's constant, you can use the normal liquibase tag and it will work fine.
If it's random, you will have to get the constraint name from the information_schema. H2 may allow something like:
If not, you may need to create a custom liquibase change (http://liquibase.org/extensions with 2.0, http://www.liquibase.org/manual/custom_refactoring_class in 1.9) that makes the call and drops the constraint.
对于 HSQL,Nathan 建议的查询不起作用(更改表 TABLE_NAME 删除约束
(选择唯一索引名称
来自 information_schema.constraints
其中 table_name='TABLE_NAME' 和 column_name='SHORT_ID'))
这是因为,DDL 和 SQL 不能混合。
使用 HSQL 似乎不可能通过在运行时知道名称来删除约束(在删除带有约束的列之前,我需要在回滚中这样做)。对于 Oracle 和 MSSQL 来说这是可能的。
我所做的是通过 hbm 文件(休眠)获取生成的数据库。由于 hibernate 不支持命名唯一键约束,因此使用 liquibase(对于 HSQL)不可能删除这些带有约束的列。我们有基于 HSQL 的测试来测试整个内容。如果能找到 HSQL 的解决方案就好了。
现在,我只是硬编码唯一约束名称(解决方法)
For HSQL the query which Nathan suggested does not work (alter table TABLE_NAME drop constraint
(select unique_index_name
from information_schema.constraints
where table_name='TABLE_NAME' and column_name='SHORT_ID'))
This is because, DDLs and SQLs can not be mixed.
Dropping constraint by knowing the name at runtime does not seems to be possible with HSQL (I need this in rollbacks before dropping columns with constraints). This is possible for Oracle and MSSQL.
What I do is get a generated databse through hbm files (hibernate). As hibernate does not support naming unique key constraints, so for e.g. dropping these columns with constraints is a not possible using liquibase (for HSQL). We have tests based on HSQL testing the whole stuff. Would be good to get a solution for HSQL.
For now, I just hard code the Unique constraint names (a work around)