1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace App\Migration; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\DBALException; |
9
|
|
|
use Doctrine\DBAL\Schema\Schema; |
10
|
|
|
use Doctrine\Migrations\AbstractMigration; |
11
|
|
|
use Psr\Log\LoggerInterface; |
12
|
|
|
|
13
|
|
|
abstract class AbstractMultiPlatformMigration extends AbstractMigration |
14
|
|
|
{ |
15
|
|
|
public const ADMIN_PW_LENGTH = 10; |
16
|
|
|
|
17
|
|
|
protected $permissions_updated = false; |
18
|
|
|
protected $admin_pw = ""; |
19
|
|
|
|
20
|
|
|
protected $logger; |
21
|
|
|
|
22
|
|
|
public function __construct(Connection $connection, LoggerInterface $logger) |
23
|
|
|
{ |
24
|
|
|
$this->logger = $logger; |
25
|
|
|
AbstractMigration::__construct($connection, $logger); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function up(Schema $schema): void |
29
|
|
|
{ |
30
|
|
|
$db_type = $this->connection->getDatabasePlatform()->getName(); |
31
|
|
|
|
32
|
|
|
switch ($db_type) { |
33
|
|
|
case 'mysql': |
34
|
|
|
$this->mySQLUp($schema); |
35
|
|
|
break; |
36
|
|
|
case 'sqlite': |
37
|
|
|
$this->sqLiteUp($schema); |
38
|
|
|
break; |
39
|
|
|
default: |
40
|
|
|
$this->abortIf(true, "Database type '$db_type' is not supported!"); |
41
|
|
|
break; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function down(Schema $schema): void |
46
|
|
|
{ |
47
|
|
|
$db_type = $this->connection->getDatabasePlatform()->getName(); |
48
|
|
|
|
49
|
|
|
switch ($db_type) { |
50
|
|
|
case 'mysql': |
51
|
|
|
$this->mySQLDown($schema); |
52
|
|
|
break; |
53
|
|
|
case 'sqlite': |
54
|
|
|
$this->sqLiteDown($schema); |
55
|
|
|
break; |
56
|
|
|
default: |
57
|
|
|
$this->abortIf(true, "Database type '$db_type' is not supported!"); |
58
|
|
|
break; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Gets the legacy Part-DB version number. Returns 0, if target database is not an legacy Part-DB database. |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
public function getOldDBVersion(): int |
67
|
|
|
{ |
68
|
|
|
if ($this->connection->getDatabasePlatform()->getName() !== "mysql") { |
69
|
|
|
//Old Part-DB version only supported MySQL therefore only |
70
|
|
|
return 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
return (int) $this->connection->fetchColumn("SELECT keyValue AS version FROM `internal` WHERE `keyName` = 'dbVersion'"); |
75
|
|
|
} catch (DBALException $dBALException) { |
76
|
|
|
//when the table was not found, we can proceed, because we have an empty DB! |
77
|
|
|
return 0; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns the hash of a new random password, created for the initial admin user, which can be written to DB. |
83
|
|
|
* The plaintext version of the password will be outputed to user after this migration. |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getInitalAdminPW(): string |
87
|
|
|
{ |
88
|
|
|
if (empty($this->admin_pw)) { |
89
|
|
|
if (!empty($_ENV['INITIAL_ADMIN_PW'])) { |
90
|
|
|
$this->admin_pw = $_ENV['INITIAL_ADMIN_PW']; |
91
|
|
|
} else { |
92
|
|
|
$this->admin_pw = substr(md5(random_bytes(10)), 0, static::ADMIN_PW_LENGTH); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//As we dont have access to container, just use the default PHP pw hash function |
97
|
|
|
return password_hash($this->admin_pw, PASSWORD_DEFAULT); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function printPermissionUpdateMessage(): void |
101
|
|
|
{ |
102
|
|
|
$this->permissions_updated = true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function postUp(Schema $schema): void |
106
|
|
|
{ |
107
|
|
|
parent::postUp($schema); |
108
|
|
|
$this->logger->warning('<question>[!!!] Permissions were updated! Please check if they fit your expectations!</question>'); |
109
|
|
|
|
110
|
|
|
if (!empty($this->admin_pw)) { |
111
|
|
|
$this->logger->warning(''); |
112
|
|
|
$this->logger->warning('<bg=yellow;fg=black>The initial password for the "admin" user is: ' . $this->admin_pw . '</>'); |
113
|
|
|
$this->logger->warning(''); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
abstract public function mySQLUp(Schema $schema): void; |
118
|
|
|
|
119
|
|
|
abstract public function mySQLDown(Schema $schema): void; |
120
|
|
|
|
121
|
|
|
abstract public function sqLiteUp(Schema $schema): void; |
122
|
|
|
|
123
|
|
|
abstract public function sqLiteDown(Schema $schema): void; |
124
|
|
|
} |