Video in progress

What are the values of spring.jpa.hibernate.ddl-auto, and which one should you use in prod?

The values are none, validate, update, create and create-drop. In prod you use none or validate, and Flyway or Liquibase migrations manage the schema.

📖 Answer

What each one does:

  • create-drop: creates the tables on startup and drops them on shutdown. It is the default for tests with an embedded database;
  • create: drops and recreates the tables on every start. Data is lost;
  • update: adds missing tables and columns, but doesn't delete anything or change a type;
  • validate: changes nothing, only checks that the entities match the schema; if they don't, the application won't start;
  • none: does nothing.

Why not update in prod: it keeps no record of which change was applied when, there's no rollback, and if you rename a column it leaves the old one and adds a new one. It can't do things like indexes or data migration at all.

spring:
  jpa:
    hibernate:
      ddl-auto: validate
  flyway:
    enabled: true

I like validate: if you make a typo in a column name in a migration, the error shows up at startup instead of reaching prod.