1
|
|
|
<?php |
2
|
|
|
namespace Kunstmaan\Skylab\Skeleton; |
3
|
|
|
use Symfony\Component\Finder\Finder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* PostgresQLSkeleton |
7
|
|
|
*/ |
8
|
|
|
class PostgreSQLSkeleton extends AbstractSkeleton |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
const NAME = "postgres"; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @return string |
15
|
|
|
*/ |
16
|
|
|
public function getName() |
17
|
|
|
{ |
18
|
|
|
return self::NAME; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param \ArrayObject $project |
23
|
|
|
* |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function create(\ArrayObject $project) |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$project["dbuser"] = $this->dialogProvider->askFor("Enter a PostgreSQL username", null, $project["name"]); |
29
|
|
|
$pwgen = new \PWGen(); |
30
|
|
|
$project["dbpass"] = $this->dialogProvider->askFor("Enter a PostgreSQL password", null, $pwgen->generate()); |
31
|
|
|
$project["dbname"] = $this->dialogProvider->askFor("Enter a PostgreSQL databasename", null, $project["name"]); |
32
|
|
|
$project["dbserver"] = $this->dialogProvider->askFor("Enter a PostgreSQL server host", null, "localhost"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function preMaintenance() |
39
|
|
|
{ |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function postMaintenance() |
46
|
|
|
{ |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param \ArrayObject $project |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function maintenance(\ArrayObject $project) |
55
|
|
|
{ |
56
|
|
|
$this->permissionsProvider->createGroupIfNeeded($project["name"]); |
57
|
|
|
$this->permissionsProvider->createUserIfNeeded($project["name"], $project["name"]); |
58
|
|
|
|
59
|
|
|
try { |
60
|
|
|
new \PDO( |
61
|
|
|
$this->dialogProvider->logQuery( |
62
|
|
|
'pgsql:host=' . $project["dbserver"] . ';dbname=' . $project["dbname"], |
63
|
|
|
array( |
64
|
|
|
"user" =>$project["dbuser"], |
65
|
|
|
"password" => $project["dbpass"] |
66
|
|
|
) |
67
|
|
|
), |
68
|
|
|
$project["dbuser"], |
69
|
|
|
$project["dbpass"] |
70
|
|
|
); |
71
|
|
|
} catch (\PDOException $exLoginTest) { |
72
|
|
|
$this->dialogProvider->logNotice("Cannot connect as " . $project["dbuser"] . ", lets test if the database exists (" . $exLoginTest->getMessage() . ")"); |
73
|
|
|
try { |
74
|
|
|
new \PDO( |
75
|
|
|
$this->dialogProvider->logQuery( |
76
|
|
|
'pgsql:host=' . $project["dbserver"] . ';dbname=' . $project["dbname"], |
77
|
|
|
array( |
78
|
|
|
"user" =>$this->app["config"]["postgresql"]["user"], |
79
|
|
|
"password" => $this->app["config"]["postgresql"]["password"] |
80
|
|
|
) |
81
|
|
|
), |
82
|
|
|
$this->app["config"]["postgresql"]["user"], |
83
|
|
|
$this->app["config"]["postgresql"]["password"] |
84
|
|
|
); |
85
|
|
|
$this->dialogProvider->logNotice("Database " . $project["dbname"] . " exists!"); |
86
|
|
|
} catch (\PDOException $exDBTest) { |
87
|
|
|
$this->dialogProvider->logNotice("Cannot connect to the " . $project["dbname"] . " database as " .$this->app["config"]["postgresql"]["user"]. " as well, lets create it. (" . $exDBTest->getMessage() . ")"); |
88
|
|
|
$backupDir = $this->fileSystemProvider->getProjectDirectory($project["name"]) . "/backup/"; |
89
|
|
|
$pdo = new \PDO( |
90
|
|
|
$this->dialogProvider->logQuery( |
91
|
|
|
'pgsql:host=' . $project["dbserver"] . ";dbname=template1", |
92
|
|
|
array( |
93
|
|
|
"user" =>$this->app["config"]["postgresql"]["user"], |
94
|
|
|
"password" => $this->app["config"]["postgresql"]["password"] |
95
|
|
|
) |
96
|
|
|
), |
97
|
|
|
$this->app["config"]["postgresql"]["user"], |
98
|
|
|
$this->app["config"]["postgresql"]["password"] |
99
|
|
|
); |
100
|
|
|
$pdo->exec($this->dialogProvider->logQuery("create user " . $project["dbuser"])); |
101
|
|
|
$pdo->exec($this->dialogProvider->logQuery("alter user ".$project["dbuser"]." with password '".$project["dbpass"]."'")); |
102
|
|
|
$pdo->exec($this->dialogProvider->logQuery("create database " . $project["dbname"] . " with owner " . $project["dbuser"] . " encoding 'UNICODE'")); |
103
|
|
|
$finder = new Finder(); |
104
|
|
|
$finder->files()->in($backupDir)->name("postgres-custom.dump"); |
105
|
|
|
if (count(iterator_to_array($finder)) > 0) { |
106
|
|
|
$this->processProvider->executeSudoCommand("PGPASSWORD='".$project["dbpass"]."' PGOPTIONS='-c maintenance_work_mem=256MB' pg_restore --disable-triggers -n public -Fc ".$backupDir."/postgres-custom.dump -d ". $project["dbname"], false, $project["name"]); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param \ArrayObject $project |
114
|
|
|
* |
115
|
|
|
* @return mixed |
116
|
|
|
*/ |
117
|
|
|
public function preBackup(\ArrayObject $project) |
118
|
|
|
{ |
119
|
|
|
$backupDir = $this->fileSystemProvider->getProjectDirectory($project["name"]) . "/backup/"; |
120
|
|
|
$this->fileSystemProvider->createDirectory($project, "backup"); |
121
|
|
View Code Duplication |
if (is_file($backupDir . '/postgres-custom.dump')) { |
|
|
|
|
122
|
|
|
$this->processProvider->executeSudoCommand('rm -f ' . $backupDir . '/postgres-custom.previous.dump'); |
123
|
|
|
$this->processProvider->executeSudoCommand('mv ' . $backupDir . '/postgres-custom.dump ' . $backupDir . '/postgres-custom.previous.dump'); |
124
|
|
|
} |
125
|
|
|
$this->processProvider->executeSudoCommand("PGPASSWORD='".$project["dbpass"]."' PGOPTIONS='-c maintenance_work_mem=256MB' pg_dump -Fc -f ".$backupDir."/postgres-custom.dump ".$project["dbname"], false, $project["name"]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param \ArrayObject $project |
130
|
|
|
* |
131
|
|
|
* @return mixed |
132
|
|
|
*/ |
133
|
|
|
public function postBackup(\ArrayObject $project) |
134
|
|
|
{ |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param \ArrayObject $project |
139
|
|
|
* |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
|
|
public function preRemove(\ArrayObject $project) |
143
|
|
|
{ |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param \ArrayObject $project |
148
|
|
|
* |
149
|
|
|
* @return mixed |
150
|
|
|
*/ |
151
|
|
|
public function postRemove(\ArrayObject $project) |
152
|
|
|
{ |
153
|
|
|
$pdo = new \PDO( |
154
|
|
|
$this->dialogProvider->logQuery( |
155
|
|
|
'pgsql:host=' . $project["dbserver"] . ";dbname=template1", |
156
|
|
|
array( |
157
|
|
|
"user" =>$this->app["config"]["postgresql"]["user"], |
158
|
|
|
"password" => $this->app["config"]["postgresql"]["password"] |
159
|
|
|
) |
160
|
|
|
), |
161
|
|
|
$this->app["config"]["postgresql"]["user"], |
162
|
|
|
$this->app["config"]["postgresql"]["password"] |
163
|
|
|
); |
164
|
|
|
$pdo->exec($this->dialogProvider->logQuery("drop user " . $project["dbuser"])); |
165
|
|
|
$pdo->exec($this->dialogProvider->logQuery("drop database " . $project["dbname"])); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param \ArrayObject $project |
170
|
|
|
* @param \SimpleXMLElement $config |
171
|
|
|
* @return \SimpleXMLElement |
172
|
|
|
*/ |
173
|
|
|
public function writeConfig(\ArrayObject $project, \SimpleXMLElement $config) |
174
|
|
|
{ |
175
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.dbuser', $project["dbuser"]); |
176
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.dbpass', $project["dbpass"]); |
177
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.dbname', $project["dbname"]); |
178
|
|
|
$config = $this->projectConfigProvider->addVar($config, 'project.dbserver', $project["dbserver"]); |
179
|
|
|
|
180
|
|
|
return $config; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string[] |
185
|
|
|
*/ |
186
|
|
|
public function dependsOn() |
187
|
|
|
{ |
188
|
|
|
return array("base"); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.