<commit_before><?php
namespace Setup\Shell;

use Cake\Collection\Collection;
use Cake\Console\Shell;
use Setup\Shell\Traits\DbToolsTrait;

if (!defined('WINDOWS')) {
	if (substr(PHP_OS, 0, 3) === 'WIN') {
		define('WINDOWS', true);
	} else {
		define('WINDOWS', false);
	}
}

/**
 * A Shell to ease database migrations needed
 * - Convert null fields without a default value
 *
 * @author <NAME>
 * @license MIT
 */
class DbMigrationShell extends Shell {

	use DbToolsTrait;

	/**
	 * Assert proper non null fields (having a default value, needed for MySQL > 5.6).
	 *
	 * @return void
	 */
	public function nulls() {
		$prefix = '';
		$tables = $this->_getTables($prefix);

		$todo = [];

		$db = $this->_getConnection();

		$this->out('Checking for tables that need updating:', 1, static::VERBOSE);
		foreach ($tables as $table) {
			// Structure
			$sql = 'DESCRIBE ' . $table['table_name'] . ';';
			$this->out('- ' . $sql, 1, static::VERBOSE);
			$res = $db->query($sql);
			$fields = new Collection($res);

			$fieldList = [];
			foreach ($fields as $field) {
				$name = $field['Field'];
				if ($name === 'id' || substr($name, -3) === '_id') {
					continue;
				}

				$type = $field['Type'];
				$null = $field['Null'];
				if (!in_array($type, ['longtext', 'mediumtext', 'text']) && !preg_match('/^(varchar|char)\(/', $type)) {
					continue;
				}
				if ($type === 'varchar(36)' || $type === 'char(36)') {
					continue;
				}

				$fieldList[] = $field['Field'];

				if ($null === 'YES' || $field['Default'] !== null) {
					continue;
				}

				// We need to migrate sth
				$todo[] = 'ALTER TABLE' . ' ' . $table['table_name'] . ' CHANGE `' . $name . '` `' . $name . '` ' . $type . ' NOT NULL DEFAULT \'\';';
			}
		}

		if (!$todo) {
			$this->out('Nothing to do :)');
			return;
		}

		$this->out(count($todo) . ' tables/fields need updating.');
		$continue = $this->in('Continue?', ['y', 'n'], 'y');
		if ($continue !== 'y') {
			$this->abort('Aborted!');
		}
		$sql = implode(PHP_EOL, $todo);
		if (!empty($this->params['dry-run'])) {
			$this->out($sql);
			return;
		}

		$db->query($sql);
		$this->out('Done :)');
	}

	/**
	 * @return \Cake\Console\ConsoleOptionParser
	 */
	public function getOptionParser() {
		$subcommandParser = [
			'options' => [
				'dry-run' => [
					'short' => 'd',<commit_msg>Add DbMigration for non nullish values<commit_after><?php
namespace Setup\Shell;

use Cake\Collection\Collection;
use Cake\Console\Shell;
use Setup\Shell\Traits\DbToolsTrait;

if (!defined('WINDOWS')) {
	if (substr(PHP_OS, 0, 3) === 'WIN') {
		define('WINDOWS', true);
	} else {
		define('WINDOWS', false);
	}
}

/**
 * A Shell to ease database migrations needed
 * - Convert null fields without a default value
 *
 * @author <NAME>
 * @license MIT
 */
class DbMigrationShell extends Shell {

	use DbToolsTrait;

	/**
	 * Assert proper non null fields (having a default value, needed for MySQL > 5.6).
	 *
	 * @return void
	 */
	public function nulls() {
		$prefix = '';
		$tables = $this->_getTables($prefix);

		$todo = [];

		$db = $this->_getConnection();

		$this->out('Checking for tables that need updating:', 1, static::VERBOSE);
		foreach ($tables as $table) {
			// Structure
			$sql = 'DESCRIBE ' . $table['table_name'] . ';';
			$this->out('- ' . $sql, 1, static::VERBOSE);
			$res = $db->query($sql);
			$fields = new Collection($res);

			$fieldList = [];
			foreach ($fields as $field) {
				$name = $field['Field'];
				if ($name === 'id' || substr($name, -3) === '_id') {
					continue;
				}

				$type = $field['Type'];
				$null = $field['Null'];
				if (!in_array($type, ['longtext', 'mediumtext', 'text']) && !preg_match('/^(varchar|char)\(/', $type)) {
					continue;
				}
				if ($type === 'varchar(36)' || $type === 'char(36)') {
					continue;
				}

				$fieldList[] = $field['Field'];

				if ($null === 'YES' || $field['Default'] !== null) {
					continue;
				}

				// We need to migrate sth
				if (!in_array($type, ['longtext', 'mediumtext', 'text'])) {
					$todo[] = 'ALTER TABLE' . ' ' . $table['table_name'] . ' CHANGE `' . $name . '` `' . $name . '` ' . $type . ' NOT NULL DEFAULT \'\';';
				} else {
					$todo[] = 'ALTER TABLE' . ' ' . $table['table_name'] . ' CHANGE `' . $name . '` `' . $name . '` ' . $type . ' DEFAULT NULL;';
				}
			}
		}

		if (!$todo) {
			$this->out('Nothing to do :)');
			return;
		}

		$this->out(count($todo) . ' tables/fields need updating.');
		$continue = $this->in('Continue?', ['y', 'n'], 'y');
		if ($continue !== 'y') {
			$this->abort('Aborted!');
		}
		$sql = implode(PHP_EOL, $todo);
		if (!empty($this->params['dry-run'])) {
			$this->out($sql);
			return;
		}

		$db->query($sql);
		$this->out('Done :)');
	}

	/**
	 * @return \Cake\Console\ConsoleOptionParser
	 */
	public function getOptionParser() {
		$subcommandParser = [
			'options' => [
				'dry-run' => [
					'short' => 'd',
{'fraction_non_alphanumeric': 0.22834967320261437, 'fraction_numerical': 0.004493464052287581, 'mean_word_length': 3.013934426229508, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '6621644', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1757, 'n_words': 396}