<commit_before><?php

namespace Oro\Bundle\IntegrationBundle\Migrations\Schema\v1_3;

use Psr\Log\LoggerInterface;

use Oro\Bundle\DataGridBundle\Common\Object as ConfigObject;
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
use Oro\Bundle\OrganizationBundle\Migrations\Data\ORM\LoadOrganizationAndBusinessUnitData;

class MigrateValuesQuery extends ParametrizedMigrationQuery
{
    /**
     * {@inheritdoc}
     */
    public function getDescription()
    {
        return 'Move integration settings from flat separated fields to serialized single field';
    }

    /**
     * {@inheritdoc}
     */
    public function execute(LoggerInterface $logger)
    {
        $sql = 'SELECT id, is_two_way_sync_enabled, sync_priority FROM oro_integration_channel';
        $this->logQuery($logger, $sql);
        $values = $this->connection->fetchAll($sql);

        $sql    = 'SELECT id FROM oro_organization WHERE name = :name';
        $params = ['name' => LoadOrganizationAndBusinessUnitData::MAIN_ORGANIZATION];
        $types  = ['name' => 'string'];
        $this->logQuery($logger, $sql, $params, $types);
        $organizationId = $this->connection->fetchColumn($sql, $params);

        $updateSql = 'UPDATE oro_integration_channel SET ' .
            'synchronization_settings = :syncSettings, ' .
            'mapping_settings = :mappingSettings, ' .
            'enabled = :enabled, ' .
            'organization_id = :organizationId ' .
            'WHERE id = :id';

        foreach ($values as $row) {
            $params = [
                'syncSettings'    => ConfigObject::create(
                    [
                        'isTwoWaySyncEnabled' => (bool)$row['is_two_way_sync_enabled'],
                        'syncPriority'        => (bool)$row['sync_priority']
                    ]
                ),
                'mappingSettings' => ConfigObject::create([]),
                'enabled'         => 1,
                'organizationId'  => $organizationId,
                'id'              => $row['id']
            ];
            $types  = [
                'syncSettings'    => 'object',
                'mappingSettings' => 'object',
                'enabled'         => 'integer',
                'organizationId'  => 'integer',
                'id'              => 'integer'
            ];

            $this->logQuery($logger, $updateSql, $params, $types);
            $this->connection->executeUpdate($updateSql, $params, $types);
        }
    }
}<commit_msg>CRM-1496: Can't open previously created Integration after update oro:platform    - fix organization assign query<commit_after><?php

namespace Oro\Bundle\IntegrationBundle\Migrations\Schema\v1_3;

use Oro\Bundle\MigrationBundle\Migration\ArrayLogger;
use Psr\Log\LoggerInterface;

use Oro\Bundle\DataGridBundle\Common\Object as ConfigObject;
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery;
use Oro\Bundle\OrganizationBundle\Migrations\Data\ORM\LoadOrganizationAndBusinessUnitData;

class MigrateValuesQuery extends ParametrizedMigrationQuery
{
    /**
     * {@inheritdoc}
     */
    public function getDescription()
    {
        $logger = new ArrayLogger();
        $logger->info('Move integration settings from flat separated fields to serialized single field');
        $this->doExecute($logger, true);

        return $logger->getMessages();
    }

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

    /**
     * {@inheritdoc}
     */
    public function doExecute(LoggerInterface $logger, $dryRun = false)
    {
        $sql = 'SELECT id, is_two_way_sync_enabled, sync_priority FROM oro_integration_channel';
        $this->logQuery($logger, $sql);
        $values = $this->connection->fetchAll($sql);

        $sql    = 'SELECT id FROM oro_organization WHERE name = :name';
        $params = ['name' => LoadOrganizationAndBusinessUnitData::MAIN_ORGANIZATION];
        $types  = ['name' => 'string'];
        $this->logQuery($logger, $sql, $params, $types);
        $organizationId = $this->connection->fetchColumn($sql, $params);

        // if default organization not found - assign on first exists
        if (false === $organizationId) {
            $sql = 'SELECT id FROM oro_organization';
            $this->logQuery($logger, $sql);
            $organizationIds = array_map(
                function ($row) {
                    return $row['id'];
                },
                $this->connection->fetchAll($sql)
            );
            $organizationId  = min($organizationIds);
        }

        $updateSql = 'UPDATE oro_integration_channel SET ' .
            'synchronization_settings = :syncSettings, ' .
            'mapping_settings = :mappingSettings, ' .
            'enabled = :enabled ' . ($organizationId ? ', organization_id = :organizationId ' : '') .
            'WHERE id = :id';

        foreach ($values as $row) {
            $params = [
                'syncSettings'    => ConfigObject::create(
                    [
                        'isTwoWaySyncEnabled' => (bool)$row['is_two_way_sync_enabled'],
                        'syncPriority'        => $row['sync_priority']
                    ]
                ),
                'mappingSettings' => ConfigObject::create([]),
                'enabled'         => 1,
                'id'              => $row['id']
            ];
            $types  = [
                'syncSettings'    => 'object',
                'mappingSettings' => 'object',
                'enabled'         => 'integer',
                'id'              => 'integer'
            ];

            if ($organizationId) {
                $params['organizationId'] = $organizationId;
                $types['organizationId']  = 'integer';
            }

            $this->logQuery($logger, $updateSql, $params, $types);
            $this->connection->executeUpdate($updateSql, $params, $types);
        }
    }
}
{'fraction_non_alphanumeric': 0.1276879479913319, 'fraction_numerical': 0.001666944490748458, 'mean_word_length': 1.997002997002997, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, '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': '2373536', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1569, 'n_words': 303}