123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace OC\DB;
- use Doctrine\DBAL\Connection;
- use Doctrine\DBAL\Exception;
- use Doctrine\DBAL\Platforms\MySQLPlatform;
- use Doctrine\DBAL\Schema\AbstractAsset;
- use Doctrine\DBAL\Schema\Schema;
- use Doctrine\DBAL\Schema\SchemaDiff;
- use Doctrine\DBAL\Types\StringType;
- use Doctrine\DBAL\Types\Type;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\IConfig;
- use function preg_match;
- class Migrator {
-
- protected $connection;
-
- protected $config;
- private ?IEventDispatcher $dispatcher;
-
- private $noEmit = false;
- public function __construct(Connection $connection,
- IConfig $config,
- ?IEventDispatcher $dispatcher = null) {
- $this->connection = $connection;
- $this->config = $config;
- $this->dispatcher = $dispatcher;
- }
-
- public function migrate(Schema $targetSchema) {
- $this->noEmit = true;
- $this->applySchema($targetSchema);
- }
-
- public function generateChangeScript(Schema $targetSchema) {
- $schemaDiff = $this->getDiff($targetSchema, $this->connection);
- $script = '';
- $sqls = $this->connection->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff);
- foreach ($sqls as $sql) {
- $script .= $this->convertStatementToScript($sql);
- }
- return $script;
- }
-
- public function createSchema() {
- $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
-
- $filterExpression = $this->getFilterExpression();
- if ($asset instanceof AbstractAsset) {
- return preg_match($filterExpression, $asset->getName()) === 1;
- }
- return preg_match($filterExpression, $asset) === 1;
- });
- return $this->connection->createSchemaManager()->introspectSchema();
- }
-
- protected function getDiff(Schema $targetSchema, Connection $connection) {
-
-
-
- foreach ($targetSchema->getTables() as $table) {
- foreach ($table->getColumns() as $column) {
- if ($column->getType() instanceof StringType) {
- if ($column->getLength() > 4000) {
- $column->setType(Type::getType('text'));
- $column->setLength(null);
- }
- }
- }
- }
- $this->connection->getConfiguration()->setSchemaAssetsFilter(function ($asset) {
-
- $filterExpression = $this->getFilterExpression();
- if ($asset instanceof AbstractAsset) {
- return preg_match($filterExpression, $asset->getName()) === 1;
- }
- return preg_match($filterExpression, $asset) === 1;
- });
- $sourceSchema = $connection->createSchemaManager()->introspectSchema();
-
- foreach ($sourceSchema->getTables() as $table) {
- if (!$targetSchema->hasTable($table->getName())) {
- $sourceSchema->dropTable($table->getName());
- }
- }
-
- foreach ($sourceSchema->getSequences() as $table) {
- if (!$targetSchema->hasSequence($table->getName())) {
- $sourceSchema->dropSequence($table->getName());
- }
- }
- $comparator = $connection->createSchemaManager()->createComparator();
- return $comparator->compareSchemas($sourceSchema, $targetSchema);
- }
-
- protected function applySchema(Schema $targetSchema, ?Connection $connection = null) {
- if (is_null($connection)) {
- $connection = $this->connection;
- }
- $schemaDiff = $this->getDiff($targetSchema, $connection);
- if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
- $connection->beginTransaction();
- }
- $sqls = $connection->getDatabasePlatform()->getAlterSchemaSQL($schemaDiff);
- $step = 0;
- foreach ($sqls as $sql) {
- $this->emit($sql, $step++, count($sqls));
- $connection->executeStatement($sql);
- }
- if (!$connection->getDatabasePlatform() instanceof MySQLPlatform) {
- $connection->commit();
- }
- }
-
- protected function convertStatementToScript($statement) {
- $script = $statement . ';';
- $script .= PHP_EOL;
- $script .= PHP_EOL;
- return $script;
- }
- protected function getFilterExpression() {
- return '/^' . preg_quote($this->config->getSystemValueString('dbtableprefix', 'oc_'), '/') . '/';
- }
- protected function emit(string $sql, int $step, int $max): void {
- if ($this->noEmit) {
- return;
- }
- if (is_null($this->dispatcher)) {
- return;
- }
- $this->dispatcher->dispatchTyped(new MigratorExecuteSqlEvent($sql, $step, $max));
- }
- }
|