Renaming Database Constraints with Liquibase

Broonix September 13, 2016
Source

We used hibernate to set up our initial database schema. Hibernate generates constraint names like: fk_jwy3ip299dvht6bhrf7kxnmap and fk_bc7w4arbgsiasoufirt1ec0na. While these are unique and of a length that works on all databases, they are not very descriptive.

We are using H2 and Postgres for our database (test, production) both of which support longer constraint names. Since we use Liquibase to deploy our database changes, this gave me a great way to rename all these constraints.

First I went into our Postgres instance and got a list of all the foreign key constraints.

[Code block]

I exported this query to CSV. This gave me a file in this format:

[Code block]

We have over 50 constraints on our tables. There was no way I was planning to type out all of these changes in YAML. I ended up creating two python scripts to output the needed YAML.

Drop constraints

[Code block]

Add back Constraints

[Code block (python)]

Read the original post with all embeds and interactive content at https://rants.broonix.ca/renaming-database-constraints-with-liquibase/

Discussion in the ATmosphere

Loading comments...