Skip to content

feat: back up the database before migrating on deploy#65

Open
kevinsleegerslameco wants to merge 1 commit into
mainfrom
feat/db-backup-before-migrate
Open

feat: back up the database before migrating on deploy#65
kevinsleegerslameco wants to merge 1 commit into
mainfrom
feat/db-backup-before-migrate

Conversation

@kevinsleegerslameco

Copy link
Copy Markdown
Contributor

Description

Symfony/Kunstmaan deploys already run database:migrate (before('deploy:symlink', 'database:migrate')) — but with no backup. dep rollback only repoints the code symlink, and down() migrations are frequently lossy, so a failed or destructive migration on prod has no recovery path. This is the real blocker before enabling auto-deploy to production.

Adds lameco:db_backup and hooks it before database:migrate, so every migrating deploy is preceded by a restorable dump.

Changes

  • lameco:db_backup — gzipped mariadb-dump --quick --single-transaction into shared/backups/<db>_<timestamp>.sql.gz, prunes to the newest N. Reuses the existing fetchEnv / extractDbCredentials helpers; DB name passed via a shell var so it's safely quoted. Halts the deploy if credentials can't be read (won't migrate without a backup).
  • Hookbefore('database:migrate', 'lameco:db_backup'), only for symfony/kunstmaan and only when enabled.
  • Settingslameco_db_backup_on_deploy (default true), lameco_db_backup_keep (default 5).

Type of Change

  • New feature (safety; default-on, opt-out via setting)

Testing

  • php -l clean, vendor/bin/ecs check clean.
  • Recommend validating on a staging deploy first (small DB): confirm shared/backups/ fills and prunes, then enable confidence for prod.

Notes

  • Craft/Laravel don't auto-migrate today (no database:migrate hook), so they get no pre-migrate backup. If/when Craft craft up-on-deploy is added, pair it with this task the same way.
  • Pair with expand/contract migration discipline — the backup is recovery, not a substitute for backward-compatible migrations.

🤖 Generated with Claude Code

Symfony/Kunstmaan deploys already run `database:migrate` before the
symlink switch, but with no safety net: `dep rollback` only repoints the
code symlink, and down() migrations are frequently lossy. A failed or
destructive migration on prod has no recovery path.

Add a `lameco:db_backup` task that writes a gzipped mariadb-dump
(--single-transaction) to shared/backups and prunes to the newest N, and
hook it `before('database:migrate', ...)` so every migrating deploy is
preceded by a restorable dump. Controlled by two settings (default on):

  lameco_db_backup_on_deploy (bool, default true)
  lameco_db_backup_keep      (int,  default 5)

If credentials can't be read the deploy halts rather than migrating
without a backup; set lameco_db_backup_on_deploy=false to opt out.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant