Trả lời nhanh: Docker database migration production cần rollout có kiểm soát: chốt topology/version, chạy staging hoặc canary, kiểm tra readiness và theo dõi metric nghiệp vụ trước khi tăng traffic. Mọi mốc dừng và revision quay lại đều được ghi rõ.
Phạm vi: Bài áp dụng cho Migration trước/sau app trong môi trường staging/standalone. Kiểm tra Nginx/Docker/OS/database và phiên bản CLI thực tế trước khi chạy; không đưa credential thật vào lệnh, log hay artifact.

Tín hiệu cần đối chiếu: Migration trước/sau app; backward compatibility; lock; điều kiện trước triển khai; staging/canary; health/readiness; rollback
Migration trước/sau app — thao tác và bằng chứng
The safe order is expand schema, deploy code that reads both versions, backfill, switch writes, then contract. Render the migration plan before running it (`docker compose run –rm api ./manage.py migrate –plan`) and record image digest plus database engine/version. Expected plan contains additive/nullable changes only; a DROP or NOT NULL without a backfill gate is a stop condition. Keep old app traffic alive until the new column is available. PostgreSQL—SQL migrations
Test old and new images against the expanded schema in staging. A migration command returning 0 proves the tool completed, not that every reader is compatible. Capture SQL lock waits, transaction duration and row counts. Rollback at this stage means the application image, while preserving the additive schema for backward compatibility. (docker-compose-database-migration-expand-contract-rollback · outline 1)
Với Migration trước/sau app, ghi lại input và phiên bản CLI trước khi chạy; sau đó đối chiếu từng tín hiệu điều kiện trước triển khai, staging/canary, health/readiness, rollback, Migration trước/sau app, backward compatibility, lock trong stdout, stderr hoặc log audit. Kết quả đạt phải chỉ ra trạng thái và phạm vi tài nguyên; kết quả lỗi phải có chuỗi nhận diện để người trực ca biết bước khoanh vùng kế tiếp. Gắn transcript với docker-compose-database-migration-expand-contract-rollback, commit/config hash, owner và timestamp. Không dùng exit code đơn độc làm bằng chứng. Che token, private key, cookie và PII trước khi lưu artifact; giữ bản raw trong kho có quyền hạn nếu incident yêu cầu.
backward compatibility — thao tác và bằng chứng
Backward compatibility means both the previous and new application can read/write the intermediate schema. Add a nullable column or shadow table first; deploy code that writes old and new fields; compare values before switching reads. Run the real proxy/worker path, not only a process health endpoint. Expected evidence is successful smoke traffic from both image digests with no `column does not exist` or deserialisation errors. PostgreSQL—SQL migrations
Do not put a destructive migration and image replacement in one Compose command. If old code cannot tolerate a new index/column, treat that as a design failure and stop before production. Record the compatibility window and the owner who decides when contract cleanup is allowed. (docker-compose-database-migration-expand-contract-rollback · outline 2)
Một lần kiểm tra backward compatibility chỉ có giá trị khi lặp lại được. Chốt workload, hostname, digest hoặc schema, rồi chạy lệnh tương ứng với docker-compose-database-migration-expand-contract-rollback. Đối chiếu các tín hiệu bắt buộc (điều kiện trước triển khai, staging/canary, health/readiness, rollback, Migration trước/sau app, backward compatibility, lock) với expected output và error signature; nếu thiếu dữ liệu, ghi unknown. So sánh baseline trước/sau trong cùng cửa sổ và nêu rõ điều gì không được suy ra từ phép đo. Artifact thay đổi cần ghi config diff đã redact, người chịu trách nhiệm và thời điểm hết hạn exception trước khi promote.
lock — thao tác và bằng chứng
Migration locks are an operational failure mode. Before applying, inspect active sessions (`SELECT pid,wait_event_type,query FROM pg_stat_activity WHERE state <> 'idle';`) and set a statement/lock timeout appropriate to the maintenance window. Expected output is a short lock wait followed by commit; `canceling statement due to lock timeout` must leave the old app serving and the migration marked incomplete. PostgreSQL—SQL migrations
Never solve a lock timeout by terminating unknown sessions blindly. Save the blocked query, transaction age and owner, then retry only after the blocker is understood. A rollback here is stopping the migration job and restoring the previous application image; schema rows already committed require a tested compensating migration. (docker-compose-database-migration-expand-contract-rollback · outline 3)
Đừng gộp lock vào một checklist chung. Tạo một phép thử nhỏ có điều kiện, ghi command, exit code, metric và log line riêng cho docker-compose-database-migration-expand-contract-rollback. Các marker điều kiện trước triển khai, staging/canary, health/readiness, rollback, Migration trước/sau app, backward compatibility, lock phải xuất hiện ở đúng section để người đọc có thể truy từ claim tới evidence. Khi kết quả không khớp expected (Output của docker-compose-database-migration-expand-contract-rollback phải có trạng thái success và error phân biệt bằng exit code/stderr; unknown flag, permission denied hoặc timeout dẫn tới nhánh khoanh vùng riêng.), dừng rollout/restore, giữ hiện trường và mở issue thay vì sửa nhiều biến cùng lúc. Artifact công khai chỉ chứa giá trị đã che và hash kiểm chứng.

