Skip to content

Add TimescaleDB hypertable support to PostgresBuilder::dropAllTables() #56280

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

Draft
wants to merge 4 commits into
base: 12.x
Choose a base branch
from

Conversation

designgears
Copy link

Description

This change enhances the PostgreSQL schema builder to properly handle TimescaleDB hypertables when dropping all tables from the database.

Problem

When using php artisan migrate:fresh or calling dropAllTables() on databases with TimescaleDB hypertables, the operation fails because hypertables require special handling and cannot be dropped using standard batch DROP TABLE operations. This results in migration errors and prevents developers from refreshing their database schema during development.

Solution

  • TimescaleDB Detection: Added detection for the TimescaleDB extension using pg_extension before attempting hypertable operations
  • Hypertable Identification: Query timescaledb_information.hypertables to identify existing hypertables using correct column names (hypertable_schema and hypertable_name)
  • Separate Handling: Drop hypertables individually with CASCADE option before processing regular tables
  • Graceful Fallback: When TimescaleDB extension is not installed, the method continues with standard table dropping behavior

Benefits to End Users

  1. Seamless Development Workflow: Developers using TimescaleDB can now use php artisan migrate:fresh without manual intervention
  2. Zero Breaking Changes: Maintains full backward compatibility with standard PostgreSQL databases
  3. Automatic Detection: No configuration required - automatically detects and handles TimescaleDB when present
  4. Error Prevention: Eliminates common migration failures in TimescaleDB environments

Code Changes

Modified Files

  • src/Illuminate/Database/Schema/PostgresBuilder.php

Key Changes

// Added TimescaleDB extension detection
$hasTimescaleDB = !empty($this->connection->select("SELECT 1 FROM pg_extension WHERE extname = 'timescaledb'"));

// Query hypertables with correct column names
if ($hasTimescaleDB) {
    $hypertables = $this->connection->select(
        "SELECT hypertable_schema || '.' || hypertable_name as name FROM timescaledb_information.hypertables"
    );
    $hypertables = array_column($hypertables, 'name');
}

// Drop hypertables individually with CASCADE
if (in_array($table['schema_qualified_name'], $hypertables)) {
    $this->connection->statement("DROP TABLE IF EXISTS {$table['schema_qualified_name']} CASCADE");
}

Compatibility

  • PostgreSQL: 9.6+ (existing requirement)
  • TimescaleDB: Optional extension support
  • Laravel: No breaking changes to existing API
  • PHP: Follows existing Laravel requirements

This change enhances the PostgreSQL schema builder to properly handle TimescaleDB hypertables when dropping all tables from the database.
@GrahamCampbell
Copy link
Member

GrahamCampbell commented Jul 13, 2025

This seems like a kinda edge use case. It's a slippery slope to try and support every database that uses the PostgreSQL wire protocol, with varying degrees of actually being compatible. EDIT: Oh, I see this is an extension, and not a separate database... still the use case is kinda fringe.

@designgears
Copy link
Author

This seems like a kinda edge use case. It's a slippery slope to try and support every database that uses the PostgreSQL wire protocol, with varying degrees of actually being compatible. EDIT: Oh, I see this is an extension, and not a separate database... still the use case is kinda fringe.

I'll leave this here either way, I did find a package that fills in a lot of the missing pgsql functionality.
laravel-postgresql-enhanced

@taylorotwell
Copy link
Member

Tests fail.

@taylorotwell taylorotwell marked this pull request as draft July 18, 2025 16:15
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.

3 participants