Skip to content
Jan Szoja edited this page Aug 26, 2018 · 4 revisions

Multi-database/Multi-tenant migrations

I implemented this concept in the multidb branch.

Your migration class has to implement MultiDbInterface to avail for cross-database migrations. This interface defines two methods:

/**
     * This function shall return the label, which will be used later
     * for retrival of the database list, eg:
     * - 'all' - will get the core/main database along with the all tenants databases
     * - 'tenants' - will get the list of the tenants databases only
     *
     * @see getDbList()
     * @return string Label of the set of databases to be migrated
     */
    public function getDbGroup();

    /**
     * Return the list of the databases based on the passed $group
     *
     * @see getDbGroup()
     *
     * @param $group
     *
     * @return array List of the databases to run migrations on
     */
    public function getDbList($group);

Sample migration class

class Multi3 extends AbstractMigration implements MultiDbInterface
{

    public function getDbGroup() {
        return 'tenant';
    }

    // $group is a returned value of getDbGroup()
    public function getDbList($group)
    {
        $tenants = [ 'client1', 'client2', 'client3' ];
        return $group === 'tenant'
            ? $tenants
            : ['core'] + $tenants;
    }    

    public function change()
    {
        $products = $this->table('products');
        $products->addColumn('name', 'string', ['limit' => 128])
            ->create();
    }
}
Clone this wiki locally