Completed
Pull Request — master (#1)
by Joao
04:35
created

PgsqlCommand   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 69
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareEnvironment() 0 6 1
A getDbDriverWithoutDatabase() 0 5 1
A createDatabaseIfNotExists() 0 11 2
A createDatabase() 0 5 1
A dropDatabase() 0 11 2
A createVersion() 0 5 1
A executeSql() 0 8 2
A executeSqlInternal() 0 7 2
1
<?php
2
3
namespace ByJG\DbMigration\Commands;
4
5
use ByJG\AnyDataset\Factory;
6
use ByJG\Util\Uri;
7
8
class PgsqlCommand extends AbstractCommand
9
{
10
11
    public static function prepareEnvironment(Uri $uri)
12
    {
13
        $database = preg_replace('~^/~', '', $uri->getPath());
14
        $dbDriver = self::getDbDriverWithoutDatabase($uri);
15
        self::createDatabaseIfNotExists($dbDriver, $database);
16
    }
17
18
    protected static function getDbDriverWithoutDatabase(Uri $uri)
19
    {
20
        $customUri = new Uri($uri->__toString());
21
        return Factory::getDbRelationalInstance($customUri->withPath('/')->__toString());
22
    }
23
24
    protected static function createDatabaseIfNotExists($dbDriver, $database)
25
    {
26
        $currentDbName = $dbDriver->getScalar(
27
            "SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(:dbname)",
28
            ['dbname' => $database]
29
        );
30
31
        if (empty($currentDbName)) {
32
            $dbDriver->execute("CREATE DATABASE $database WITH encoding=UTF8;");
33
        }
34
    }
35
36
    public function createDatabase()
37
    {
38
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
39
        self::createDatabaseIfNotExists($this->getDbDriver(), $database);
40
    }
41
42
    public function dropDatabase()
43
    {
44
        // $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
46
        $iterator = $this->getDbDriver()->getIterator(
47
            "select 'drop table if exists \"' || tablename || '\" cascade;' command from pg_tables where schemaname = 'public';"
48
        );
49
        foreach ($iterator as $singleRow) {
50
            $this->getDbDriver()->execute($singleRow->get('command'));
51
        }
52
    }
53
54
    public function createVersion()
55
    {
56
        $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS migration_version (version int)');
57
        $this->checkExistsVersion();
58
    }
59
60
    public function executeSql($sql)
61
    {
62
        $statements = explode(";", $sql);
63
64
        foreach ($statements as $sql) {
65
            $this->executeSqlInternal($sql);
66
        }
67
    }
68
69
    protected function executeSqlInternal($sql)
70
    {
71
        if (empty(trim($sql))) {
72
            return;
73
        }
74
        $this->getDbDriver()->execute($sql);
75
    }
76
}
77