<?php
$_db_migration_to = '33.11'; // 8.7.6
if (!isset($migrations) || !is_object($migrations))
	die("This file cannot be executed directly");
$migrations->CheckValid($_db_migration_to);
//===========================================================================================



$migrations->Run('01_revert_conversations.php', <<<'DB_UPDATE_FILE'
<?php

/** @var \Claromentis\Setup\SetupFacade $migrations */
$db = $migrations->GetDb();

$large_conversations = $db->query("SELECT conversation_id FROM im_conversations_users GROUP BY conversation_id HAVING COUNT(user_id) > 50;")->fetchAllValues();

if (!empty($large_conversations))
{
	$db->query(/** @lang SQL */ "UPDATE imessages SET message_type = 1, conversation_id = 0 WHERE conversation_id IN in:int:ids", $large_conversations);
	$db->query(/** @lang SQL */ "DELETE FROM im_conversations_users WHERE conversation_id IN in:int:ids", $large_conversations);
	$db->query(/** @lang SQL */ "DELETE FROM im_conversations WHERE id IN in:int:ids", $large_conversations);
}

DB_UPDATE_FILE
);


//===========================================================================================
$migrations->SetVersion('33.11');
