<commit_before><?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;

class MigrateEnv extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:env';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Migrate to specific environments';

    /**
     * The set environment to target
     * @var
     */
    protected $env;

    /**
     * The stored or set database details
     * @var
     */
    protected $dbHost;
    protected $dbName;
    protected $dbUser;
    protected $dbPassword;
    protected $dbPort = 3306;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Ask for environment target (simply add more to support additional environment targets)
        $this->env = $this->choice('Please select an environment target?', ['Local', 'QA', 'Production']);
        $this->info('Environment: ' . $this->env);

        // Stored json file of credentials (Note the storage folder should be ignored by git, therefore not committed)
        $filePath = storage_path() . "/app/db-migration-" . $this->env . ".json";

        // Check if we have previously saved details for the set environment
        if(file_exists($filePath)) {
            if ($this->confirm('Settings for this environment have been found, do you want to use stored settings?')) {
                $details = json_decode(file_get_contents($filePath), true);
            }
        }

        // If not stored, or said no to using stored details, then re-ask for all the needed information
        if(!isset($details)) {
            $this->dbHost = $this->ask('Please enter the Database Host');
            $this->dbName = $this->ask('Please enter the Database Name');
            $this->dbUser = $this->ask('Please enter the Database User');
            $this->dbPassword = $<PASSWORD>->secret('Please enter the Database Password');
            $this->dbPort = $this->ask('Please enter the Database Port (default: 3306)', 3306);

            $details = [
                'env' => $this->env,
                'dbHost' => $this->dbHost,
                'dbName' => $this->dbName,
                'dbUser' => $this->dbUser,
                'dbPassword' => $<PASSWORD>,
                'dbPort' => $this->dbPort
            ];

            // Store the environment credentials in a json file inside storage/app
            file_put_contents($filePath, json_encode($details));
        }

        // Set the database connection details for this request.
        config(['database.connections.mysql.host' => $details['dbHost']]);
        config(['database.connections.mysql.port' => $details['dbPort']]);
        config(['database.connections.mysql.database' => $details['dbName']]);
        config(['database.connections.mysql.username' => $details['dbUser']]);
        config(['database.connections.mysql.password' => $details['dbPassword']]);

        // Test the database connection before doing anything else
        try {
            DB::connection()->getPdo();
        } catch (\Exception $e) {
            $this->error("Could not connect to the database.  Please check your configuration.");
            exit;
        }

        // Execute the migrations
        Artisan::call('migrate');
        $this->info(Artisan::output());

    }
}<commit_msg>Updated to refactor the creation of a database connection to make isolated just for the migration tool<commit_after><?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;

class MigrateEnv extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:env';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Migrate to specific environments';

    /**
     * The set environment to target
     * @var
     */
    protected $env;

    /**
     * The stored or set database details
     * @var
     */
    protected $dbHost;
    protected $dbName;
    protected $dbUser;
    protected $dbPassword;
    protected $dbPort = 3306;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Ask for environment target (simply add more to support additional environment targets)
        $this->env = $this->choice('Please select an environment target?', ['Local', 'QA', 'Production']);
        $this->info('Environment: ' . $this->env);

        // Stored json file of credentials (Note the storage folder should be ignored by git, therefore not committed)
        $filePath = storage_path() . "/app/db-migration-" . $this->env . ".json";

        // Check if we have previously saved details for the set environment
        if(file_exists($filePath)) {
            if ($this->confirm('Settings for this environment have been found, do you want to use stored settings?')) {
                $details = json_decode(file_get_contents($filePath), true);
            }
        }

        // If not stored, or said no to using stored details, then re-ask for all the needed information
        if(!isset($details)) {
            $this->dbHost = $this->ask('Please enter the Database Host');
            $this->dbName = $this->ask('Please enter the Database Name');
            $this->dbUser = $this->ask('Please enter the Database User');
            $this->dbPassword = $this->secret('Please enter the Database Password');
            $this->dbPort = $this->ask('Please enter the Database Port (default: 3306)', 3306);

            $details = [
                'env' => $this->env,
                'dbHost' => $this->dbHost,
                'dbName' => $this->dbName,
                'dbUser' => $this->dbUser,
                'dbPassword' => $<PASSWORD>,
                'dbPort' => $this->dbPort
            ];

            // Store the environment credentials in a json file inside storage/app
            file_put_contents($filePath, json_encode($details));
        }

        // Create temp connection details
        $tempConnection = [
            'driver' => 'mysql',
            'host' => $details['dbHost'],
            'port' => $details['dbPort'],
            'database' => $details['dbName'],
            'username' => $details['dbUser'],
            'password' => $details['dbPassword'],
            'unix_socket' => '',
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ];

        // Set the database connection details for this request.
        config(['database.connections.mysql_env_migration_temp' => $tempConnection]);

        // Attempt to reconnect if previous connection
        DB::reconnect('mysql_env_migration_temp');

        // Test the database connection before doing anything else
        try {
            DB::connection('mysql_env_migration_temp')->getPdo();
        } catch (\Exception $e) {
            $this->error("Could not connect to the database.  Please check your configuration.");
            exit;
        }

        // Execute the migrations
        Artisan::call('migrate', ['--database' => 'mysql_env_migration_temp']);
        $this->info(Artisan::output());

    }
}
{'fraction_non_alphanumeric': 0.13012139605462822, 'fraction_numerical': 0.0035407182599898838, 'mean_word_length': 2.1610711430855316, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 85, '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': '4365428', 'n_tokens_mistral': 2122, 'n_tokens_neox': 2060, 'n_words': 608}