blob: 54a113972a92ae064fa3ab54b370a864f655656c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?php
/* Icinga Web 2 | (c) 2023 Icinga GmbH | GPLv2+ */
namespace Icinga\Application\Hook\Common;
use ipl\Sql\Connection;
use RuntimeException;
class DbMigrationStep
{
/** @var string The sql script version the queries are loaded from */
protected $version;
/** @var string */
protected $scriptPath;
/** @var ?string */
protected $description;
/** @var ?string */
protected $lastState;
public function __construct(string $version, string $scriptPath)
{
$this->scriptPath = $scriptPath;
$this->version = $version;
}
/**
* Get the sql script version the queries are loaded from
*
* @return string
*/
public function getVersion(): string
{
return $this->version;
}
/**
* Get upgrade script relative path name
*
* @return string
*/
public function getScriptPath(): string
{
return $this->scriptPath;
}
/**
* Get the description of this database migration if any
*
* @return ?string
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* Set the description of this database migration
*
* @param ?string $description
*
* @return DbMigrationStep
*/
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* Get the last error message of this hook if any
*
* @return ?string
*/
public function getLastState(): ?string
{
return $this->lastState;
}
/**
* Set the last error message
*
* @param ?string $message
*
* @return $this
*/
public function setLastState(?string $message): self
{
$this->lastState = $message;
return $this;
}
/**
* Perform the sql migration
*
* @param Connection $conn
*
* @return $this
*
* @throws RuntimeException Throws an error in case of any database errors or when there is nothing to migrate
*/
public function apply(Connection $conn): self
{
$statements = @file_get_contents($this->getScriptPath());
if ($statements === false) {
throw new RuntimeException(sprintf('Cannot load upgrade script %s', $this->getScriptPath()));
}
if (empty($statements)) {
throw new RuntimeException('Nothing to migrate');
}
if (preg_match('/\s*delimiter\s*(\S+)\s*$/im', $statements, $matches)) {
/** @var string $statements */
$statements = preg_replace('/\s*delimiter\s*(\S+)\s*$/im', '', $statements);
/** @var string $statements */
$statements = preg_replace('/' . preg_quote($matches[1], '/') . '$/m', ';', $statements);
}
$conn->exec($statements);
return $this;
}
}
|