CodeIgniter4: Bug: php spark migrate throws wrongful CodeIgniter\Database\Exceptions\DatabaseException Unable to connect to the database

Bug Description Impossible to perform migrations through spark (experienced in versions 4.0.2 to 4.0.4)

Although DB credentials are set in .env and/orin app/Config/Database.php and successful query results obtained through Controller classes,

php spark migrate causes CodeIgniter\Database\Exceptions\DatabaseException

Exact command line output:


CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 05:22:57am

Running all new migrations...
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:     Unable to connect to the database.
Filename:    /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

	Backtrace:
						-646 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::initialize
								-1548 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::query
								-1591 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::listTables
								-945 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::tableExists
								-193 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::ensureTable
								-128 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
								-100 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Commands.php::run
								-107 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
								-85 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
								-914 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
								-404 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
								-312 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
								-86 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Console.php::run
								-57 - /Applications/MAMP/ci4projs/deneme/spark::run

CodeIgniter 4 version Last gotten in v4.0.4 release

Affected module(s) spark

Expected behavior All migrations performed successfully (without exceptions).

Steps to reproduce

  1. Open up a Terminal / command line window
  2. Change to the directory to the grand-parent level of your webserver’s htdocs where htdocs will be substituted with the public directory of CI4
  3. composer create-project codeigniter4/appstarter deneme
  4. Open the project in your code editor and edit the .env file to set CI_ENVIRONMENT = development (duplicate it first from the file named ‘env’ if it doesn’t exist - i.e. cp env .env)
  5. Perform one or both of steps 6 and 7:
  6. Enter DB credentials for MySQL in .env file (with database.default.DBDriver = MySQLi)
  7. Enter DB credentials in app/Config/Database.php
  8. Go back to the command line and run the following commands (in my example proj name is ‘deneme’: 9: cd deneme
  9. php spark migrate:create AddBlog
  10. (Optional) Go back to your code editor and open the file whose name is resembles “2020_mm_dd_hhmmss_AddBlog.php” from app/Database/Migrations
  11. (Optional) Replace the contents of up() and down() methods with example from https://codeigniter.com/user_guide/dbmgmt/migration.html
  12. Go back to the command line and while still in the root directory of the CI4 project run
  13. php spark migrate
  14. Compare your Terminal output to the following output:
CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 05:42:10am

Running all new migrations...
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:     Unable to connect to the database.
Filename:    /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

	Backtrace:
						-646 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::initialize
								-1548 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::query
								-1591 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::listTables
								-945 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::tableExists
								-193 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::ensureTable
								-128 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
								-100 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Commands.php::run
								-107 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
								-85 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
								-914 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
								-404 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
								-312 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
								-86 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Console.php::run
								-57 - /Applications/MAMP/ci4projs/deneme/spark::run

Context

  • OS: Experienced in macOS 10.3
  • Web server: Apache 2.x (in MAMP 5.4)
  • PHP version 7.3.7
  • MySQL 5.7

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 36 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Have you tried changing localhost to 127.0.0.1?

@lonnieezell

Have you tried changing localhost to 127.0.0.1?

Following your suggestion, issue is resolved. Changing localhost to 127.0.0.1 in .env (and not app/Config/Database.php) works for spark.

Thanks to all who spared time and effort in looking into this.

I want to add something to this. I’ve just retested my case.

If I try to connect to the database using a standard model, the connection works. The database exists, and everything works. When I try to run the migration itself he doesn’t work, I have this error again.

CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 09:57:05am

An uncaught Exception was encountered

Type: CodeIgniter\Database\Exceptions\DatabaseException
Message: Unable to connect to the database.
Filename: /var/www/html/system/Database/BaseConnection.php
Line Number: 425

Via controller/request is working but is not the same when I try to use commands via spark, no.

If you are using MAMP like I am you will have to provide the path to the php you want to use.

Example: /Applications/MAMP/bin/php/php7.4.21/bin/php spark <command>

I had the same issue, after checking the writable/logs file, the only error message was that class SQLite3 was not found… after installing php-sqlite3, the migration worked.

This is weird given that I’m not using sqlite…

at .env files are set the same:

example:

`database.default.hostname = 127.0.0.1
 database.default.database = ci4
 database.default.username = root
 database.default.password = 
 database.default.DBDriver = MySQLi

 database.tests.hostname = 127.0.0.1
 database.tests.database = ci4
 database.tests.username = root
 database.tests.password = 
 database.tests.DBDriver = MySQLi`
`┌──(aer㉿aerhost)-[/opt/lampp/htdocs/pci4]
└─$ php spark migrate -all                                                                  

CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2021-01-18 09:53:52am

Running all new migrations...
        Running: (Myth\Auth) 2017-11-20-223112_Myth\Auth\Database\Migrations\CreateAuthTables
Done

`

I just found a solution to migrate with command() function. But still, from cli it does not work. echo command("migrate"); in controller.

DB connection works through code in Controllers running in the web application so database is accessible alright. It just doesn’t work with spark. Spark is not seeing the DB config in .env or in app\Config\Database.php .

I also tested with ci-news app - same result. Did you try to reproduce through the steps in the issue description? (e.g. using composer to create a project from scratch)