tenancy: [v3.5.0] - Database manager for driver is not registered

Bug description

I’m trying to upgrade from 3.4.5 to 3.5.0, but when accessing a Tenant I am getting an error

Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException
Database manager for driver is not registered.

It seems to have been caused by the changes made in https://github.com/archtechx/tenancy/commit/73a4a3018cadca2ba0fb5f2130fca1718a2b3670

It’s this line, https://github.com/archtechx/tenancy/commit/73a4a3018cadca2ba0fb5f2130fca1718a2b3670#diff-570b873722200ff149851a2e1c3c016ed87596744cc8b8e5be003760a825f974R81, where it is now unsetting the database.connections.tenant config.

I am using the PostgreSQLDatabaseManager.

Steps to reproduce

n/a

Expected behavior

Connect to the Tenant database successfully.

Laravel version

8.78.1

stancl/tenancy version

3.5.0

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 28 (17 by maintainers)

Commits related to this issue

Most upvoted comments

@metadeck For toArray(), see this: https://github.com/archtechx/tenancy/issues/774#issuecomment-1010048328

The method essentially uses the model’s PDO connection to fetch the format used for timestamps.

The ideal solution is to avoid converting tenant models to an array when you’re outside the tenant context.

One possible fix that I proposed above would be setting the date format while the connection is still alive.

And then if there were still issues, I could reimplement this part of the logic to keep the last tenant’s connection alive. But to be honest I don’t really like that solution. Ending tenancy should remove all of the connections that were created for the tenant. Keeping a connection alive after that to support edge cases like these feels messy even if it’d be practical in some cases.

So ideally this could be fixed just by changing your usage of the models.

Updating from 3.4.5 to 3.5.1 faced the same issue. I’ve made the following changes:

//config/tenancy.php

    'database' => [
        /**
         * Connection used as a "template" for the tenant database connection.
         */
-        'template_tenant_connection' => 'tenant',
+        'template_tenant_connection' => 'tenant_template',

//config/database.php

        //Tenant
-        'tenant' => [
+        'tenant_template' => [
            'driver'         => 'mysql',
            'url'            => env('DATABASE_URL'),
            'host'           => env('DB_HOST', '127.0.0.1'),
            'port'           => env('DB_PORT', '3306'),
            'database'       => null,
            'username'       => env('DB_USERNAME', 'forge'),
            'password'       => env('DB_PASSWORD', ''),
            'unix_socket'    => env('DB_SOCKET', ''),
            'charset'        => 'utf8mb4',
            'collation'      => 'utf8mb4_unicode_ci',
            'prefix'         => '',
            'prefix_indexes' => true,
            'strict'         => true,
            'engine'         => null,
            'options'        => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

For the first sight, application works fine. However, any test that uses DB connection now doesn’t give any error anymore, but just “hang” forever. @stancl any idea why?

Hmm I see, nice work finding that!

I’d say that there’s no reason to do e.g. toArray() yourself on models that are on the wrong connection. But perhaps due to cases like these, we could add a feature to make it possible to recover the connection on the model instance?

We could store the tenant ID in a special property that’s not part of $attributes, for example. But still, that’d have to temporarily initialize tenancy, so it’d be pretty heavy. Finding an alternative solution first would be better.

Yeah, naming it tenant_template would be ideal. And there should be no tenant connection in config/database.php defined by you.