backup — thao tác và bằng chứng
Take a backup or snapshot that covers the affected tables before backfill, and verify it can be read in an isolated environment. Record backup LSN/timestamp, row count and checksum sample. A green backup job without a restore test is not a recovery plan. Keep write volume and replication lag within the declared window; pause backfill when lag or lock waits cross the threshold. PostgreSQL—SQL migrations
Restore the backup to a new volume/database for a read-only check before any destructive contract step. Do not overwrite production to test a migration rollback. If the backup is stale or incomplete, freeze the contract migration and escalate to the data owner while the additive schema remains in place. (docker-compose-database-migration-expand-contract-rollback · outline 4)
Trong runbook của docker-compose-database-migration-expand-contract-rollback, backup là điểm quyết định: allow/deny, ready/not-ready, pass/fail hoặc baseline/regression. Định lượng bằng tín hiệu điều kiện trước triển khai, staging/canary, health/readiness, rollback, Migration trước/sau app, backward compatibility, lock, ghi phiên bản và phạm vi áp dụng, rồi nêu ngưỡng chuyển sang rollback. Một output xanh nhưng dependency hoặc health đỏ vẫn là fail. Sau khi sửa, chạy lại cùng lệnh và lưu kết quả cạnh config hash; revoke/rotate credential trước khi dọn mọi bản sao chứa secret.
health gate — thao tác và bằng chứng
A health gate for a migration must exercise a business request through proxy, API and database. Check `/ready`, error rate, p95 latency, migration duration and replication lag for at least one observation window. Expected output is health=200 plus stable 5xx/latency and no pending migration lock. A process that is merely `running` can still fail every request because the schema is half-applied. PostgreSQL—SQL migrations
Define concrete stop thresholds before canary: for example any `column does not exist`, 5xx above baseline +2 percentage points, or lock wait over 5s. Assign an owner and link the dashboard/runbook. If one threshold fires, stop promotion and preserve the migration transcript and DB activity snapshot. (docker-compose-database-migration-expand-contract-rollback · outline 5)
Với health gate, ghi lại input và phiên bản CLI trước khi chạy; sau đó đối chiếu từng tín hiệu điều kiện trước triển khai, staging/canary, health/readiness, rollback, Migration trước/sau app, backward compatibility, lock trong stdout, stderr hoặc log audit. Kết quả đạt phải chỉ ra trạng thái và phạm vi tài nguyên; kết quả lỗi phải có chuỗi nhận diện để người trực ca biết bước khoanh vùng kế tiếp. Gắn transcript với docker-compose-database-migration-expand-contract-rollback, commit/config hash, owner và timestamp. Không dùng exit code đơn độc làm bằng chứng. Che token, private key, cookie và PII trước khi lưu artifact; giữ bản raw trong kho có quyền hạn nếu incident yêu cầu.
rollback schema — thao tác và bằng chứng
Schema rollback is not `docker compose down`. For an additive expand, roll the application image back and leave the new nullable column; for a bad backfill, stop the job and use a compensating update after a data-owner review. Only contract (drop/rename) after all old readers are gone and a verified backup exists. Expected audit records include migration ID, actor, SQL checksum and start/end time. PostgreSQL—SQL migrations
If a destructive migration already committed, restore into isolation first and quantify lost/new writes before choosing PITR. Never claim zero data loss from a successful CLI exit. Keep the old schema artifact and migration lock evidence even after traffic is healthy. (docker-compose-database-migration-expand-contract-rollback · outline 6)
Một lần kiểm tra rollback schema chỉ có giá trị khi lặp lại được. Chốt workload, hostname, digest hoặc schema, rồi chạy lệnh tương ứng với docker-compose-database-migration-expand-contract-rollback. Đối chiếu các tín hiệu bắt buộc (điều kiện trước triển khai, staging/canary, health/readiness, rollback, Migration trước/sau app, backward compatibility, lock) với expected output và error signature; nếu thiếu dữ liệu, ghi unknown. So sánh baseline trước/sau trong cùng cửa sổ và nêu rõ điều gì không được suy ra từ phép đo. Artifact thay đổi cần ghi config diff đã redact, người chịu trách nhiệm và thời điểm hết hạn exception trước khi promote.
audit — thao tác và bằng chứng
Audit the migration as a change to both code and data: commit/digest, migration plan, approver, backup ID, lock timeout, health metrics and final schema description. Redact connection strings and user data. The audit record should identify which version was live at each timestamp and which command performed the change. PostgreSQL—SQL migrations
Close the change only after a second operator reproduces the plan on staging and verifies the rollback branch. If evidence is missing, mark the change incomplete and keep the canary at the previous revision. This audit trail is what makes a future incident diagnosable instead of a guess from container logs. (docker-compose-database-migration-expand-contract-rollback · outline 7)
Đừng gộp audit vào một checklist chung. Tạo một phép thử nhỏ có điều kiện, ghi command, exit code, metric và log line riêng cho docker-compose-database-migration-expand-contract-rollback. Các marker điều kiện trước triển khai, staging/canary, health/readiness, rollback, Migration trước/sau app, backward compatibility, lock phải xuất hiện ở đúng section để người đọc có thể truy từ claim tới evidence. Khi kết quả không khớp expected (Output của docker-compose-database-migration-expand-contract-rollback phải có trạng thái success và error phân biệt bằng exit code/stderr; unknown flag, permission denied hoặc timeout dẫn tới nhánh khoanh vùng riêng.), dừng rollout/restore, giữ hiện trường và mở issue thay vì sửa nhiều biến cùng lúc. Artifact công khai chỉ chứa giá trị đã che và hash kiểm chứng.
Recipe lệnh/config cho docker-compose-database-migration-expand-contract-rollback
Chạy ở staging cho Docker database migration production với hostname, path và digest đã thay bằng giá trị không nhạy cảm. Lưu exit code cùng stdout/stderr đã redact và gắn artifact với docker-compose-database-migration-expand-contract-rollback.
docker compose run --rm api ./manage.py migrate --plan
docker compose run --rm api ./manage.py migrate
psql $DATABASE_URL -c '\d+ orders'
Expected output và error signature — docker-compose-database-migration-expand-contract-rollback
Output của docker-compose-database-migration-expand-contract-rollback phải có trạng thái success và error phân biệt bằng exit code/stderr; unknown flag, permission denied hoặc timeout dẫn tới nhánh khoanh vùng riêng. Đối chiếu thêm version, timestamp, health/readiness và log liên quan; output minh họa không phải kết quả production.

