laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.53k stars 11.02k forks source link

Schema::hasTable does not work when multiple schemas are used #38888

Closed laacz closed 3 years ago

laacz commented 3 years ago

Description:

When checking for table existence using Schema::hasTable(), PostgreSQL grammar does not take into account multiple schemas, although Illuminate\Database\Connectors\PostgresConnector::configureSchema() allows for multiple schemas in search_path.

I'll be happy to offer a fix via PR later, if this will be considered worth fixing.

Steps To Reproduce:

Configure your postgres database connection with multiple schemas. For example:

'pgsql' => [
    'driver' => 'pgsql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '5432'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    'charset' => 'utf8',
    'prefix' => '',
    'schema' => ['public', 'user', 'postgis'],
],

Schema::hasTable generated query is:

select * from information_schema.tables 
 where table_schema = 'public'
   and table_name = 'table'
   and table_type = 'BASE TABLE'

It should be (with schemas array as a binding):

select * 
  from information_schema.tables 
 where table_schema IN ('public', 'user', 'postgis') 
   and table_name = 'table'
   and table_type = 'BASE TABLE'
derekmd commented 3 years ago

You're better off submitting the PR if you want this feature. It really isn't a "fix" since support for many schemas isn't officially documented:

  1. The config('database.connections.pgsql.schema') sample shows a single schema configured with a string:

    https://github.com/laravel/laravel/blob/195faa16cbeabc84b0eb9c9f4227ead762c93575/config/database.php#L66-L79

  2. https://laravel.com/docs/8.x/database#configuration doesn't cover configuring it.
  3. https://laravel.com/docs/8.x/migrations#checking-for-table-column-existence only shows Schema::hasTable() supporting a string argument.

Feature requests are meant to be directed through https://github.com/laravel/framework/discussions.

laacz commented 3 years ago

Thanks, @derekmd. I'll do just that then.

driesvints commented 3 years ago

Thanks @derekmd