Generate a diff changelog from SchemaDiff object.

@param SchemaDiff $schemaDiff
@param LiquibaseOutput|LiquibaseOutputOptions|null $output
@return \DOMDocument|mixed
339077
public function diffChangeLogFromSchemaDiff(SchemaDiff $schemaDiff, $output = null)
    {
        $output = $this->sanitizeOutputParameter($output);

        $output->started($this->em);

        foreach ($schemaDiff->newNamespaces as $newNamespace) {
            $output->createSchema($newNamespace);
        }

        foreach ($schemaDiff->orphanedForeignKeys as $orphanedForeignKey) {
            $output->dropForeignKey($orphanedForeignKey, $orphanedForeignKey->getLocalTable());
        }

        foreach ($schemaDiff->changedSequences as $sequence) {
            $output->alterSequence($sequence);
        }

        foreach ($schemaDiff->removedSequences as $sequence) {
            $output->dropSequence($sequence);
        }

        foreach ($schemaDiff->newSequences as $sequence) {
            $output->createSequence($sequence);
        }

        foreach ($schemaDiff->newTables as $table) {
            $output->createTable($table);
            
            foreach ($table->getForeignKeys() as $foreignKey) {
                $output->createForeignKey($foreignKey, $table);
            }
        }

        foreach ($schemaDiff->removedTables as $table) {
            $output->dropTable($table);
        }

        foreach ($schemaDiff->changedTables as $tableDiff) {
            $output->alterTable($tableDiff);
        }

        $output->terminated();

        return $output->getResult();
    }
1255804
public function getModifyColumnDDL(ColumnDiff $columnDiff)
    {
        $ret = '';
        $changedProperties = $columnDiff->getChangedProperties();

        $fromColumn = $columnDiff->getFromColumn();
        $toColumn = clone $columnDiff->getToColumn();

        $fromTable = $fromColumn->getTable();
        $table = $toColumn->getTable();

        $colName = $this->quoteIdentifier($toColumn->getName());

        $pattern = "
ALTER TABLE %s ALTER COLUMN %s;
";

        if (isset($changedProperties['autoIncrement'])) {
            $tableName = $table->getName();
            $colPlainName = $toColumn->getName();
            $seqName = "{$tableName}_{$colPlainName}_seq";

            if ($toColumn->isAutoIncrement() && $table && $table->getIdMethodParameters() == null) {

                $defaultValue = "nextval('$seqName'::regclass)";
                $toColumn->setDefaultValue($defaultValue);
                $changedProperties['defaultValueValue'] = [null, $defaultValue];

                //add sequence
                if (!$fromTable->getDatabase()->hasSequence($seqName)) {
                    $this->createOrDropSequences .= sprintf("
CREATE SEQUENCE %s;
",
                        $seqName
                    );
                    $fromTable->getDatabase()->addSequence($seqName);
                }
            }

            if (!$toColumn->isAutoIncrement() && $fromColumn->isAutoIncrement()) {
                $changedProperties['defaultValueValue'] = [$fromColumn->getDefaultValueString(), null];
                $toColumn->setDefaultValue(null);

                //remove sequence
                if ($fromTable->getDatabase()->hasSequence($seqName)) {
                    $this->createOrDropSequences .= sprintf("
DROP SEQUENCE %s CASCADE;
",
                        $seqName
                    );
                    $fromTable->getDatabase()->removeSequence($seqName);
                }
            }
        }

        if (isset($changedProperties['size']) || isset($changedProperties['type']) || isset($changedProperties['sqlType']) || isset($changedProperties['scale'])) {

            $sqlType = $toColumn->getDomain()->getSqlType();

            if ($this->hasSize($sqlType) && $toColumn->isDefaultSqlType($this)) {
                if ($this->isNumber($sqlType)) {
                    if ('NUMERIC' === strtoupper($sqlType)) {
                        $sqlType .= $toColumn->getSizeDefinition();
                    }
                } else {
                    $sqlType .= $toColumn->getSizeDefinition();
                }
            }

            if ($using = $this->getUsingCast($fromColumn, $toColumn)) {
                $sqlType .= $using;
            }
            $ret .= sprintf($pattern,
                $this->quoteIdentifier($table->getName()),
                $colName . ' TYPE ' . $sqlType
            );
        }

        if (isset($changedProperties['defaultValueValue'])) {
            $property = $changedProperties['defaultValueValue'];
            if ($property[0] !== null && $property[1] === null) {
                $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' DROP DEFAULT');
            } else {
                $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . ' SET ' . $this->getColumnDefaultValueDDL($toColumn));
            }
        }

        if (isset($changedProperties['notNull'])) {
            $property = $changedProperties['notNull'];
            $notNull = ' DROP NOT NULL';
            if ($property[1]) {
                $notNull = ' SET NOT NULL';
            }
            $ret .= sprintf($pattern, $this->quoteIdentifier($table->getName()), $colName . $notNull);
        }

        return $ret;
    }
