Completed
Push — master ( 2ccea1...f0c834 )
by Freek
01:34
created

Pgsql   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A dropAllTables() 0 10 2
A drop() 0 4 1
A getTables() 0 6 1
A getSchema() 0 4 1
1
<?php
2
3
namespace Spatie\MigrateFresh\TableDroppers;
4
5
use DB;
6
use Illuminate\Support\Collection;
7
8
class Pgsql implements TableDropper
9
{
10
    public function dropAllTables()
11
    {
12
        $tables = $this->getTables($this->getSchema());
13
14
        if ($tables->isEmpty()) {
15
            return;
16
        }
17
18
        $this->drop($tables);
19
    }
20
21
    /**
22
     * Drop tables.
23
     *
24
     * @param \Illuminate\Support\Collection $tables
25
     */
26
    protected function drop(Collection $tables)
27
    {
28
        DB::statement("DROP TABLE {$tables->implode(',')} CASCADE");
29
    }
30
31
    /**
32
     * Get a list of all tables in the schema.
33
     *
34
     * @param $schema
35
     * @return \Illuminate\Support\Collection
36
     */
37
    protected function getTables($schema)
38
    {
39
        return collect(
40
            DB::select('SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = ?', [$schema])
41
        )->pluck('tablename');
42
    }
43
44
    /**
45
     * Get schema name for the connection.
46
     *
47
     * @return string
48
     */
49
    protected function getSchema()
50
    {
51
        return DB::getConfig('schema');
52
    }
53
}
54