52 lines
1.7 KiB
PHP
52 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Migration;
|
|
|
|
use Ulmus\Adapter\AdapterInterface;
|
|
use Ulmus\Adapter\SqlFieldMapper;
|
|
use Ulmus\ConnectionAdapter;
|
|
use Ulmus\Entity\Mysql\Table;
|
|
use Ulmus\Repository;
|
|
|
|
trait SqlMigrationTrait
|
|
{
|
|
protected array $QUERY_REPLACE = [
|
|
'change' => "alter",
|
|
];
|
|
|
|
public function schemaTable(ConnectionAdapter $adapter, $databaseName, string $tableName) : null|object
|
|
{
|
|
return \Ulmus\Entity\InformationSchema\Table::repository(Repository::DEFAULT_ALIAS, $adapter)
|
|
->select(\Ulmus\Common\Sql::raw('this.*'))
|
|
->where($this->escapeIdentifier('table_schema', AdapterInterface::IDENTIFIER_FIELD), $databaseName)
|
|
->or($this->escapeIdentifier('table_catalog', AdapterInterface::IDENTIFIER_FIELD), $databaseName)
|
|
->loadOneFromField($this->escapeIdentifier('table_name', AdapterInterface::IDENTIFIER_FIELD), $tableName);
|
|
}
|
|
|
|
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
|
|
{
|
|
$mapper = new SqlFieldMapper($field);
|
|
|
|
return $typeOnly ? $mapper->type : $mapper->render();
|
|
}
|
|
|
|
public function generateAlterColumn(FieldDefinition $definition, array $field) : string|\Stringable
|
|
{
|
|
return implode(" ", array_filter([
|
|
strtoupper($field['action']),
|
|
$this->escapeIdentifier($definition->getSqlName(), AdapterInterface::IDENTIFIER_FIELD),
|
|
$definition->getSqlType(),
|
|
$definition->getSqlParams(),
|
|
]));
|
|
}
|
|
|
|
public function splitAlterQuery() : bool
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public function supportAlterColumnAction(string $action)
|
|
{
|
|
return in_array(strtolower($action), [ 'add', 'change' ]);
|
|
}
|
|
} |