Skip to content

Commit

Permalink
Update buildCodeFromSql to match the one from doctrine migration diff…
Browse files Browse the repository at this point in the history
… command
  • Loading branch information
Arnaud GOULPEAU​​ committed Apr 27, 2016
1 parent 5ad8e5d commit e3c42e7
Showing 1 changed file with 48 additions and 30 deletions.
78 changes: 48 additions & 30 deletions Command/DiffFileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,67 @@ protected function saveCurrentSchema($configuration, $schema, $version)
file_put_contents($filepath, serialize($schema));
}

/**
*
* @return type
*/
protected function getSchemaProvider()
{
if (!$this->schemaProvider) {
$this->schemaProvider = new OrmSchemaProvider($this->getHelper('entityManager')->getEntityManager());
}

return $this->schemaProvider;
}

/**
* Resolve a table name from its fully qualified name. The `$name` argument
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
* the table name from that.
*
* @param string $name
* @return string
*/
protected function resolveTableName($name)
{
$pos = strpos($name, '.');

return false === $pos ? $name : substr($name, $pos + 1);
}

/**
*
* @param Configuration $configuration
* @param array $sql
* @param type $formatted
* @param type $lineLength
* @return type
* @throws \InvalidArgumentException
*/
protected function buildCodeFromSql(Configuration $configuration, array $sql)
private function buildCodeFromSql(Configuration $configuration, array $sql, $formatted = false, $lineLength = 120)
{
$currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName();
$code = [];
foreach ($sql as $query) {
if (stripos($query, $configuration->getMigrationsTableName()) !== false) {
continue;
}

if ($formatted) {
if (!class_exists('\SqlFormatter')) {
throw new \InvalidArgumentException(
'The "--formatted" option can only be used if the sql formatter is installed.'.'Please run "composer require jdorn/sql-formatter".'
);
}

$maxLength = $lineLength - 18 - 8; // max - php code length - indentation

if (strlen($query) > $maxLength) {
$query = \SqlFormatter::format($query, false);
}
}

$code[] = sprintf("\$this->addSql(%s);", var_export($query, true));
}

Expand All @@ -152,33 +199,4 @@ protected function buildCodeFromSql(Configuration $configuration, array $sql)

return implode("\n", $code);
}

/**
*
* @return type
*/
protected function getSchemaProvider()
{
if (!$this->schemaProvider) {
$this->schemaProvider = new OrmSchemaProvider($this->getHelper('entityManager')->getEntityManager());
}

return $this->schemaProvider;
}

/**
* Resolve a table name from its fully qualified name. The `$name` argument
* comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return
* a namespaced name with the form `{namespace}.{tableName}`. This extracts
* the table name from that.
*
* @param string $name
* @return string
*/
protected function resolveTableName($name)
{
$pos = strpos($name, '.');

return false === $pos ? $name : substr($name, $pos + 1);
}
}

0 comments on commit e3c42e7

Please sign in to comment.