Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fix schema names on DatabaseTruncation trait (PostgreSQL and SQLServer) #53787

Merged
merged 10 commits into from
Dec 10, 2024

Conversation

hafezdivandari
Copy link
Contributor

@hafezdivandari hafezdivandari commented Dec 7, 2024

Fixes #53745

Unlike migrate:fresh, db:wipe commands, and RefreshDatabase trait, the DatabaseTruncation trait does not handle schema names on SQLServer and PostgreSQL and doesn't respect search_path on PostgreSQL.

This PR does:

  • Fix DatabaseTruncation trait to support schema names (SQLServer and PostgreSQL).
  • Fix DatabaseTruncation trait to respect search_path config of the DB connection (PostgreSQL).
  • Handle prefix config better on DatabaseTruncation trait.
  • Add tests for DatabaseTruncation trait.
  • Optimize Schema::dropAllTables(), Schema::dropAllViews(), and Schema::dropAllTypes() methods on the PostgreSQL schema builder: No need to escape schema names when comparing values, because Schema::getSchemas() and Schema::getTables() return unescaped values.

Example

Refer to DatabaseTruncation docs.

use DatabaseTruncation;

protected $tablesToTruncate = ['foo', 'private.bar']; // Truncates tables named "foo" on all schemas and "bar" table on "private" schema only.
protected $exceptTables = ['foo', 'private.bar'];     // Truncates all tables except any tables named "foo" on any schema, "bar" table on "private" schema.

You can also define these properties based on connection names:

use DatabaseTruncation;

protected $tablesToTruncate = [
    'mysql' => ['foo'],                // Truncates "foo" table when using "mysql" connection.
    'pgsql' => ['foo', 'private.bar'], // Truncates tables named "foo" on all schemas and "bar" table on "private" schema only when using "pgsql" connection.
];

protected $exceptTables = [
    'mysql' => ['foo'],                // Truncates all tables except "foo" table when using "mysql" connection.
    'pgsql' => ['foo', 'private.bar'], // Truncates all tables except any tables named "foo" on any schema, "bar" table on "private" schema when using "pgsql" connection.
];

Note: migrations table (on all schemas) is always ignored, and never get truncated using this trait.

Copy link

github-actions bot commented Dec 7, 2024

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@hafezdivandari hafezdivandari marked this pull request as ready for review December 10, 2024 10:37
@hafezdivandari hafezdivandari changed the title [11.x] Fix database truncation on PostgreSQL [11.x] Fix schema names on DatabaseTruncation trait (PostgreSQL and SQLServer) Dec 10, 2024
@taylorotwell
Copy link
Member

Hi @hafezdivandari - can you fix the conflict and mark as ready for review?

@taylorotwell taylorotwell marked this pull request as draft December 10, 2024 15:04
@hafezdivandari hafezdivandari marked this pull request as ready for review December 10, 2024 15:31
@hafezdivandari
Copy link
Contributor Author

Hi @taylorotwell it's ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DatabaseTruncation does not respect tables in other postgres schemas
2 participants