Skip to content

Commit

Permalink
Merge pull request #5774 from morozov/remove-schema-diff-save-mode
Browse files Browse the repository at this point in the history
Remove SchemaDiff::toSql() and SchemaDiff::toSaveSql()
  • Loading branch information
morozov authored Oct 18, 2022
2 parents 4a7598d + 142e138 commit 26b6351
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 92 deletions.
4 changes: 0 additions & 4 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@
<!--
TODO: remove in 4.0.0
-->
<referencedMethod name="Doctrine\DBAL\Schema\SchemaDiff::toSql"/>
<!--
TODO: remove in 4.0.0
-->
<referencedMethod name="Doctrine\DBAL\Schema\Comparator::diffTable"/>
</errorLevel>
</DeprecatedMethod>
Expand Down
40 changes: 39 additions & 1 deletion src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,10 +1102,48 @@ public function getCreateTemporaryTableSnippetSQL(): string
* Generates SQL statements that can be used to apply the diff.
*
* @return list<string>
*
* @throws Exception
*/
public function getAlterSchemaSQL(SchemaDiff $diff): array
{
return $diff->toSql($this);
$sql = [];

if ($this->supportsSchemas()) {
foreach ($diff->getCreatedSchemas() as $schema) {
$sql[] = $this->getCreateSchemaSQL($schema);
}
}

if ($this->supportsSequences()) {
foreach ($diff->getAlteredSequences() as $sequence) {
$sql[] = $this->getAlterSequenceSQL($sequence);
}

foreach ($diff->getDroppedSequences() as $sequence) {
$sql[] = $this->getDropSequenceSQL($sequence->getQuotedName($this));
}

foreach ($diff->getCreatedSequences() as $sequence) {
$sql[] = $this->getCreateSequenceSQL($sequence);
}
}

$sql = array_merge(
$sql,
$this->getCreateTablesSQL(
$diff->getCreatedTables(),
),
$this->getDropTablesSQL(
$diff->getDroppedTables(),
),
);

foreach ($diff->getAlteredTables() as $tableDiff) {
$sql = array_merge($sql, $this->getAlterTableSQL($tableDiff));
}

return $sql;
}

/**
Expand Down
87 changes: 0 additions & 87 deletions src/Schema/SchemaDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@

namespace Doctrine\DBAL\Schema;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\Deprecation;

use function array_filter;
use function array_merge;
use function array_values;
use function count;

/**
Expand Down Expand Up @@ -111,86 +106,4 @@ public function isEmpty(): bool
&& count($this->alteredSequences) === 0
&& count($this->droppedSequences) === 0;
}

/**
* The to save sql mode ensures that the following things don't happen:
*
* 1. Tables are deleted
* 2. Sequences are deleted
* 3. Foreign Keys which reference tables that would otherwise be deleted.
*
* This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
*
* @deprecated
*
* @return list<string>
*/
public function toSaveSql(AbstractPlatform $platform): array
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5766',
'%s is deprecated.',
__METHOD__,
);

return $this->_toSql($platform, true);
}

/**
* @deprecated Use {@link AbstractPlatform::getAlterSchemaSQL()} instead.
*
* @return list<string>
*/
public function toSql(AbstractPlatform $platform): array
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5766',
'%s is deprecated. Use AbstractPlatform::getAlterSchemaSQL() instead.',
__METHOD__,
);

return $this->_toSql($platform, false);
}

/** @return list<string> */
protected function _toSql(AbstractPlatform $platform, bool $saveMode = false): array
{
$sql = [];

if ($platform->supportsSchemas()) {
foreach ($this->getCreatedSchemas() as $schema) {
$sql[] = $platform->getCreateSchemaSQL($schema);
}
}

if ($platform->supportsSequences()) {
foreach ($this->getAlteredSequences() as $sequence) {
$sql[] = $platform->getAlterSequenceSQL($sequence);
}

if ($saveMode === false) {
foreach ($this->getDroppedSequences() as $sequence) {
$sql[] = $platform->getDropSequenceSQL($sequence->getQuotedName($platform));
}
}

foreach ($this->getCreatedSequences() as $sequence) {
$sql[] = $platform->getCreateSequenceSQL($sequence);
}
}

$sql = array_merge($sql, $platform->getCreateTablesSQL(array_values($this->getCreatedTables())));

if ($saveMode === false) {
$sql = array_merge($sql, $platform->getDropTablesSQL(array_values($this->getDroppedTables())));
}

foreach ($this->getAlteredTables() as $tableDiff) {
$sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
}

return $sql;
}
}

0 comments on commit 26b6351

Please sign in to comment.