4
4
5
5
namespace Tpetry \PostgresqlEnhanced \Schema ;
6
6
7
+ use Illuminate \Support \Collection ;
8
+
7
9
trait BuilderWipe
8
10
{
9
11
public function dropAllContinuousAggregates (): void
10
12
{
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
+ )
15
19
);
16
20
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 " );
21
22
}
22
23
}
23
24
24
25
public function dropAllHypertables (): void
25
26
{
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
+ )
30
33
);
31
34
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 " );
36
36
}
37
37
}
38
38
39
39
public function dropAllMaterializedViews (): void
40
40
{
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
+ );
42
44
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 " );
47
46
}
48
47
}
49
48
@@ -57,7 +56,35 @@ public function dropAllViews(): void
57
56
{
58
57
$ this ->dropAllContinuousAggregates ();
59
58
$ 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
+ );
61
88
}
62
89
63
90
/**
0 commit comments