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

Pgsql::getSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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