<commit_before><?php

namespace Oro\Bundle\UserBundle\Migrations\Schema\v1_15;

use Psr\Log\LoggerInterface;

use Oro\Bundle\EntityBundle\ORM\DatabaseDriverInterface;
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;

class UpdateEmailOriginTableQuery extends ParametrizedMigrationQuery
{
    /**
     * {@inheritDoc}
     */
    public function getDescription()
    {
        $logger = new ArrayLogger();
        $this->doExecute($logger, true);

        return $logger->getMessages();
    }

    /**
     * {@inheritDoc}
     */
    public function execute(LoggerInterface $logger)
    {
        $this->doExecute($logger);
    }

    /**
     * @param LoggerInterface $logger
     * @param bool $dryRun
     *
     * @throws \Doctrine\DBAL\DBALException
     */
    protected function doExecute(LoggerInterface $logger, $dryRun = false)
    {

        $dbDriver = $this->connection->getDriver()->getName();
        switch ($dbDriver) {
            case DatabaseDriverInterface::DRIVER_POSTGRESQL:
                $query = 'UPDATE oro_email_origin AS eo SET eo.owner_id =
                  (SELECT ueo.user_id FROM oro_user_email_origin AS ueo WHERE ueo.origin_id = eo.id);
                      UPDATE oro_email_origin AS eo SET eo.organization_id =
                  (SELECT u.organization_id FROM oro_user AS u WHERE u.id = eo.owner_id)
                  ';

                break;
            case DatabaseDriverInterface::DRIVER_MYSQL:
            default:
                $query = 'UPDATE oro_email_origin eo SET eo.owner_id =
                  (SELECT ueo.user_id FROM oro_user_email_origin ueo WHERE ueo.origin_id = eo.id);
                      UPDATE oro_email_origin eo SET eo.organization_id =
                  (SELECT u.organization_id FROM oro_user u WHERE u.id = eo.owner_id)
                  ';

                break;
        }

        $this->logQuery($logger, $query);
        if (!$dryRun) {
            $this->connection->executeUpdate($query);
        }
    }
}
<commit_msg>CRM-2480: User Mailbox Sync with Ownership Type "User" for Emails - pg fix<commit_after><?php

namespace Oro\Bundle\UserBundle\Migrations\Schema\v1_15;

use Psr\Log\LoggerInterface;

use Oro\Bundle\EntityBundle\ORM\DatabaseDriverInterface;
use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;

class UpdateEmailOriginTableQuery extends ParametrizedMigrationQuery
{
    /**
     * {@inheritDoc}
     */
    public function getDescription()
    {
        $logger = new ArrayLogger();
        $this->doExecute($logger, true);

        return $logger->getMessages();
    }

    /**
     * {@inheritDoc}
     */
    public function execute(LoggerInterface $logger)
    {
        $this->doExecute($logger);
    }

    /**
     * @param LoggerInterface $logger
     * @param bool $dryRun
     *
     * @throws \Doctrine\DBAL\DBALException
     */
    protected function doExecute(LoggerInterface $logger, $dryRun = false)
    {

        $dbDriver = $this->connection->getDriver()->getName();
        switch ($dbDriver) {
            case DatabaseDriverInterface::DRIVER_POSTGRESQL:
                $query = <<<SQL
UPDATE oro_email_origin AS eo SET eo.owner_id = ueo.user_id
  FROM oro_user_email_origin AS ueo WHERE ueo.origin_id = eo.id;
UPDATE oro_email_origin AS eo SET eo.organization_id = u.organization_id
  FROM oro_user AS u WHERE u.id = eo.owner_id)
SQL;
                break;
            case DatabaseDriverInterface::DRIVER_MYSQL:
            default:
                $query = <<<SQL
UPDATE oro_email_origin eo SET eo.owner_id =
  (SELECT ueo.user_id FROM oro_user_email_origin ueo WHERE ueo.origin_id = eo.id);
UPDATE oro_email_origin eo SET eo.organization_id =
  (SELECT u.organization_id FROM oro_user u WHERE u.id = eo.owner_id)
SQL;
                break;
        }

        $this->logQuery($logger, $query);
        if (!$dryRun) {
            $this->connection->executeUpdate($query);
        }
    }
}

{'fraction_non_alphanumeric': 0.10202069985214392, 'fraction_numerical': 0.0024642681123706258, 'mean_word_length': 2.413793103448276, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6892773', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1239, 'n_words': 253}