Rollback và stop boundary của docker-compose-database-migration-expand-contract-rollback
Dừng canary docker-compose-database-migration-expand-contract-rollback khi 5xx, latency p95, queue hoặc readiness vượt ngưỡng đã ghi. Đưa traffic về digest/config revision trước, kiểm tra smoke test rồi mới đóng rollout.
Failure mode cần tránh ở Migration trước/sau app
- Migration trước/sau app: thay đổi nhiều biến cùng lúc nên không biết nguyên nhân.
- Migration trước/sau app: đọc exit code nhưng bỏ qua health, dependency hoặc error signature.
- Migration trước/sau app: lưu secret/PII trong log, image layer, backup hoặc ảnh chụp.
- Migration trước/sau app: rollback nhầm failure domain làm blast radius lớn hơn.
Đọc tiếp trong cùng cụm Linux/DevOps — docker-compose-database-migration-expand-contract-rollback
chien luoc backup postgresql pg dump pg basebackup wal pitr và docker compose startup order healthcheck
Nguồn chính thức cho docker-compose-database-migration-expand-contract-rollback
PostgreSQL—SQL migrations; Docker—Compose production
FAQ theo đúng chủ đề Docker database migration production
Migration trước/sau app áp dụng cho phiên bản nào?
Kiểm tra version/tool và topology trước khi áp dụng Migration trước/sau app. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.
backward compatibility áp dụng cho phiên bản nào?
Kiểm tra version/tool và topology trước khi áp dụng backward compatibility. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.
lock áp dụng cho phiên bản nào?
Kiểm tra version/tool và topology trước khi áp dụng lock. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.
backup áp dụng cho phiên bản nào?
Kiểm tra version/tool và topology trước khi áp dụng backup. Chạy recipe ở trên trong staging; nếu expected/error signature khác, dừng và cập nhật runbook thay vì copy nguyên lệnh.
Cập nhật docker-compose-database-migration-expand-contract-rollback: 08/09/2026. Nội dung là runbook tham khảo; phải kiểm tra phiên bản, nguồn chính thức và rollback trong môi trường thật trước khi áp dụng. Safety boundary: Không thay thế tư vấn chuyên môn; kiểm tra tình huống thực tế trước khi áp dụng.
