Pgsql::getTableNames()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Spatie\MigrateFresh\TableDroppers;
4
5
use Illuminate\Support\Facades\DB;
6
7
class Pgsql implements TableDropper
8
{
9
    public function dropAllTables()
10
    {
11
        $tableNames = $this->getTableNames();
12
13
        if ($tableNames->isEmpty()) {
14
            return;
15
        }
16
17
        DB::statement("DROP TABLE {$tableNames->implode(',')} CASCADE");
18
    }
19
20
    /**
21
     * Get a list of all tables in the schema.
22
     *
23
     * @return \Illuminate\Support\Collection
24
     */
25
    protected function getTableNames()
26
    {
27
        $schemas = DB::getConfig('used_schemas') ?: [DB::getConfig('schema')];
28
29
        $schemaCount = count($schemas);
30
31
        $binds = implode(',', array_fill(0, $schemaCount, '?'));
32
33
        return collect(
34
            DB::select("SELECT schemaname || '.' || tablename AS table FROM pg_catalog.pg_tables WHERE schemaname IN (".$binds.')', $schemas)
35
        )->pluck('table')->reject(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
            $tableName = explode('.', $value)[1];
37
38
            return $tableName === 'spatial_ref_sys';
39
        });
40
    }
41
}
42