Passed
Push — master ( aa56fb...89b72d )
by Joao
10:12 queued 12s
created

PgsqlDatabase::schema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace ByJG\DbMigration\Database;
4
5
use ByJG\AnyDataset\Db\Factory;
6
use ByJG\Util\Uri;
7
use Psr\Http\Message\UriInterface;
8
9
class PgsqlDatabase extends AbstractDatabase
10
{
11
    public static function schema()
12
    {
13
        return 'pgsql';
14
    }
15
16
    public static function prepareEnvironment(UriInterface $uri)
17
    {
18
        $database = preg_replace('~^/~', '', $uri->getPath());
19
        $dbDriver = static::getDbDriverWithoutDatabase($uri);
20
        static::createDatabaseIfNotExists($dbDriver, $database);
21
    }
22
23
    protected static function getDbDriverWithoutDatabase(UriInterface $uri)
24
    {
25
        $customUri = new Uri($uri->__toString());
26
        return Factory::getDbRelationalInstance($customUri->withPath('/postgres')->__toString());
27
    }
28
29
    /**
30
     * @param \ByJG\AnyDataset\Db\DbDriverInterface $dbDriver
31
     * @param $database
32
     */
33
    protected static function createDatabaseIfNotExists($dbDriver, $database)
34
    {
35
        $currentDbName = $dbDriver->getScalar(
36
            "SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(:dbname)",
37
            ['dbname' => $database]
38
        );
39
40
        if (empty($currentDbName)) {
41
            $dbDriver->execute("CREATE DATABASE $database WITH encoding=\"UTF8\";");
42
        }
43
    }
44
45
    public function createDatabase()
46
    {
47
        $database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath());
48
        static::createDatabaseIfNotExists($this->getDbDriver(), $database);
49
    }
50
51
    public function dropDatabase()
52
    {
53
        $iterator = $this->getDbDriver()->getIterator(
54
            "select 'drop table if exists \"' || tablename || '\" cascade;' command from pg_tables where schemaname = 'public';"
55
        );
56
        foreach ($iterator as $singleRow) {
57
            $this->getDbDriver()->execute($singleRow->get('command'));
58
        }
59
    }
60
61
    /**
62
     * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException
63
     * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException
64
     */
65
    public function createVersion()
66
    {
67
        $this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS ' . $this->getMigrationTable() . ' (version int, status varchar(20), PRIMARY KEY (version))');
68
        $this->checkExistsVersion();
69
    }
70
71
    public function executeSql($sql)
72
    {
73
        $statements = preg_split("/;(\r\n|\r|\n)/", $sql);
74
75
        foreach ($statements as $sql) {
0 ignored issues
show
introduced by
$sql is overwriting one of the parameters of this function.
Loading history...
76
            $this->executeSqlInternal($sql);
77
        }
78
    }
79
80
    protected function executeSqlInternal($sql)
81
    {
82
        if (empty(trim($sql))) {
83
            return;
84
        }
85
        $this->getDbDriver()->execute($sql);
86
    }
87
88
    public function isDatabaseVersioned()
89
    {
90
        return $this->isTableExists('public', $this->getMigrationTable());
91
    }
92
}
93