Skip to content

Table columns not discovered in multi-tenant databases #1141

Open
@vpratfr

Description

@vpratfr

Versions:

  • ide-helper Version: 2.9.0
  • Laravel Version: 8.x
  • PHP Version: 8.0

Description:

We have a multi-tenant application where models are split in 2 DB connections : system (app DB) and tenant (each customer has one)

Models on the tenant database do not have their attributes discovered from the database.

Package used to manage tenancy is hyn-multitenant which basically dynamically builds the Laravel DB connection information depending on the active tenant (which could be determined by the subdomain for instance or programmatically)

Issue

The code linked below does not get the proper database which should match a tenant database

protected function getPropertiesFromTable($model)
{
$table = $model->getConnection()->getTablePrefix() . $model->getTable();
$schema = $model->getConnection()->getDoctrineSchemaManager();
$databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');
$platformName = $databasePlatform->getName();
$customTypes = $this->laravel['config']->get("ide-helper.custom_db_types.{$platformName}", []);
foreach ($customTypes as $yourTypeName => $doctrineTypeName) {
$databasePlatform->registerDoctrineTypeMapping($yourTypeName, $doctrineTypeName);
}
$database = null;
if (strpos($table, '.')) {
[$database, $table] = explode('.', $table);
}
$columns = $schema->listTableColumns($table, $database);

$database stays null

Request

We should have a mecanism to allow discovery of those attributes.

One possible way to allow that could be to have a configuration entry where we could override the database according to the connection name.

Something like

   'db_mapping'=> [
      // connection => database name
      'system' => 'homestead',
      'tenant' => 'my_tenant',
   ],

Then code to get the database could be fixed liked that:

       $connectionName = $model->getConnection()->getName();
       $database = config("ide-helper.db_mapping.$connectionName");
        if (strpos($table, '.')) {
            [$database, $table] = explode('.', $table);
        }

I have tested that on our project. There should be no difference in previous behaviour if the configuration is not specified.

Any thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions