2018-06-14 11:43:09 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Wallabag\Doctrine;
|
2018-06-14 11:43:09 +00:00
|
|
|
|
2023-08-05 17:35:09 +00:00
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
|
2018-06-14 11:43:09 +00:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
2018-10-24 19:02:35 +00:00
|
|
|
use Doctrine\Migrations\AbstractMigration;
|
2018-06-14 11:43:09 +00:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
|
|
|
|
|
|
abstract class WallabagMigration extends AbstractMigration implements ContainerAwareInterface
|
|
|
|
{
|
2022-12-13 09:26:51 +00:00
|
|
|
public const UN_ESCAPED_TABLE = true;
|
2018-06-14 12:15:07 +00:00
|
|
|
|
2018-06-14 11:43:09 +00:00
|
|
|
/**
|
|
|
|
* @var ContainerInterface
|
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
// because there are declared as abstract in `AbstractMigration` we need to delarer here too
|
2022-12-14 13:36:29 +00:00
|
|
|
public function up(Schema $schema): void
|
2018-06-14 11:43:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-14 13:36:29 +00:00
|
|
|
public function down(Schema $schema): void
|
2018-06-14 11:43:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-02-19 08:31:30 +00:00
|
|
|
public function setContainer(?ContainerInterface $container = null)
|
2018-06-14 11:43:09 +00:00
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
2022-01-31 08:27:20 +00:00
|
|
|
/**
|
|
|
|
* @todo remove when upgrading DoctrineMigration (only needed for PHP 8)
|
|
|
|
*
|
|
|
|
* @see https://github.com/doctrine/DoctrineMigrationsBundle/issues/393
|
|
|
|
*/
|
|
|
|
public function isTransactional(): bool
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-10 16:30:29 +00:00
|
|
|
protected function getTablePrefix(): string
|
|
|
|
{
|
|
|
|
return (string) $this->container->getParameter('database_table_prefix');
|
|
|
|
}
|
|
|
|
|
2018-06-14 12:15:07 +00:00
|
|
|
protected function getTable($tableName, $unEscaped = false)
|
2018-06-14 11:43:09 +00:00
|
|
|
{
|
|
|
|
$table = $this->container->getParameter('database_table_prefix') . $tableName;
|
|
|
|
|
2018-06-14 12:15:07 +00:00
|
|
|
if (self::UN_ESCAPED_TABLE === $unEscaped) {
|
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
2018-06-14 11:43:09 +00:00
|
|
|
// escape table name is handled using " on postgresql
|
2023-08-05 17:35:09 +00:00
|
|
|
if ($this->connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
|
2018-06-14 11:43:09 +00:00
|
|
|
return '"' . $table . '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
// return escaped table
|
|
|
|
return '`' . $table . '`';
|
|
|
|
}
|
2024-03-10 16:30:29 +00:00
|
|
|
|
|
|
|
protected function getForeignKeyName(string $tableName, string $foreignColumnName): string
|
|
|
|
{
|
|
|
|
return $this->generateIdentifierName([$this->getTable($tableName, true), $foreignColumnName], 'fk');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getIndexName(string $tableName, $indexedColumnNames): string
|
|
|
|
{
|
|
|
|
$indexedColumnNames = (array) $indexedColumnNames;
|
|
|
|
|
|
|
|
return $this->generateIdentifierName(array_merge([$this->getTable($tableName, true)], $indexedColumnNames), 'idx');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getUniqueIndexName(string $tableName, string $indexedColumnName): string
|
|
|
|
{
|
|
|
|
return $this->generateIdentifierName([$this->getTable($tableName, true), $indexedColumnName], 'uniq');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \Doctrine\DBAL\Schema\AbstractAsset::_generateIdentifierName
|
|
|
|
*
|
|
|
|
* @param string[] $columnNames
|
|
|
|
*/
|
|
|
|
protected function generateIdentifierName(array $columnNames, string $prefix = ''): string
|
|
|
|
{
|
|
|
|
$hash = implode('', array_map(static function ($column): string {
|
|
|
|
return dechex(crc32($column));
|
|
|
|
}, $columnNames));
|
|
|
|
|
|
|
|
return strtoupper(substr($prefix . '_' . $hash, 0, $this->platform->getMaxIdentifierLength()));
|
|
|
|
}
|
2018-06-14 11:43:09 +00:00
|
|
|
}
|