Welcome Guest, Not a member yet? Register   Sign In
connect to database mid-script
#1

Hi,
I have created a CLI command which sets up the database.
The user is asked for DB credentials.
PHP Code:
        $dbHost CLI::prompt('Database host''localhost');
        $dbName CLI::prompt('Database name');
        $dbUser CLI::prompt('Database username');
        $dbPass CLI::prompt('Database password (input is readable!)'); 

This sets the provided values in the .env file.
But, within this same command I want to run migrations.
PHP Code:
        $migrate Services::migrations();
        try{
            $migrate->setNamespace(null)->latest();
            CLI::write('Database setup completed''yellow');
        } catch(Throwable $e){
            CLI::write('Failed writing to database.['.$e.']''red');
        

But since it's in the same script/command, I don't have access to the database yet.
Is there a way I can connect to the provided credentials mid-script and run the migrations on that connection?
Reply
#2

Create array $config = [dbhost, dbname, ...] for db connect and inject to Services::migrations(null, db_connect($config))
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 08-14-2023, 06:34 AM by sjender.)

I'm afraid that doesn't work.
Here's what I did...
PHP Code:
        $config = [
            'hostname' => $dbHost,
            'username' => $dbUser,
            'password' => $dbPass,
            'database' => $dbName,
            'DBDriver' => 'MySQLi'
        ];

        $dbConnection = \Config\Database::connect($config);

        if($dbConnection)
        {
            print_r($dbConnection);
        }

        $migrate Services::migrations(null$dbConnection); 

The print_r does provide a connection object:
Code:
CodeIgniter\Database\MySQLi\Connection Object
(
    [DSN:protected] =>
    [port:protected] =>
    [hostname:protected] => localhost
    [username:protected] => root
    [password:protected] =>
    [database:protected] => witest
    [DBDriver] => MySQLi
    [subdriver:protected] =>
    [DBPrefix:protected] =>
    [pConnect:protected] => 1
    [DBDebug:protected] => 1
    [charset:protected] => utf8
    [DBCollat:protected] => utf8_general_ci
    [swapPre:protected] =>
    [encrypt:protected] =>
    [compress:protected] =>
    [strictOn:protected] =>
    [failover:protected] => Array
        (
        )

    [lastQuery:protected] =>
    [connID] =>
    [resultID] =>
    [protectIdentifiers] => 1
    [reservedIdentifiers:protected] => Array
        (
            [0] => *
        )

    [escapeChar] => `
    [likeEscapeStr] =>  ESCAPE '%s'
    [likeEscapeChar] => !
    [pregEscapeChar:protected] => Array
        (
        )

    [dataCache] => Array
        (
        )

    [connectTime:protected] => 0
    [connectDuration:protected] => 0
    [pretend:protected] =>
    [transEnabled] => 1
    [transStrict] => 1
    [transDepth:protected] => 0
    [transStatus:protected] => 1
    [transFailure:protected] =>
    [transException:protected] =>
    [aliasedTables:protected] => Array
        (
        )

    [queryClass:protected] => CodeIgniter\Database\Query
    [deleteHack] => 1
    [mysqli] =>
    [resultMode] => 0
)


But the error when trying to migrate is: Access denied for user ''@localhost (using password: NO).
Since there is no user, I suspect the connection object is not used properly, because the user is present in the connection object...

I use the same credentials to login to the database localy....
Reply
#4

[password:protected] =>
Empty. Connection work, but config failed. Hmm
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

In this particular case the password should be empty, it's a local XAMP install.
I have tried it with another user and pass, results are the same.
Reply
#6

Better set a password, even on XAMPP
Reply
#7

Can I run another cli command from this cli command.
Then I can put this migration in the second one. Then the .env should be loaded when runnning the second one.
Reply
#8

May be \Config\Services::commands()->run('migrate', []); ?
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB