diff --git a/system/Commands/Generators/MigrationGenerator.php b/system/Commands/Generators/MigrationGenerator.php index b5e64ec1bce1..5967c12c30af 100644 --- a/system/Commands/Generators/MigrationGenerator.php +++ b/system/Commands/Generators/MigrationGenerator.php @@ -89,10 +89,28 @@ public function run(array $params) if (array_key_exists('session', $params) || CLI::getOption('session')) { $table = $params['table'] ?? CLI::getOption('table') ?? 'ci_sessions'; $params[0] = "_create_{$table}_table"; + + $group = $params['dbgroup'] ?? CLI::getOption('dbgroup'); + $group = is_string($group) ? $group : 'default'; + $driver = config(Database::class)->{$group}['DBDriver'] ?? null; + + if ($driver === null) { + CLI::error(lang('CLI.generator.undefinedDatabaseGroup', [$group])); + + return EXIT_ERROR; + } + + if ($driver !== 'MySQLi' && $driver !== 'Postgre') { + CLI::error(lang('CLI.generator.unsupportedSessionDriver', [$group, $driver])); + + return EXIT_ERROR; + } } $this->classNameLang = 'CLI.generator.className.migration'; $this->generateClass($params); + + return EXIT_SUCCESS; } /** diff --git a/system/Language/en/CLI.php b/system/Language/en/CLI.php index 01e60c402955..822186dba926 100644 --- a/system/Language/en/CLI.php +++ b/system/Language/en/CLI.php @@ -33,17 +33,19 @@ 'transformer' => 'Transformer class name', 'validation' => 'Validation class name', ], - 'commandType' => 'Command type', - 'databaseGroup' => 'Database group', - 'fileCreate' => 'File created: {0}', - 'fileError' => 'Error while creating file: "{0}"', - 'fileExist' => 'File exists: "{0}"', - 'fileOverwrite' => 'File overwritten: "{0}"', - 'parentClass' => 'Parent class', - 'returnType' => 'Return type', - 'tableName' => 'Table name', - 'usingCINamespace' => 'Warning: Using the "CodeIgniter" namespace will generate the file in the system directory.', - 'viewName' => [ + 'commandType' => 'Command type', + 'databaseGroup' => 'Database group', + 'fileCreate' => 'File created: {0}', + 'fileError' => 'Error while creating file: "{0}"', + 'fileExist' => 'File exists: "{0}"', + 'fileOverwrite' => 'File overwritten: "{0}"', + 'parentClass' => 'Parent class', + 'returnType' => 'Return type', + 'tableName' => 'Table name', + 'undefinedDatabaseGroup' => 'The "{0}" database group is not defined.', + 'unsupportedSessionDriver' => 'Database sessions are only supported on MySQLi and Postgre. The "{0}" database group uses the "{1}" driver.', + 'usingCINamespace' => 'Warning: Using the "CodeIgniter" namespace will generate the file in the system directory.', + 'viewName' => [ 'cell' => 'Cell view name', ], ], diff --git a/tests/system/Commands/Generators/MigrationGeneratorTest.php b/tests/system/Commands/Generators/MigrationGeneratorTest.php index c4670019a422..7750a2ed35d7 100644 --- a/tests/system/Commands/Generators/MigrationGeneratorTest.php +++ b/tests/system/Commands/Generators/MigrationGeneratorTest.php @@ -13,8 +13,10 @@ namespace CodeIgniter\Commands\Generators; +use CodeIgniter\Config\Factories; use CodeIgniter\Test\CIUnitTestCase; use CodeIgniter\Test\StreamFilterTrait; +use Config\Database; use PHPUnit\Framework\Attributes\Group; /** @@ -27,6 +29,8 @@ final class MigrationGeneratorTest extends CIUnitTestCase protected function tearDown(): void { + Factories::reset('config'); + $result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer()); $file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14))); if (is_file($file)) { @@ -52,6 +56,29 @@ public function testGenerateMigrationWithOptionTable(): void $this->assertStringContainsString('_CreateLoggerTable.php', $this->getStreamFilterBuffer()); } + public function testSessionRejectsUnsupportedDriver(): void + { + $config = new Database(); + $config->default['DBDriver'] = 'SQLite3'; + Factories::injectMock('config', 'Database', $config); + + command('make:migration -session'); + + $this->assertStringContainsString( + 'Database sessions are only supported on MySQLi and Postgre. The "default" database group uses the "SQLite3" driver.', + $this->getStreamFilterBuffer(), + ); + $this->assertSame([], glob(APPPATH . 'Database/Migrations/*_CreateCiSessionsTable.php')); + } + + public function testSessionRejectsUndefinedGroup(): void + { + command('make:migration -session -dbgroup bogus'); + + $this->assertStringContainsString('The "bogus" database group is not defined.', $this->getStreamFilterBuffer()); + $this->assertSame([], glob(APPPATH . 'Database/Migrations/*_CreateCiSessionsTable.php')); + } + public function testGenerateMigrationWithOptionSuffix(): void { command('make:migration database -suffix'); diff --git a/user_guide_src/source/changelogs/v4.7.5.rst b/user_guide_src/source/changelogs/v4.7.5.rst index e180de7d33f2..68e5bf096d3a 100644 --- a/user_guide_src/source/changelogs/v4.7.5.rst +++ b/user_guide_src/source/changelogs/v4.7.5.rst @@ -18,6 +18,7 @@ BREAKING Message Changes *************** +- Added the ``CLI.generator.undefinedDatabaseGroup`` and ``CLI.generator.unsupportedSessionDriver`` language strings. - Added the ``Cookie.invalidCookieValue`` language string. - Added the ``Cookie.invalidCookiePath`` language string. - Added the ``Cookie.invalidCookieDomain`` language string. @@ -43,6 +44,7 @@ Bugs Fixed On Windows, where the ``readline`` extension is built on WinEditLine, the prompt is written to STDOUT first because WinEditLine reports no library version and prints ANSI sequences literally. - **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing. - **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes). +- **Commands:** Fixed a bug where ``make:migration --session`` silently generated a broken migration when the database group's driver is neither ``MySQLi`` nor ``Postgre``. The command now reports an error and returns ``EXIT_ERROR``. - **Content Security Policy:** Fixed a bug where empty ``Content-Security-Policy``, ``Content-Security-Policy-Report-Only``, and ``Reporting-Endpoints`` response headers were generated when no corresponding values existed. - **Cookie:** Fixed a bug where ``Cookie`` instances created with ``raw: true`` allowed invalid characters in cookie values rejected by ``setrawcookie()``. - **Cookie:** Fixed a bug where ``Cookie`` instances allowed invalid characters in path, domain, and prefix attributes rejected by ``setcookie()`` and ``setrawcookie()``.