1368623
private function alterTable($tableDiff)
    {
        if ($this->getSqlStatements) {
            $this->addSql($this->getPlatform()->getAlterTableSQL($tableDiff));
        } else {
            $this->schemaManager->alterTable($tableDiff);
        }
    }
1203215
private function getPreAlterTableAlterIndexForeignKeySQL(TableDiff $diff)
    {
        $sql   = [];
        $table = $diff->getName($this)->getQuotedName($this);

        foreach ($diff->changedIndexes as $changedIndex) {
            // Changed primary key
            if (! $changedIndex->isPrimary() || ! ($diff->fromTable instanceof Table)) {
                continue;
            }

            foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) {
                $column = $diff->fromTable->getColumn($columnName);

                // Check if an autoincrement column was dropped from the primary key.
                if (! $column->getAutoincrement() || in_array($columnName, $changedIndex->getColumns())) {
                    continue;
                }

                // The autoincrement attribute needs to be removed from the dropped column
                // before we can drop and recreate the primary key.
                $column->setAutoincrement(false);

                $sql[] = 'ALTER TABLE ' . $table . ' MODIFY ' .
                    $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());

                // Restore the autoincrement attribute as it might be needed later on
                // by other parts of the table alteration.
                $column->setAutoincrement(true);
            }
        }

        return $sql;
    }
41777
protected function printDbBuildVersion(OutputInterface $output) : void
    {
        $output->writeln(
            sprintf(
                '<info>Reference Database Version</info> => %s%s',
                $this->getApplication()->getDbVersions()['build.version'],
                PHP_EOL

            )
        );
    }
149766
function diff( $old, $new )
    {
        $datatype = $this->dataType();
        $result = $datatype->diff( $old, $new );
        return $result;
    }
1147546
public function createSchema($outputPathAndFilename = null)
    {
        $schemaTool = new SchemaTool($this->entityManager);
        $allMetaData = $this->entityManager->getMetadataFactory()->getAllMetadata();
        if ($outputPathAndFilename === null) {
            $schemaTool->createSchema($allMetaData);
        } else {
            $createSchemaSqlStatements = $schemaTool->getCreateSchemaSql($allMetaData);
            file_put_contents($outputPathAndFilename, implode(PHP_EOL, $createSchemaSqlStatements));
        }
    }
1209356
private function getChangedColumns($tableName, TableDiff $diff)
    {
        /** @var $col \Doctrine\DBAL\Schema\ColumnDiff */
        foreach ($diff->changedColumns as $col) {
            $this->addMessage($tableName, sprintf('invalid column `%s`', $col->oldColumnName));
        }
    }
790866
protected function outputDiffTable(array $change_list)
    {
        $header = [
            $this->trans('commands.config.diff.table.headers.collection'),
            $this->trans('commands.config.diff.table.headers.config-name'),
            $this->trans('commands.config.diff.table.headers.operation'),
        ];
        $rows = [];
        foreach ($change_list as $collection => $changes) {
            foreach ($changes as $operation => $configs) {
                foreach ($configs as $config) {
                    $rows[] = [
                        $collection,
                        $config,
                        sprintf(self::$operationColours[$operation], $operation),
                    ];
                }
            }
        }
        $this->getIo()->table($header, $rows);
    }
639740
public function getDiffSql(\Doctrine\DBAL\Schema\Schema $fromSchema, \Doctrine\DBAL\Schema\Schema $toSchema)
    {
        $schemaDiff = $this->getComparator()->compare($fromSchema, $toSchema);

        return $this->toSql($schemaDiff); //$schemaDiff->toSql($this->getPlatform());
    }
85449
private function diffTableQueries($table, &$schema)
    {
        // first order queries used as output array
        $foQueries = [];

        // second order queries used as output array
        $soQueries = [];

        // describe table get current table description
        $fields = $this->descTable($table);

        // test field definition
        foreach ($schema as $field => &$attributes) {
            $this->diffTableField(
                $table,
                $field,
                $attributes,
                $fields,
                $foQueries,
                $soQueries
            );
        }

        return $this->diffTableMergeQueries(
            $table,
            $fields,
            $foQueries,
            $soQueries
        );
    }
659794
public function checkDiff($tableName, TableDiff $diff)
    {
        $this->diffs[$tableName] = $diff;

        // Adds
        $this->getAddedColumns($tableName, $diff);
        $this->getAddedIndexes($tableName, $diff);
        $this->getAddedForeignKeys($tableName, $diff);

        // Changes
        $this->getChangedColumns($tableName, $diff);
        $this->getChangedIndexes($tableName, $diff);
        $this->getChangedForeignKeys($tableName, $diff);

        // Renames
        $this->getRenamedColumns($tableName, $diff);

        // Removes
        $this->getRemovedColumns($tableName, $diff);
        $this->getRemovedIndexes($tableName, $diff);
        $this->getRemovedForeignKeys($tableName, $diff);

        if (count($diff->removedColumns) > 0) {
            $hint = sprintf(
                'The following fields in the `%s` table are not defined in your configuration. You can safely delete them manually if they are no longer needed: `%s`',
                $tableName,
                implode('`, `', array_keys($diff->removedColumns))
            );
            $this->hints[] = $hint;
        }
    }
