Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions system/Database/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Seeder
/**
* The name of the database group to use.
*
* @var non-empty-string
* @var non-empty-string|null
*/
protected $DBGroup;

Expand Down Expand Up @@ -92,10 +92,16 @@ public function __construct(Database $config, ?BaseConnection $db = null)

$this->config = &$config;

$db ??= Database::connect($this->DBGroup);

$this->db = $db;
$this->forge = Database::forge($this->DBGroup);
if (isset($this->DBGroup)) {
$this->db = Database::connect($this->DBGroup);
$this->forge = Database::forge($this->DBGroup);
} elseif ($db instanceof BaseConnection) {
$this->db = $db;
$this->forge = Database::forge($db);
} else {
$this->db = Database::connect($config->defaultGroup);
$this->forge = Database::forge($config->defaultGroup);
}
}

/**
Expand Down Expand Up @@ -145,7 +151,7 @@ public function call(string $class)
}

/** @var Seeder $seeder */
$seeder = new $class($this->config);
$seeder = new $class($this->config, $this->db);
$seeder->setSilent($this->silent)->run();

unset($seeder);
Expand Down
51 changes: 51 additions & 0 deletions tests/_support/Database/Seeds/SeederWithDBGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Database\Seeds;

use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\Seeder;

/**
* Test seeder with explicit DBGroup set.
*/
class SeederWithDBGroup extends Seeder
{
protected $DBGroup = 'tests';

/**
* Store the connection used during run() for testing.
*/
public static ?BaseConnection $lastConnection = null;

public function run(): void
{
self::$lastConnection = $this->db;
}

/**
* Expose the db connection for testing.
*/
public function getDatabase(): BaseConnection
{
return $this->db;
}

/**
* Reset static state for testing.
*/
public static function reset(): void
{
self::$lastConnection = null;
}
}
49 changes: 49 additions & 0 deletions tests/_support/Database/Seeds/SeederWithoutDBGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Database\Seeds;

use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\Seeder;

/**
* Test seeder without DBGroup set (should inherit connection).
*/
class SeederWithoutDBGroup extends Seeder
{
/**
* Store the connection used during run() for testing.
*/
public static ?BaseConnection $lastConnection = null;

public function run(): void
{
self::$lastConnection = $this->db;
}

/**
* Expose the db connection for testing.
*/
public function getDatabase(): BaseConnection
{
return $this->db;
}

/**
* Reset static state for testing.
*/
public static function reset(): void
{
self::$lastConnection = null;
}
}
68 changes: 67 additions & 1 deletion tests/system/Database/DatabaseSeederTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@
use Config\Database;
use Faker\Generator;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Database\Seeds\SeederWithDBGroup;
use Tests\Support\Database\Seeds\SeederWithoutDBGroup;

/**
* @internal
*/
#[Group('Others')]
#[Group('DatabaseLive')]
final class DatabaseSeederTest extends CIUnitTestCase
{
protected function tearDown(): void
{
parent::tearDown();

SeederWithDBGroup::reset();
SeederWithoutDBGroup::reset();
}

public function testInstantiateNoSeedPath(): void
{
$this->expectException('InvalidArgumentException');
Expand Down Expand Up @@ -57,4 +67,60 @@ public function testCallOnEmptySeeder(): void
$seeder = new Seeder(new Database());
$seeder->call('');
}

public function testSeederWithDBGroupUsesOwnConnection(): void
{
$config = new Database();
$db = Database::connect('tests', false);

$seeder = new SeederWithDBGroup($config, $db);

$testsDb = Database::connect('tests');
$this->assertSame($testsDb, $seeder->getDatabase());
$this->assertNotSame($db, $seeder->getDatabase());
}

public function testSeederWithoutDBGroupUsesPassedConnection(): void
{
$config = new Database();
$db = Database::connect('tests');

$seeder = new SeederWithoutDBGroup($config, $db);

$this->assertSame($db, $seeder->getDatabase());
}

public function testSeederWithoutDBGroupAndNoConnectionUsesDefault(): void
{
$config = new Database();

$seeder = new SeederWithoutDBGroup($config);

$defaultDb = Database::connect($config->defaultGroup);
$this->assertSame($defaultDb, $seeder->getDatabase());
}

public function testCallPassesConnectionToChildSeeder(): void
{
$config = new Database();
$db = Database::connect('tests');

$seeder = new Seeder($config, $db);
$seeder->setSilent(true)->call(SeederWithoutDBGroup::class);

$this->assertSame($db, SeederWithoutDBGroup::$lastConnection);
}

public function testCallChildWithDBGroupUsesOwnConnection(): void
{
$config = new Database();
$db = Database::connect('tests', false);

$seeder = new Seeder($config, $db);
$seeder->setSilent(true)->call(SeederWithDBGroup::class);

$testsDb = Database::connect('tests');
$this->assertSame($testsDb, SeederWithDBGroup::$lastConnection);
$this->assertNotSame($db, SeederWithDBGroup::$lastConnection);
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.6.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Deprecations
Bugs Fixed
**********

- **Database:** Fixed a bug where ``Seeder::call()`` did not pass the database connection to child seeders, causing them to use the default connection instead of the one specified via ``Database::seeder('group')``.

See the repo's
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.
23 changes: 23 additions & 0 deletions user_guide_src/source/dbmgmt/seeds.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ You can grab a copy of the main seeder through the database config class:

.. literalinclude:: seeds/004.php

Using a Different Database Group
================================

You can specify a different database group when obtaining a seeder instance by passing the group name
as the first parameter:

.. literalinclude:: seeds/005.php

When using ``call()`` to run child seeders, the database connection is automatically passed to them.
This means child seeders will use the same connection as the parent seeder, unless they explicitly
specify their own ``$DBGroup`` property.

If a seeder needs to always use a specific database group regardless of the parent seeder's connection,
you can set the ``$DBGroup`` property in the seeder class:

.. literalinclude:: seeds/006.php

The connection priority is:

1. If ``$DBGroup`` is set in the seeder class, that connection group is always used
2. Otherwise, if a connection was passed (from parent seeder via ``call()`` or from ``Database::seeder()``), it is used
3. Otherwise, the default connection group is used

Command Line Seeding
====================

Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/dbmgmt/seeds/005.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

$seeder = \Config\Database::seeder('group_name');
$seeder->call('TestSeeder');
15 changes: 15 additions & 0 deletions user_guide_src/source/dbmgmt/seeds/006.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Database\Seeds;

use CodeIgniter\Database\Seeder;

class UserSeeder extends Seeder
{
protected $DBGroup = 'group_name';

public function run()
{
// ...
}
}
Loading