开发者

How to delete an index in Grails with Liquibase

开发者 https://www.devze.com 2022-12-11 04:41 出处:网络
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.

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.

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 (ea开发者_运维百科ch server my have different name).

is it possible to drop an index of something without knowing its name ?


According to the MySQL Manual...

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.

After that, you should be able to execute this:

DROP INDEX index_name ON tbl_name

And boom, no more index.


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.

// 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: "joe@example.com", id: "1381257863746-1") {
    dropIndex(indexName: "FKAC7AAF67162A158F", tableName: "answer_option")
}


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.

One option is to use a custom change class using the SQL from Frank's answer or access the JDBC metadata to get the actual index name from a passed table.

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.


Another way of doing this with liquibase would be to do the following:

changeSet(author: "joe@example.com", id: "1381257863746-1") { sql('DROP INDEX FKAC7AAF67162A158F') }

0

精彩评论

暂无评论...
验证码 换一张
取 消