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()); |
|
|
|
|
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
|
|
|
|
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.