Skip to content

Commit 61c6f44

Browse files
authored
Add TimescaleDB hypertable support to PostgresBuilder::dropAllTables()
This change enhances the PostgreSQL schema builder to properly handle TimescaleDB hypertables when dropping all tables from the database.
1 parent 0d1887e commit 61c6f44

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/Illuminate/Database/Schema/PostgresBuilder.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@ class PostgresBuilder extends Builder
1616
public function dropAllTables()
1717
{
1818
$tables = [];
19+
$hypertables = [];
1920

2021
$excludedTables = $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys'];
22+
$hasTimescaleDB = !empty($this->connection->select("SELECT 1 FROM pg_extension WHERE extname = 'timescaledb'"));
23+
24+
if ($hasTimescaleDB) {
25+
$hypertables = $this->connection->select(
26+
"SELECT hypertable_schema || '.' || hypertable_name as name FROM timescaledb_information.hypertables"
27+
);
28+
$hypertables = array_column($hypertables, 'name');
29+
}
2130

2231
foreach ($this->getTables($this->getCurrentSchemaListing()) as $table) {
23-
if (empty(array_intersect([$table['name'], $table['schema_qualified_name']], $excludedTables))) {
24-
$tables[] = $table['schema_qualified_name'];
32+
if (!in_array($table['name'], $excludedTables) && !in_array($table['schema_qualified_name'], $excludedTables)) {
33+
if (in_array($table['schema_qualified_name'], $hypertables)) {
34+
$this->connection->statement("DROP TABLE IF EXISTS {$table['schema_qualified_name']} CASCADE");
35+
} else {
36+
$tables[] = $table['schema_qualified_name'];
37+
}
2538
}
2639
}
2740

0 commit comments

Comments
 (0)