Files
kimai2/src/Doctrine/AbstractMigration.php
Kevin Papst 3ac72a5c89 enhanced plugin support (#634)
- refactored admin controller and templates
- plugin support for entity actions
- changed column mail to email in customer table
- refactored theme events
2019-03-11 14:46:30 +01:00

143 lines
3.3 KiB
PHP

<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Doctrine;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration as BaseAbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Base class for all Doctrine migrations.
*/
abstract class AbstractMigration extends BaseAbstractMigration implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
/**
* @param ContainerInterface $container
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* @return ContainerInterface
*/
public function getContainer()
{
return $this->container;
}
/**
* @param string $name
* @return string
*/
protected function getTableName($name)
{
return getenv('DATABASE_PREFIX') . $name;
}
/**
* @param Schema $schema
* @throws DBALException
*/
public function preUp(Schema $schema): void
{
$this->abortIfPlatformNotSupported();
}
/**
* @param Schema $schema
* @throws DBALException
*/
public function preDown(Schema $schema): void
{
$this->abortIfPlatformNotSupported();
}
/**
* Abort the migration is the current platform is not supported.
*
* @throws DBALException
*/
protected function abortIfPlatformNotSupported()
{
$platform = $this->getPlatform();
if (!in_array($platform, ['sqlite', 'mysql'])) {
$this->abortIf(true, 'Unsupported database platform: ' . $platform);
}
}
/**
* @return bool
* @throws DBALException
*/
protected function isPlatformSqlite()
{
return ($this->getPlatform() === 'sqlite');
}
/**
* @return bool
* @throws DBALException
*/
protected function isPlatformMysql()
{
return ($this->getPlatform() === 'mysql');
}
/**
* @return string
* @throws DBALException
*/
protected function getPlatform()
{
return $this->connection->getDatabasePlatform()->getName();
}
/**
* Call me like this:
* $schema = $this->getClassMetaData(User::class);
*
* @param string $entityName
* @return ClassMetadata
*/
protected function getClassMetaData($entityName)
{
$em = $this->getContainer()->get('doctrine')->getManager();
return $em->getClassMetadata($entityName);
}
/**
* we do it via addSql instead of $schema->getTable($users)->dropIndex()
* otherwise the commands will be executed as last ones.
*
* @param string $indexName
* @param string $tableName
* @throws DBALException
*/
protected function addSqlDropIndex($indexName, $tableName)
{
$dropSql = 'DROP INDEX ' . $indexName;
if (!$this->isPlatformSqlite()) {
$dropSql .= ' ON ' . $tableName;
}
$this->addSql($dropSql);
}
}