Skip to content

DatabaseTruncation does not respect tables in other postgres schemas #53745

@kolaente

Description

@kolaente

Laravel Version

11.34.2

PHP Version

8.3.14

Database Driver & Version

Postgres 17, using the postgres:17 docker image and psql extension from the 8.3 php docker image

Description

Using the DatabaseTruncation trait in a PostgreSQL database with multiple schemas will fail with the error relation "foo_in_other_schema" does not exist.

This is unexpected because the database connection is set to only use the public schema.

I was able to work around this issue with this code in the test which used DatabaseTruncation:

    public $exceptTables = [];

    protected function beforeTruncatingDatabase(): void
    {
        $tables = DB::select("SELECT table_name FROM information_schema.tables WHERE table_schema = 'other_schema'");
        $this->exceptTables = array_map(fn($t) => $t->table_name, $tables);
    }

However, that resulted in the test running after the first one to fail because the old data was not properly cleaned up. The test uses RefreshDatabase. That required another workaround:

   protected function beforeRefreshingDatabase()
    {
        $tables = DB::select("SELECT table_name, table_type FROM information_schema.tables WHERE table_schema = 'public'");
        $tableList = implode(', ', array_map(fn($v) => $v->table_name, array_filter($tables, fn($v) => $v->table_type === 'BASE TABLE')));
        DB::unprepared('TRUNCATE '.$tableList);
    }

Steps To Reproduce

Here's a repo with a minimal reproduction: https://github.com/kolaente/laravel-framework-issue-53745-reproduction

To see the error:

  1. clone the repo
  2. cd in the directory
  3. run docker compose up -d
  4. run docker compose exec web php artisan migrate so that the tables exist
  5. run docker compose exec web php artisan test

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions