Skip to content

Commit 1aabe7e

Browse files
committed
fix: improved handling of hypertables/views not to drop (resolves #104)
1 parent 5cef347 commit 1aabe7e

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

src/Schema/BuilderWipe.php

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,45 @@
44

55
namespace Tpetry\PostgresqlEnhanced\Schema;
66

7+
use Illuminate\Support\Collection;
8+
79
trait BuilderWipe
810
{
911
public function dropAllContinuousAggregates(): void
1012
{
11-
$continuousAggregates = rescue(
12-
callback: fn () => $this->getConnection()->table('timescaledb_information.continuous_aggregates')->select(['view_schema', 'view_name'])->whereIn('view_schema', $this->getActiveSchemas())->get(),
13-
rescue: [],
14-
report: false,
13+
$continuousAggregates = $this->findRelations(
14+
rescue(
15+
callback: fn () => $this->getConnection()->table('timescaledb_information.continuous_aggregates')->select(['view_schema as schema', 'view_name as name'])->whereIn('view_schema', $this->getActiveSchemas())->get(),
16+
rescue: [],
17+
report: false,
18+
)
1519
);
1620
foreach ($continuousAggregates as $continuousAggregate) {
17-
$name = "{$continuousAggregate->view_schema}.{$continuousAggregate->view_name}";
18-
if (!\in_array($name, $this->connection->getConfig('dont_drop') ?? [])) {
19-
$this->connection->statement("drop materialized view if exists {$this->grammar->wrap($name)} cascade");
20-
}
21+
$this->connection->statement("drop materialized view if exists {$this->grammar->wrap($continuousAggregate['schema_qualified_name'])} cascade");
2122
}
2223
}
2324

2425
public function dropAllHypertables(): void
2526
{
26-
$hypertables = rescue(
27-
callback: fn () => $this->getConnection()->table('timescaledb_information.hypertables')->select(['hypertable_schema', 'hypertable_name'])->whereIn('hypertable_schema', $this->getActiveSchemas())->get(),
28-
rescue: [],
29-
report: false,
27+
$hypertables = $this->findRelations(
28+
rescue(
29+
callback: fn () => $this->getConnection()->table('timescaledb_information.hypertables')->select(['hypertable_schema as schema', 'hypertable_name as name'])->whereIn('hypertable_schema', $this->getActiveSchemas())->get(),
30+
rescue: [],
31+
report: false,
32+
)
3033
);
3134
foreach ($hypertables as $hypertable) {
32-
$name = "{$hypertable->hypertable_schema}.{$hypertable->hypertable_name}";
33-
if (!\in_array($name, $this->connection->getConfig('dont_drop') ?? [])) {
34-
$this->connection->statement("drop table if exists {$this->grammar->wrap($name)} cascade");
35-
}
35+
$this->connection->statement("drop table if exists {$this->grammar->wrap($hypertable['schema_qualified_name'])} cascade");
3636
}
3737
}
3838

3939
public function dropAllMaterializedViews(): void
4040
{
41-
$materializedViews = $this->getConnection()->table('pg_matviews')->select(['schemaname', 'matviewname'])->whereIn('schemaname', $this->getActiveSchemas())->get();
41+
$materializedViews = $this->findRelations(
42+
$this->getConnection()->table('pg_matviews')->select(['schemaname as schema', 'matviewname as name'])->whereIn('schemaname', $this->getActiveSchemas())->get()
43+
);
4244
foreach ($materializedViews as $materializedView) {
43-
$name = "{$materializedView->schemaname}.{$materializedView->matviewname}";
44-
if (!\in_array($name, $this->connection->getConfig('dont_drop') ?? [])) {
45-
$this->connection->statement("drop materialized view if exists {$this->grammar->wrap($name)} cascade");
46-
}
45+
$this->connection->statement("drop materialized view if exists {$this->grammar->wrap($materializedView['schema_qualified_name'])} cascade");
4746
}
4847
}
4948

@@ -57,7 +56,35 @@ public function dropAllViews(): void
5756
{
5857
$this->dropAllContinuousAggregates();
5958
$this->dropAllMaterializedViews();
60-
parent::dropAllViews();
59+
60+
$views = $this->findRelations($this->connection->table('pg_views')->select(['schemaname as schema', 'viewname as name'])->whereIn('schemaname', $this->getActiveSchemas())->get(), [
61+
// PostGIS
62+
'geometry_columns',
63+
'geography_columns',
64+
// pg_buffercache
65+
'pg_buffercache',
66+
// pgRouting
67+
'raster_columns',
68+
'raster_overviews',
69+
]);
70+
if (filled($views)) {
71+
$this->connection->statement("drop view {$this->grammar->columnize(array_column($views, 'schema_qualified_name'))} cascade");
72+
}
73+
}
74+
75+
/**
76+
* @return array<array{schema_qualified_name: string}>
77+
*/
78+
private function findRelations(Collection $relations, array $exclude = []): array
79+
{
80+
$excludedRelations = array_merge($this->connection->getConfig('dont_drop') ?? [], $exclude);
81+
82+
return array_filter(
83+
array: $this->connection->getPostProcessor()->processTables($relations->all()),
84+
callback: function ($relation) use ($excludedRelations) {
85+
return blank(array_intersect([$relation['name'], $relation['schema_qualified_name']], $excludedRelations));
86+
}
87+
);
6188
}
6289

6390
/**

0 commit comments

Comments
 (0)