Skip to content

Fixed bug that the command migrate:fresh can't drop postgres tables by default.#7447

Merged
limingxinleo merged 6 commits intohyperf:masterfrom
binaryfire:postgres-migrate-fresh
Aug 2, 2025
Merged

Fixed bug that the command migrate:fresh can't drop postgres tables by default.#7447
limingxinleo merged 6 commits intohyperf:masterfrom
binaryfire:postgres-migrate-fresh

Conversation

@binaryfire
Copy link
Contributor

When running php bin/hyperf.php migrate:fresh with PostgreSQL, tables are not dropped if no schema is configured in the database config. This happens because PostgresBuilder::getAllTables() passes an empty array to compileGetAllTables() when no schema is configured, which results in no tables being returned.

This PR adds a getSchemas() method that defaults to ['public'] when no schema is configured, matching Laravel's behavior:
https://github.com/laravel/framework/blob/0bbe0022ea60de20fabaed520c00c84689b083ee/src/Illuminate/Database/Schema/PostgresBuilder.php#L237

@sy-records
Copy link
Member

Please update changelog.

@binaryfire
Copy link
Contributor Author

@sy-records Done

sy-records
sy-records previously approved these changes Jul 9, 2025
Copy link
Member

@sy-records sy-records left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@sy-records sy-records requested a review from limingxinleo July 9, 2025 06:48
huangdijia
huangdijia previously approved these changes Jul 15, 2025
Copy link
Member

@huangdijia huangdijia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: Approve ✅

Overview

This PR fixes a bug where PostgreSQL tables weren't being dropped during migrate:fresh operations when no schema was configured. The solution adds a getSchemas() method that defaults to ['public'], aligning with Laravel's behavior.

Analysis

✅ Code Quality & Style

  • Clean, readable implementation following existing patterns
  • Proper PHPDoc documentation
  • Consistent with codebase conventions

✅ Solution Approach

  • Addresses root cause: empty schema array passed to compileGetAllTables()
  • Centralized schema resolution in getSchemas() method
  • Applied consistently to both getAllTables() and getAllViews()

✅ Technical Implementation

  • Appropriate method placement as protected
  • Simple and robust logic
  • Good reusability between methods

✅ Security & Performance

  • No security risks identified
  • Minimal performance impact

Minor Considerations

  • Consider adding unit tests for default schema behavior
  • Schema assumption of public is standard for PostgreSQL

Recommendation

Approve - Well-implemented fix that cleanly solves the reported issue and follows established patterns.

@limingxinleo limingxinleo dismissed stale reviews from huangdijia and sy-records via 92c0ff1 August 2, 2025 02:58
@limingxinleo limingxinleo changed the title Use public schema for Postgres getAllTables if no schema provided Fixed bug that the command migrate:fresh can't drop postgres tables by default. Aug 2, 2025
@limingxinleo limingxinleo merged commit 36e0ca8 into hyperf:master Aug 2, 2025
82 of 83 checks passed
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.

4 participants

Comments