How to correctly add a primary key to databasechangelog(PostgreSQL) for Liquibase?

3.9k views Asked by At

I try to migrate database from old PostgreSQL (9.3) to new PostgreSQL(9.5, 9.6) by Bucardo.

Bucardo used Primary key for migrate.

I have many databases with public.databasechangelog without Primary key. How correct add primary key to databasechangelog(PostgreSQL) for Liquibase?

Update answer: I can add Primary Key by SQL, but may be Liquibase have setting for added Primary key in XML?

Safely added Primary Key by SQL to public.databasechangelog ?

2

There are 2 answers

1
mgms_kumara On

I create DATABASECHANGELOG table at the beginning with primary key. This may not the correct way, but it will help

CREATE TABLE "DATABASECHANGELOG" (
  "ID" varchar(255) NOT NULL,
  "AUTHOR" varchar(255) NOT NULL,
  "FILENAME" varchar(255) NOT NULL,
  "DATEEXECUTED" datetime NOT NULL,
  "ORDEREXECUTED" varchar(10) NOT NULL,
  "EXECTYPE" varchar(45) NOT NULL,
  "MD5SUM" varchar(35) DEFAULT NULL,
  "DESCRIPTION" varchar(255) DEFAULT NULL,
  "COMMENTS" varchar(255) DEFAULT NULL,
  "TAG" varchar(255) DEFAULT NULL,
  "LIQUIBASE" varchar(20) DEFAULT NULL,
  "CONTEXTS" varchar(255) DEFAULT NULL,
  "LABELS" varchar(255) DEFAULT NULL,
  "DEPLOYMENT_ID" varchar(10) DEFAULT NULL,
  PRIMARY KEY ("ID")
)
1
AudioBubble On

The Liquibase developers decided to remove the primary key on that table because apparently some DBMS have a problem with creating an index on those three columns due to limits on the maximum size of an index entry.

See e.g. this discussion: http://forum.liquibase.org/topic/why-does-databasechangelog-not-have-a-primary-key

Postgres is not subject to those limits, so you should be fine just adding one:

alter table databasechangelog add primary key (id, author, filename);