|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tpetry\PostgresqlEnhanced\Schema; |
| 6 | + |
| 7 | +trait BuilderWipe |
| 8 | +{ |
| 9 | + public function dropAllContinuousAggregates(): void |
| 10 | + { |
| 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, |
| 15 | + ); |
| 16 | + 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 | + } |
| 22 | + } |
| 23 | + |
| 24 | + public function dropAllHypertables(): void |
| 25 | + { |
| 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, |
| 30 | + ); |
| 31 | + 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 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public function dropAllMaterializedViews(): void |
| 40 | + { |
| 41 | + $materializedViews = $this->getConnection()->table('pg_matviews')->select(['schemaname', 'matviewname'])->whereIn('schemaname', $this->getActiveSchemas())->get(); |
| 42 | + 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 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function dropAllTables(): void |
| 51 | + { |
| 52 | + $this->dropAllHypertables(); |
| 53 | + parent::dropAllTables(); |
| 54 | + } |
| 55 | + |
| 56 | + public function dropAllViews(): void |
| 57 | + { |
| 58 | + $this->dropAllContinuousAggregates(); |
| 59 | + $this->dropAllMaterializedViews(); |
| 60 | + parent::dropAllViews(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return string[] |
| 65 | + */ |
| 66 | + private function getActiveSchemas(): array |
| 67 | + { |
| 68 | + return rescue( |
| 69 | + callback: fn () => $this->getCurrentSchemaListing(), // Laravel >= 12.x |
| 70 | + rescue: [$this->connection->getConfig('schema') ?? 'public'], // Laravel < 12.x |
| 71 | + report: false, |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
0 commit comments