Running migrations across 55+ tenant databases with Stancl/tenancy

2026-12-01来源 dev.toEnglish

When you run one database, a migration is one command. When every customer has their own database, a migration is a loop, and the loop can fail halfway.

I work on a multi-tenant SaaS built on Laravel with a database per tenant, using stancl/tenancy. It serves 55+ companies from one codebase. This post is about a part that is easy to ignore until the first bad deploy: changing the schema of all those databases safely.

Two kinds of migrations

Stancl/tenancy separates them, and getting this right comes first.

  • Central migrations live in database/migrations. They run once, against the central database (tenants, domains and so on).
  • Tenant migrations live in their own folder, for example database/migrations/tenant. They run once per tenant database.

You tell the package where tenant migrations live in config/tenancy.php:

'migration_parameters' => [ '--force' => true, '--path' => [database_path('migrations/tenant')], ], 

The --force flag is needed to run migrations in production. If a table holds customer data, it belongs in the tenant folder. Put it in the wrong one and you will find out in production.

The commands

The package gives you tenant versions of the usual commands:

php artisan tenants:migrate php artisan tenants:rollback php artisan tenants:seed 

tenants:migrate loops over your tenants and runs the tenant migrations on each database. The --tenants option lets you target specific tenants, which is a good way to try a migration on one or two before running it on all of them.

What can go wrong

With one database, a failed migration stops the deploy. With many, you can end up with some tenants on the new schema and some on the old one, and your code has to work with both until you fix it.

Things worth planning for:

  1. A migration that works on an empty tenant can fail on a tenant with real data, for example a new NOT NULL column or a unique index on dirty data.
  2. A tenant database can be in a different state from the others if an earlier run was interrupted.
  3. Rolling back is also a loop, and it is not always safe once new data has been written.

Habits that help

  • Write migrations so that running them twice does not break anything.
  • Add new columns as nullable or with a default first, and tighten them in a later release.
  • Try the migration on one or two tenants with --tenants before running it on everyone.
  • Make sure you can tell which tenant failed. Log the tenant key, so a failure is one grep away.
  • Test on a tenant that has real data, not only on a fresh one.

Backups per tenant

Because each customer has their own database, backup and restore also work per tenant. If one company needs to go back to an earlier state, you can restore that one database without touching the other customers. This is one of the real reasons to choose database per tenant over a shared schema.

Creating databases on cPanel

Our app runs on cPanel hosting, where creating a database needs the cPanel API rather than a plain CREATE DATABASE. Stancl/tenancy lets you swap the database manager in config/tenancy.php, so we use a small custom class that extends the package's MySQLDatabaseManager. In production it creates the tenant database through the cPanel API. On a local machine it falls back to normal SQL, so development needs no cPanel access.

Here is the shape of it:

class CPanelDatabaseManager extends MySQLDatabaseManager { public function createDatabase(TenantWithDatabase $tenant): bool { if (app()->environment('local')) { return parent::createDatabase($tenant); } // production: create the database through the cPanel API } } 

No fork of the package was needed.

Summary

  • Decide early which tables are central and which are tenant.
  • Test every migration on a tenant with real data.
  • Know how you will find out which tenant failed, before the first failure.
  • Keep per-tenant backups. You will need one eventually.

Database per tenant costs more operational work than a shared schema. For us, the isolation and per-customer restore are worth it.

If you run a multi-tenant app, how do you handle migrations across tenants? I would like to compare notes.

原文:https://www.a8q9.cn/en/article/running-migrations-across-55-tenant-databases-with-stancl-tenancy-388ee9.html

相关文章