Skip to content

Commit

Permalink
Replace deprecated saveToSql with custom method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Sep 2, 2025
1 parent 1751f2b commit 8dcf207
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions app/src/Lib/Util/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

use Cake\Console\ConsoleIo;

use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaDiff;
Expand Down Expand Up @@ -346,8 +348,11 @@ protected function processSchema(
// schema file).
$comparator = new Comparator();
$schemaDiff = $comparator->compareSchemas($curSchema, $schema);

$diffSql = $schemaDiff->toSaveSql($this->conn->getDatabasePlatform());

$diffSql = $this->getAlterSchemaSqlSaveMode(
$this->conn->getDatabasePlatform(),
$schemaDiff
);

// We don't start a transaction since in general we always want to move to
// the desired state, and if we fail in flight it's probably a bug that
Expand Down Expand Up @@ -375,7 +380,7 @@ protected function processSchema(

if($qualifiedTable !== null) {
// We will skip this index if it's bound to a constraint
if($this->io) $this->io->out('Skipping index drop since it is bound to a constraint');
if($this->io) $this->io->out('Skipping index:' . $m[1] . ' drop since it is bound to a constraint');
continue;
}
}
Expand Down Expand Up @@ -451,4 +456,54 @@ protected function resolvePgConstraintTable(string $constraintName): ?string {
return null;
}
}
}


/**
* Generate non-destructive (save-mode) schema SQL.
*
* Equivalent to SchemaDiff::_toSql($platform, true).
*
* - Does NOT drop tables
* - Does NOT drop sequences
* - Does NOT drop orphaned FKs
*
* @param AbstractPlatform $platform Database platform abstraction
* @param SchemaDiff $diff Schema differences to generate SQL for
* @return array Array of SQL statements to execute
* @throws Exception
* @since COmanage Registry v5.2.0
*/
protected function getAlterSchemaSqlSaveMode(
AbstractPlatform $platform,
SchemaDiff $diff
): array {
$sql = [];

// 1) Schemas to create
if ($platform->supportsSchemas()) {
foreach ($diff->getCreatedSchemas() as $schema) {
$sql[] = $platform->getCreateSchemaSQL($schema);
}
}

// 2) Sequences (alter + create; no drops in save mode)
if ($platform->supportsSequences()) {
foreach ($diff->getAlteredSequences() as $sequence) {
$sql[] = $platform->getAlterSequenceSQL($sequence);
}
foreach ($diff->getCreatedSequences() as $sequence) {
$sql[] = $platform->getCreateSequenceSQL($sequence);
}
}

// 3) Tables (create only; no drops in save mode)
$sql = array_merge($sql, $platform->getCreateTablesSQL($diff->getCreatedTables()));

// 4) Alter existing tables (may emit FK drops/adds as required for alterations)
foreach ($diff->getAlteredTables() as $tableDiff) {
$sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
}

return $sql;
}
}

0 comments on commit 8dcf207

Please sign in to comment.