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
|
|
|
|
12
|
|
|
public static function prepareEnvironment(UriInterface $uri) |
13
|
|
|
{ |
14
|
|
|
$database = preg_replace('~^/~', '', $uri->getPath()); |
15
|
|
|
$dbDriver = static::getDbDriverWithoutDatabase($uri); |
16
|
|
|
static::createDatabaseIfNotExists($dbDriver, $database); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected static function getDbDriverWithoutDatabase(UriInterface $uri) |
20
|
|
|
{ |
21
|
|
|
$customUri = new Uri($uri->__toString()); |
22
|
|
|
return Factory::getDbRelationalInstance($customUri->withPath('/postgres')->__toString()); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param \ByJG\AnyDataset\Db\DbDriverInterface $dbDriver |
27
|
|
|
* @param $database |
28
|
|
|
*/ |
29
|
|
|
protected static function createDatabaseIfNotExists($dbDriver, $database) |
30
|
|
|
{ |
31
|
|
|
$currentDbName = $dbDriver->getScalar( |
32
|
|
|
"SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower(:dbname)", |
33
|
|
|
['dbname' => $database] |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
if (empty($currentDbName)) { |
37
|
|
|
$dbDriver->execute("CREATE DATABASE $database WITH encoding=\"UTF8\";"); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function createDatabase() |
42
|
|
|
{ |
43
|
|
|
$database = preg_replace('~^/~', '', $this->getDbDriver()->getUri()->getPath()); |
44
|
|
|
static::createDatabaseIfNotExists($this->getDbDriver(), $database); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function dropDatabase() |
48
|
|
|
{ |
49
|
|
|
$iterator = $this->getDbDriver()->getIterator( |
50
|
|
|
"select 'drop table if exists \"' || tablename || '\" cascade;' command from pg_tables where schemaname = 'public';" |
51
|
|
|
); |
52
|
|
|
foreach ($iterator as $singleRow) { |
53
|
|
|
$this->getDbDriver()->execute($singleRow->get('command')); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
59
|
|
|
* @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
60
|
|
|
*/ |
61
|
|
|
public function createVersion() |
62
|
|
|
{ |
63
|
|
|
$this->getDbDriver()->execute('CREATE TABLE IF NOT EXISTS ' . $this->getMigrationTable() . ' (version int, status varchar(20))'); |
64
|
|
|
$this->checkExistsVersion(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function executeSql($sql) |
68
|
|
|
{ |
69
|
|
|
$statements = preg_split("/;(\r\n|\r|\n)/", $sql); |
70
|
|
|
|
71
|
|
|
foreach ($statements as $sql) { |
72
|
|
|
$this->executeSqlInternal($sql); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function executeSqlInternal($sql) |
77
|
|
|
{ |
78
|
|
|
if (empty(trim($sql))) { |
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
$this->getDbDriver()->execute($sql); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function isDatabaseVersioned() |
85
|
|
|
{ |
86
|
|
|
return $this->isTableExists('public', $this->getMigrationTable()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|