Description
This change enhances the PostgreSQL schema builder to properly handle TimescaleDB hypertables when dropping all tables from the database.
Problem
When usingphp artisan migrate:fresh
or callingdropAllTables()
on databases with TimescaleDB hypertables, the operation fails because hypertables require special handling and cannot be dropped using standard batchDROP 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
andhypertable_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
- Seamless Development Workflow: Developers using TimescaleDB can now use
php artisan migrate:fresh
without manual intervention - Zero Breaking Changes: Maintains full backward compatibility with standard PostgreSQL databases
- Automatic Detection: No configuration required - automatically detects and handles TimescaleDB when present
- 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 namesif ($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 CASCADEif (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
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 callingdropAllTables()
on databases with TimescaleDB hypertables, the operation fails because hypertables require special handling and cannot be dropped using standard batchDROP TABLE
operations. This results in migration errors and prevents developers from refreshing their database schema during development.Solution
pg_extension
before attempting hypertable operationstimescaledb_information.hypertables
to identify existing hypertables using correct column names (hypertable_schema
andhypertable_name
)CASCADE
option before processing regular tablesBenefits to End Users
php artisan migrate:fresh
without manual interventionCode Changes
Modified Files
src/Illuminate/Database/Schema/PostgresBuilder.php
Key Changes
Compatibility