Skip to content

Commit 896c7b6

Browse files
authored
fix: reject unsupported database drivers in make:migration --session (#10546)
1 parent 39a0d2f commit 896c7b6

4 files changed

Lines changed: 60 additions & 11 deletions

File tree

‎system/Commands/Generators/MigrationGenerator.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,28 @@ public function run(array $params)
8989
if (array_key_exists('session', $params) || CLI::getOption('session')) {
9090
$table = $params['table'] ?? CLI::getOption('table') ?? 'ci_sessions';
9191
$params[0] = "_create_{$table}_table";
92+
93+
$group = $params['dbgroup'] ?? CLI::getOption('dbgroup');
94+
$group = is_string($group) ? $group : 'default';
95+
$driver = config(Database::class)->{$group}['DBDriver'] ?? null;
96+
97+
if ($driver === null) {
98+
CLI::error(lang('CLI.generator.undefinedDatabaseGroup', [$group]));
99+
100+
return EXIT_ERROR;
101+
}
102+
103+
if ($driver !== 'MySQLi' && $driver !== 'Postgre') {
104+
CLI::error(lang('CLI.generator.unsupportedSessionDriver', [$group, $driver]));
105+
106+
return EXIT_ERROR;
107+
}
92108
}
93109

94110
$this->classNameLang = 'CLI.generator.className.migration';
95111
$this->generateClass($params);
112+
113+
return EXIT_SUCCESS;
96114
}
97115

98116
/**

‎system/Language/en/CLI.php‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,19 @@
3333
'transformer' => 'Transformer class name',
3434
'validation' => 'Validation class name',
3535
],
36-
'commandType' => 'Command type',
37-
'databaseGroup' => 'Database group',
38-
'fileCreate' => 'File created: {0}',
39-
'fileError' => 'Error while creating file: "{0}"',
40-
'fileExist' => 'File exists: "{0}"',
41-
'fileOverwrite' => 'File overwritten: "{0}"',
42-
'parentClass' => 'Parent class',
43-
'returnType' => 'Return type',
44-
'tableName' => 'Table name',
45-
'usingCINamespace' => 'Warning: Using the "CodeIgniter" namespace will generate the file in the system directory.',
46-
'viewName' => [
36+
'commandType' => 'Command type',
37+
'databaseGroup' => 'Database group',
38+
'fileCreate' => 'File created: {0}',
39+
'fileError' => 'Error while creating file: "{0}"',
40+
'fileExist' => 'File exists: "{0}"',
41+
'fileOverwrite' => 'File overwritten: "{0}"',
42+
'parentClass' => 'Parent class',
43+
'returnType' => 'Return type',
44+
'tableName' => 'Table name',
45+
'undefinedDatabaseGroup' => 'The "{0}" database group is not defined.',
46+
'unsupportedSessionDriver' => 'Database sessions are only supported on MySQLi and Postgre. The "{0}" database group uses the "{1}" driver.',
47+
'usingCINamespace' => 'Warning: Using the "CodeIgniter" namespace will generate the file in the system directory.',
48+
'viewName' => [
4749
'cell' => 'Cell view name',
4850
],
4951
],

‎tests/system/Commands/Generators/MigrationGeneratorTest.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
namespace CodeIgniter\Commands\Generators;
1515

16+
use CodeIgniter\Config\Factories;
1617
use CodeIgniter\Test\CIUnitTestCase;
1718
use CodeIgniter\Test\StreamFilterTrait;
19+
use Config\Database;
1820
use PHPUnit\Framework\Attributes\Group;
1921

2022
/**
@@ -27,6 +29,8 @@ final class MigrationGeneratorTest extends CIUnitTestCase
2729

2830
protected function tearDown(): void
2931
{
32+
Factories::reset('config');
33+
3034
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
3135
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
3236
if (is_file($file)) {
@@ -52,6 +56,29 @@ public function testGenerateMigrationWithOptionTable(): void
5256
$this->assertStringContainsString('_CreateLoggerTable.php', $this->getStreamFilterBuffer());
5357
}
5458

59+
public function testSessionRejectsUnsupportedDriver(): void
60+
{
61+
$config = new Database();
62+
$config->default['DBDriver'] = 'SQLite3';
63+
Factories::injectMock('config', 'Database', $config);
64+
65+
command('make:migration -session');
66+
67+
$this->assertStringContainsString(
68+
'Database sessions are only supported on MySQLi and Postgre. The "default" database group uses the "SQLite3" driver.',
69+
$this->getStreamFilterBuffer(),
70+
);
71+
$this->assertSame([], glob(APPPATH . 'Database/Migrations/*_CreateCiSessionsTable.php'));
72+
}
73+
74+
public function testSessionRejectsUndefinedGroup(): void
75+
{
76+
command('make:migration -session -dbgroup bogus');
77+
78+
$this->assertStringContainsString('The "bogus" database group is not defined.', $this->getStreamFilterBuffer());
79+
$this->assertSame([], glob(APPPATH . 'Database/Migrations/*_CreateCiSessionsTable.php'));
80+
}
81+
5582
public function testGenerateMigrationWithOptionSuffix(): void
5683
{
5784
command('make:migration database -suffix');

‎user_guide_src/source/changelogs/v4.7.5.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ BREAKING
1818
Message Changes
1919
***************
2020

21+
- Added the ``CLI.generator.undefinedDatabaseGroup`` and ``CLI.generator.unsupportedSessionDriver`` language strings.
2122
- Added the ``Cookie.invalidCookieValue`` language string.
2223
- Added the ``Cookie.invalidCookiePath`` language string.
2324
- Added the ``Cookie.invalidCookieDomain`` language string.
@@ -44,6 +45,7 @@ Bugs Fixed
4445
- **CLI:** Fixed a bug where ``CLI::input()`` and ``CLI::prompt()`` threw a ``TypeError`` when STDIN reached end-of-file (e.g., Ctrl+D) with the ``readline`` extension enabled. An empty string is now returned, matching the behavior without ``readline``.
4546
- **CLIRequest:** Fixed a bug where ``parseCommand()`` could throw a TypeError when ``argv`` is missing.
4647
- **CodeIgniter:** Fixed a bug where ``gatherOutput()`` could be called twice when ``startController()`` returned a ``ResponseInterface`` (e.g., from filter attributes or closure routes).
48+
- **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``.
4749
- **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.
4850
- **Cookie:** Fixed a bug where ``Cookie`` instances created with ``raw: true`` allowed invalid characters in cookie values rejected by ``setrawcookie()``.
4951
- **Cookie:** Fixed a bug where ``Cookie`` instances allowed invalid characters in path, domain, and prefix attributes rejected by ``setcookie()`` and ``setrawcookie()``.

0 commit comments

Comments
 (0)