mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-23 10:18:56 +02:00
Updated to doctrine migrations 3.0.
This commit is contained in:
parent
c3d22556a0
commit
96acf0d858
16 changed files with 394 additions and 342 deletions
124
src/Migration/AbstractMultiPlatformMigration.php
Normal file
124
src/Migration/AbstractMultiPlatformMigration.php
Normal file
|
@ -0,0 +1,124 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace App\Migration;
|
||||
|
||||
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\Migrations\AbstractMigration;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
abstract class AbstractMultiPlatformMigration extends AbstractMigration
|
||||
{
|
||||
public const ADMIN_PW_LENGTH = 10;
|
||||
|
||||
protected $permissions_updated = false;
|
||||
protected $admin_pw = "";
|
||||
|
||||
protected $logger;
|
||||
|
||||
public function __construct(Connection $connection, LoggerInterface $logger)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
AbstractMigration::__construct($connection, $logger);
|
||||
}
|
||||
|
||||
public function up(Schema $schema): void
|
||||
{
|
||||
$db_type = $this->connection->getDatabasePlatform()->getName();
|
||||
|
||||
switch ($db_type) {
|
||||
case 'mysql':
|
||||
$this->mySQLUp($schema);
|
||||
break;
|
||||
case 'sqlite':
|
||||
$this->sqLiteUp($schema);
|
||||
break;
|
||||
default:
|
||||
$this->abortIf(true, "Database type '$db_type' is not supported!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public function down(Schema $schema): void
|
||||
{
|
||||
$db_type = $this->connection->getDatabasePlatform()->getName();
|
||||
|
||||
switch ($db_type) {
|
||||
case 'mysql':
|
||||
$this->mySQLDown($schema);
|
||||
break;
|
||||
case 'sqlite':
|
||||
$this->sqLiteDown($schema);
|
||||
break;
|
||||
default:
|
||||
$this->abortIf(true, "Database type '$db_type' is not supported!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the legacy Part-DB version number. Returns 0, if target database is not an legacy Part-DB database.
|
||||
* @return int
|
||||
*/
|
||||
public function getOldDBVersion(): int
|
||||
{
|
||||
if ($this->connection->getDatabasePlatform()->getName() !== "mysql") {
|
||||
//Old Part-DB version only supported MySQL therefore only
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
return (int) $this->connection->fetchColumn("SELECT keyValue AS version FROM `internal` WHERE `keyName` = 'dbVersion'");
|
||||
} catch (DBALException $dBALException) {
|
||||
//when the table was not found, we can proceed, because we have an empty DB!
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hash of a new random password, created for the initial admin user, which can be written to DB.
|
||||
* The plaintext version of the password will be outputed to user after this migration.
|
||||
* @return string
|
||||
*/
|
||||
public function getInitalAdminPW(): string
|
||||
{
|
||||
if (empty($this->admin_pw)) {
|
||||
if (!empty($_ENV['INITIAL_ADMIN_PW'])) {
|
||||
$this->admin_pw = $_ENV['INITIAL_ADMIN_PW'];
|
||||
} else {
|
||||
$this->admin_pw = substr(md5(random_bytes(10)), 0, static::ADMIN_PW_LENGTH);
|
||||
}
|
||||
}
|
||||
|
||||
//As we dont have access to container, just use the default PHP pw hash function
|
||||
return password_hash($this->admin_pw, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
public function printPermissionUpdateMessage(): void
|
||||
{
|
||||
$this->permissions_updated = true;
|
||||
}
|
||||
|
||||
public function postUp(Schema $schema): void
|
||||
{
|
||||
parent::postUp($schema);
|
||||
$this->logger->warning('<question>[!!!] Permissions were updated! Please check if they fit your expectations!</question>');
|
||||
|
||||
if (!empty($this->admin_pw)) {
|
||||
$this->logger->warning('');
|
||||
$this->logger->warning('<bg=yellow;fg=black>The initial password for the "admin" user is: ' . $this->admin_pw . '</>');
|
||||
$this->logger->warning('');
|
||||
}
|
||||
}
|
||||
|
||||
abstract public function mySQLUp(Schema $schema): void;
|
||||
|
||||
abstract public function mySQLDown(Schema $schema): void;
|
||||
|
||||
abstract public function sqLiteUp(Schema $schema): void;
|
||||
|
||||
abstract public function sqLiteDown(Schema $schema): void;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue