<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mohamed Soliman</title>
    <description>The latest articles on DEV Community by Mohamed Soliman (@mohamed_soliman_76f3a0713).</description>
    <link>https://dev.to/mohamed_soliman_76f3a0713</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3217293%2F8dba33de-8ae2-4ace-9748-48ea4480ba07.png</url>
      <title>DEV Community: Mohamed Soliman</title>
      <link>https://dev.to/mohamed_soliman_76f3a0713</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohamed_soliman_76f3a0713"/>
    <language>en</language>
    <item>
      <title>Running migrations across 55+ tenant databases with Stancl/tenancy</title>
      <dc:creator>Mohamed Soliman</dc:creator>
      <pubDate>Tue, 01 Dec 2026 10:07:01 +0000</pubDate>
      <link>https://dev.to/mohamed_soliman_76f3a0713/running-migrations-across-55-tenant-databases-with-stancltenancy-2d2b</link>
      <guid>https://dev.to/mohamed_soliman_76f3a0713/running-migrations-across-55-tenant-databases-with-stancltenancy-2d2b</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two kinds of migrations
&lt;/h2&gt;

&lt;p&gt;Stancl/tenancy separates them, and getting this right comes first.&lt;/p&gt;

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

&lt;p&gt;You tell the package where tenant migrations live in config/tenancy.php:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'migration_parameters'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'--force'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'--path'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;database_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'migrations/tenant'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The commands
&lt;/h2&gt;

&lt;p&gt;The package gives you tenant versions of the usual commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan tenants:migrate
php artisan tenants:rollback
php artisan tenants:seed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can go wrong
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Things worth planning for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;A tenant database can be in a different state from the others if an earlier run was interrupted.&lt;/li&gt;
&lt;li&gt;Rolling back is also a loop, and it is not always safe once new data has been written.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Habits that help
&lt;/h2&gt;

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

&lt;h2&gt;
  
  
  Backups per tenant
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating databases on cPanel
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Here is the shape of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CPanelDatabaseManager&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;MySQLDatabaseManager&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TenantWithDatabase&lt;/span&gt; &lt;span class="nv"&gt;$tenant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;parent&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;createDatabase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$tenant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// production: create the database through the cPanel API&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No fork of the package was needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

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

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

&lt;p&gt;If you run a multi-tenant app, how do you handle migrations across tenants? I would like to compare notes.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>saas</category>
      <category>database</category>
    </item>
    <item>
      <title>Building a multi-tenant SaaS in Laravel: lessons from running 55+ companies on one codebase</title>
      <dc:creator>Mohamed Soliman</dc:creator>
      <pubDate>Mon, 30 Nov 2026 14:56:06 +0000</pubDate>
      <link>https://dev.to/mohamed_soliman_76f3a0713/building-a-multi-tenant-saas-in-laravel-lessons-from-running-45-companies-on-one-codebase-1pnk</link>
      <guid>https://dev.to/mohamed_soliman_76f3a0713/building-a-multi-tenant-saas-in-laravel-lessons-from-running-45-companies-on-one-codebase-1pnk</guid>
      <description>&lt;p&gt;I built Saleszawy, a sales, marketing and CRM platform used by 55+ companies, mostly on my own. Here is what I would tell anyone starting a multi-tenant Laravel app.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pick the isolation model early.&lt;/strong&gt; I use a separate database per tenant. It costs more to manage, but one company's data can never leak into another's, and you can back up, move or delete a single tenant cleanly. A shared database with a &lt;code&gt;tenant_id&lt;/code&gt; column is simpler, but one missing scope in a query becomes a data leak.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Resolve the tenant from the domain.&lt;/strong&gt; Each company gets its own domain or subdomain. A middleware resolves the tenant on every request and switches the database connection before any controller runs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Queues must be tenant-aware.&lt;/strong&gt; Jobs carry the tenant ID and re-initialize the tenant before running. I run Laravel Horizon on Redis so I can watch queues per workload. The classic bug is a job that runs against the wrong database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Real-time per tenant.&lt;/strong&gt; WebSocket channels are namespaced by tenant, so a company only ever hears its own events.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Migrations are a release process.&lt;/strong&gt; Every deploy migrates N databases. Run them in batches, log failures per tenant, and keep migrations backward compatible so one failed tenant does not block the rest.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Test the boundary.&lt;/strong&gt; Write tests that create two tenants and assert that user A cannot read anything from tenant B.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Questions welcome, find me on &lt;a href="https://www.linkedin.com/in/mohamed-soliman-laravel/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>saas</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
