database 如何使用 Liquibase 在 Grails 中删除索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1685637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete an index in Grails with Liquibase
提问by nightingale2k1
I have a table generated by GORM (Grails Domain). It has foreign key / index that generated random characters like FKAC7AAF67162A158F. I need to remove that field that not needed anymore.
我有一个由 GORM(Grails 域)生成的表。它具有生成随机字符的外键/索引,如 FKAC7AAF67162A158F。我需要删除不再需要的字段。
The problems, I have some servers that need to be updated. So I need to create a migration using Liquibase. But I have no idea how to remove that index manualy if the index are in random name (each server my have different name).
问题,我有一些服务器需要更新。所以我需要使用 Liquibase 创建迁移。但是我不知道如果索引是随机名称(每个服务器都有不同的名称),我不知道如何手动删除该索引。
is it possible to drop an index of something without knowing its name ?
是否可以在不知道名称的情况下删除某个东西的索引?
采纳答案by Jay P.
You can remove the index using the database-migration plugin (liquibase). It requires that you know the index name, but that name should be in one of your previous migrations.
您可以使用数据库迁移插件 (liquibase) 删除索引。它要求您知道索引名称,但该名称应该在您之前的迁移之一中。
// older index added in a previous release
changeSet(author: "frank", id: "1354228052849-1") {
createIndex(indexName: "FKAC7AAF67162A158F", tableName: "answer_option") {
column(name: "question_id")
}
}
Create a new migration to remove the index.
创建一个新的迁移来移除索引。
changeSet(author: "[email protected]", id: "1381257863746-1") {
dropIndex(indexName: "FKAC7AAF67162A158F", tableName: "answer_option")
}
回答by Frank DeRosa
According to the MySQL Manual...
根据MySQL 手册...
SHOW INDEX FROM mydb.mytable;
will return information about the mytable. It returns several fields with info about the table and its index, including a Column_name
and key_name
fields. You can probably sort out which one you need.
将返回有关 mytable 的信息。它返回几个字段,其中包含有关表及其索引的信息,包括 aColumn_name
和key_name
字段。你大概可以找出你需要哪一个。
After that, you should be able to execute this:
之后,您应该能够执行此操作:
DROP INDEX index_name ON tbl_name
And boom, no more index.
繁荣,没有更多的指数。
回答by Nathan Voxland
If you are wanting to script the drop index from liqubase, you are going to need to do some scripting since the standard drop index requires an index name.
如果您想从 liqubase 编写删除索引的脚本,则需要编写一些脚本,因为标准删除索引需要索引名称。
One option is to use a custom change classusing the SQL from Frank's answer or access the JDBC metadata to get the actual index name from a passed table.
一种选择是使用Frank's answer 中的 SQL使用自定义更改类,或者访问 JDBC 元数据以从传递的表中获取实际索引名称。
Another option would be to create a stored procedure which takes a table name as a parameter and queries the information_schema to get the correct index name and then drops it.
另一种选择是创建一个存储过程,该过程将表名作为参数并查询 information_schema 以获取正确的索引名称,然后将其删除。
回答by Spencer Powell
Another way of doing this with liquibase would be to do the following:
使用 liquibase 执行此操作的另一种方法是执行以下操作:
changeSet(author: "[email protected]", id: "1381257863746-1") {
sql('DROP INDEX FKAC7AAF67162A158F')
}
changeSet(author: "[email protected]", id: "1381257863746-1") {
sql('DROP INDEX FKAC7AAF67162A158F')
}