Video in progress
Why is spring.jpa.hibernate.ddl-auto=update dangerous in production, and what do you use instead?
update only changes the schema in the "add things" direction, and only roughly: it doesn't drop columns, can't rename them, doesn't migrate data, and doesn't record what it did anywhere. In prod you change the schema with Flyway or Liquibase migrations and keep ddl-auto at validate or none.
📖 Answer
Here's the kind of mess update makes in practice:
- rename a field and you get a new empty column, while the old one stays with the data;
- type changes (
int→long) often aren't applied; - indexes, constraints, data migration — none of it;
- dev, staging and prod schemas slowly drift apart, and nobody notices.
With Flyway:
src/main/resources/db/migration/
V1__init.sql
V2__add_order_status.sql
V3__backfill_status.sql
-- V2__add_order_status.sql
alter table orders add column status varchar(20) not null default 'NEW';
create index ix_orders_status on orders(status);
Which migration was applied and when is recorded in the flyway_schema_history table. On startup Flyway runs only the new ones, in order.
spring:
jpa:
hibernate:
ddl-auto: validate # entity and schema don't match — fails on startup
Don't edit a file that's already been applied — you'll get a checksum error; if you need a change, add a new file. To add an index to a big table in PostgreSQL, use create index concurrently — it has to live in its own migration that runs without a transaction.
Sign up to mark studied questions and save your progress.