774985
private function collectTables(SchemaDiff $schemaDiff)
    {
        foreach ($schemaDiff->getCreatedTables() as $table) {
            $this->createTableSQLCollector->collect($table);
        }

        foreach ($schemaDiff->getDroppedTables() as $table) {
            $this->dropTableSQLCollector->collect($table);
        }

        foreach ($schemaDiff->getAlteredTables() as $tableDiff) {
            $this->alterTableSQLCollector->collect($tableDiff);
        }
    }
363603
public function getModifyDatabaseDDL(PropelDatabaseDiff $databaseDiff)
    {
        $ret = $this->getBeginDDL();

        foreach ($databaseDiff->getRemovedTables() as $table) {
            $ret .= $this->getDropTableDDL($table);
        }

        foreach ($databaseDiff->getRenamedTables() as $fromTableName => $toTableName) {
            $ret .= $this->getRenameTableDDL($fromTableName, $toTableName);
        }

        foreach ($databaseDiff->getAddedTables() as $table) {
            $ret .= $this->getAddTableDDL($table);
            $ret .= $this->getAddIndicesDDL($table);
        }

        foreach ($databaseDiff->getModifiedTables() as $tableDiff) {
            $ret .= $this->getModifyTableDDL($tableDiff);
        }

        foreach ($databaseDiff->getAddedTables() as $table) {
            $ret .= $this->getAddForeignKeysDDL($table);
        }

        $ret .= $this->getEndDDL();

        return $ret;
    }
1021651
private function collectSequences(SchemaDiff $schemaDiff)
    {
        foreach ($schemaDiff->getCreatedSequences() as $sequence) {
            $this->createSequenceQueries = array_merge(
                $this->createSequenceQueries,
                $this->platform->getCreateSequenceSQLQueries($sequence)
            );
        }

        foreach ($schemaDiff->getDroppedSequences() as $sequence) {
            $this->dropSequenceQueries = array_merge(
                $this->dropSequenceQueries,
                $this->platform->getDropSequenceSQLQueries($sequence)
            );
        }
    }
706642
public static void dumpLdif(LdifOut lo, DirRecord rec) throws NamingException {
    if (rec == null) {
      throw new NamingException("dumpLdif: No record supplied");
    }

    String dn = rec.getDn();
    if (dn == null) {
      throw new NamingException("Unable to get dn");
    }

    lo.out("dn: " + dn);

    int ctype = rec.getChangeType();

    if (!rec.getIsContent()) {
      // Emit a changetype attribute
      lo.out("changeType: " + LdifRecord.changeTypes[ctype]);
    }

    if ((rec.getIsContent()) || (ctype == DirRecord.changeTypeAdd)) {
      Attributes as = rec.getAttributes();

      if (as == null) throw new NamingException("No attributes");

      Enumeration e = as.getAll();

      while (e.hasMoreElements()) {
        dumpAttr(lo, (Attribute)e.nextElement());
      } // while
    } else if (ctype == DirRecord.changeTypeDelete) {
      lo.out("changetype: delete");
    } else {
      lo.out("changetype: modify");

      // Dump changes
      ModificationItem[] mods = rec.getMods();

      if (mods == null) {
        lo.out("# Invalid record - no mods");
      } else {
        for (int i = 0; i < mods.length; i ++) {
          ModificationItem m = mods[i];
          int op = m.getModificationOp();
          Attribute a = m.getAttribute();
          String aid = a.getID();

          if (op == DirContext.ADD_ATTRIBUTE) {
            lo.out("add: " + aid);
          } else if (op == DirContext.REPLACE_ATTRIBUTE) {
            lo.out("replace: " + aid);
          } else if (op == DirContext.REMOVE_ATTRIBUTE) {
            lo.out("delete: " + aid);
          } else {
            lo.out("# Invalid record - bad mod op " + op);
          }
          dumpAttr(lo, a);
        }
      }
      lo.out("-");
    }

    lo.out(""); // null terminator
  }
1265152
public function getModifyTableForeignKeysDDL(TableDiff $tableDiff)
    {
        $ret = '';

        foreach ($tableDiff->getRemovedFks() as $fk) {
            $ret .= $this->getDropForeignKeyDDL($fk);
        }

        foreach ($tableDiff->getAddedFks() as $fk) {
            $ret .= $this->getAddForeignKeyDDL($fk);
        }

        foreach ($tableDiff->getModifiedFks() as $fkModification) {
            list($fromFk, $toFk) = $fkModification;
            $ret .= $this->getDropForeignKeyDDL($fromFk);
            $ret .= $this->getAddForeignKeyDDL($toFk);
        }

        return $ret;
    }