How to Verify the Existence of a Unique Constraint in a Table as a Precondition in Liquibase?

I’m using Liquibase for database schema management, and I need to ensure that a unique constraint exists in a given table before proceeding with further database changes. Could you please guide me on how to check the existence of a unique constraint as a precondition in Liquibase?

Specifically, I would like to know the recommended approach or best practice for programmatically verifying the presence of a unique constraint in a table using Liquibase’s built-in functionality or available extensions.

Any insights or code examples would be greatly appreciated. Thank you!

Welcome @akshaysargar!

I’d suggest using Liquibase Preconditions.

In the changeset after your create table, use a precondition to check for the existence of the unique constraint, and add a change in the changeset that creates the unique constraint if it doesn’t exist.

The predefined preconditions are great, but I got into a problem with the “uniqueConstraintExists” precondition spesifically, because it does not support attribute schemaName. For that reason we have been unable to use it.

In our case the user running the liquibase migration will never be the same user /schema that actually owns the objects, so that we always require schemaName to be specified.