Skip to content

Commit cb86efa

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.5
2 parents 0efc561 + 3d742a3 commit cb86efa

File tree

5 files changed

+67
-46
lines changed

5 files changed

+67
-46
lines changed

phpstan-baseline.php

-5
Original file line numberDiff line numberDiff line change
@@ -1096,11 +1096,6 @@
10961096
'count' => 1,
10971097
'path' => __DIR__ . '/system/Database/Migration.php',
10981098
];
1099-
$ignoreErrors[] = [
1100-
'message' => '#^Property CodeIgniter\\\\Database\\\\Migration\\:\\:\\$DBGroup \\(string\\) on left side of \\?\\? is not nullable\\.$#',
1101-
'count' => 1,
1102-
'path' => __DIR__ . '/system/Database/Migration.php',
1103-
];
11041099
$ignoreErrors[] = [
11051100
'message' => '#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#',
11061101
'count' => 8,

system/Database/Migration.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ abstract class Migration
2323
/**
2424
* The name of the database group to use.
2525
*
26-
* @var string
26+
* @var string|null
2727
*/
2828
protected $DBGroup;
2929

@@ -41,12 +41,15 @@ abstract class Migration
4141
*/
4242
protected $forge;
4343

44-
/**
45-
* Constructor.
46-
*/
4744
public function __construct(?Forge $forge = null)
4845
{
49-
$this->forge = $forge ?? Database::forge($this->DBGroup ?? config(Database::class)->defaultGroup);
46+
if (isset($this->DBGroup)) {
47+
$this->forge = Database::forge($this->DBGroup);
48+
} elseif ($forge !== null) {
49+
$this->forge = $forge;
50+
} else {
51+
$this->forge = Database::forge(config(Database::class)->defaultGroup);
52+
}
5053

5154
$this->db = $this->forge->getConnection();
5255
}

system/Database/MigrationRunner.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,12 @@ public function __construct(MigrationsConfig $config, $db = null)
137137
$this->enabled = $config->enabled ?? false;
138138
$this->table = $config->table ?? 'migrations';
139139

140-
// Default name space is the app namespace
141140
$this->namespace = APP_NAMESPACE;
142141

143-
// get default database group
144-
$config = config(Database::class);
145-
$this->group = $config->defaultGroup;
146-
unset($config);
142+
// Even if a DB connection is passed, since it is a test,
143+
// it is assumed to use the default group name
144+
$this->group = is_string($db) ? $db : config(Database::class)->defaultGroup;
147145

148-
// If no db connection passed in, use
149-
// default database group.
150146
$this->db = db_connect($db);
151147
}
152148

@@ -838,8 +834,9 @@ protected function migrate($direction, $migration): bool
838834
throw new RuntimeException($message);
839835
}
840836

841-
$instance = new $class();
842-
$group = $instance->getDBGroup() ?? config(Database::class)->defaultGroup;
837+
/** @var Migration $instance */
838+
$instance = new $class(Database::forge($this->db));
839+
$group = $instance->getDBGroup() ?? $this->group;
843840

844841
if (ENVIRONMENT !== 'testing' && $group === 'tests' && $this->groupFilter !== 'tests') {
845842
// @codeCoverageIgnoreStart
@@ -855,8 +852,6 @@ protected function migrate($direction, $migration): bool
855852
return true;
856853
}
857854

858-
$this->setGroup($group);
859-
860855
if (! is_callable([$instance, $direction])) {
861856
$message = sprintf(lang('Migrations.missingMethod'), $direction);
862857

tests/system/Database/Migrations/MigrationRunnerTest.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace CodeIgniter\Database\Migrations;
1515

1616
use CodeIgniter\Database\BaseConnection;
17-
use CodeIgniter\Database\Config;
1817
use CodeIgniter\Database\MigrationRunner;
1918
use CodeIgniter\Events\Events;
2019
use CodeIgniter\Exceptions\ConfigException;
@@ -456,11 +455,34 @@ public function testGetBatchVersions(): void
456455
$this->assertSame('2018-01-24-102302', $runner->getBatchEnd(1));
457456
}
458457

459-
protected function resetTables(): void
458+
public function testMigrationUsesSameConnectionAsMigrationRunner(): void
460459
{
461-
$forge = Config::forge();
460+
$config = ['database' => WRITEPATH . 'runner.sqlite', 'DBDriver' => 'SQLite3', 'DBDebug' => true];
462461

463-
foreach (db_connect()->listTables() as $table) {
462+
$database = Database::connect($config, false);
463+
$this->resetTables($database);
464+
465+
$runner = new MigrationRunner(config(Migrations::class), $database);
466+
$runner->clearCliMessages();
467+
$runner->clearHistory();
468+
$runner->setNamespace('Tests\Support\MigrationTestMigrations');
469+
$runner->latest();
470+
471+
$tables = $database->listTables();
472+
$this->assertCount(2, $tables);
473+
$this->assertSame('migrations', $tables[0]);
474+
$this->assertSame('foo', $tables[1]);
475+
}
476+
477+
protected function resetTables($db = null): void
478+
{
479+
$forge = Database::forge($db);
480+
481+
/** @var BaseConnection $conn */
482+
$conn = $forge->getConnection();
483+
$conn->resetDataCache();
484+
485+
foreach (db_connect($db)->listTables() as $table) {
464486
$table = str_replace('db_', '', $table);
465487
$forge->dropTable($table, true);
466488
}

user_guide_src/source/models/model.rst

+27-21
Original file line numberDiff line numberDiff line change
@@ -738,12 +738,19 @@ points in the model's execution can be affected, each through a class property:
738738
Defining Callbacks
739739
==================
740740

741-
You specify the callbacks by first creating a new class method in your model to use. This class will always
742-
receive a ``$data`` array as its only parameter. The exact contents of the ``$data`` array will vary between events, but
743-
will always contain a key named **data** that contains the primary data passed to the original method. In the case
744-
of the insert* or update* methods, that will be the key/value pairs that are being inserted into the database. The
745-
main array will also contain the other values passed to the method, and be detailed later. The callback method
746-
must return the original $data array so other callbacks have the full information.
741+
You specify the callbacks by first creating a new class method in your model to use.
742+
743+
This class method will always receive a ``$data`` array as its only parameter.
744+
745+
The exact contents of the ``$data`` array will vary between events, but will always
746+
contain a key named ``data`` that contains the primary data passed to the original
747+
method. In the case of the **insert*()** or **update*()** methods, that will be
748+
the key/value pairs that are being inserted into the database. The main ``$data``
749+
array will also contain the other values passed to the method, and be detailed
750+
in `Event Parameters`_.
751+
752+
The callback method must return the original ``$data`` array so other callbacks
753+
have the full information.
747754

748755
.. literalinclude:: model/050.php
749756

@@ -774,37 +781,36 @@ passed to each event:
774781
Event $data contents
775782
================= =========================================================================================================
776783
beforeInsert **data** = the key/value pairs that are being inserted. If an object or Entity class is passed to the
777-
insert method, it is first converted to an array.
784+
``insert()`` method, it is first converted to an array.
778785
afterInsert **id** = the primary key of the new row, or 0 on failure.
779786
**data** = the key/value pairs being inserted.
780-
**result** = the results of the insert() method used through the Query Builder.
787+
**result** = the results of the ``insert()`` method used through the Query Builder.
781788
beforeUpdate **id** = the array of primary keys of the rows being updated.
782789
**data** = the key/value pairs that are being updated. If an object or Entity class is passed to the
783-
update method, it is first converted to an array.
790+
``update()`` method, it is first converted to an array.
784791
afterUpdate **id** = the array of primary keys of the rows being updated.
785792
**data** = the key/value pairs being updated.
786-
**result** = the results of the update() method used through the Query Builder.
793+
**result** = the results of the ``update()`` method used through the Query Builder.
787794
beforeFind The name of the calling **method**, whether a **singleton** was requested, and these additional fields:
788-
- first() No additional fields
789-
- find() **id** = the primary key of the row being searched for.
790-
- findAll() **limit** = the number of rows to find.
795+
- ``first()`` No additional fields
796+
- ``find()`` **id** = the primary key of the row being searched for.
797+
- ``findAll()`` **limit** = the number of rows to find.
791798
**offset** = the number of rows to skip during the search.
792799
afterFind Same as **beforeFind** but including the resulting row(s) of data, or null if no result found.
793-
beforeDelete Varies by delete* method. See the following:
794-
- delete() **id** = primary key of row being deleted.
800+
beforeDelete **id** = primary key of row being passed to the ``delete()`` method.
795801
**purge** = boolean whether soft-delete rows should be hard deleted.
796-
afterDelete **id** = primary key of row being deleted.
802+
afterDelete **id** = primary key of row being passed to the ``delete()`` method.
797803
**purge** = boolean whether soft-delete rows should be hard deleted.
798-
**result** = the result of the delete() call on the Query Builder.
804+
**result** = the result of the ``delete()`` call on the Query Builder.
799805
**data** = unused.
800806
beforeInsertBatch **data** = associative array of values that are being inserted. If an object or Entity class is passed to the
801-
insertBatch method, it is first converted to an array.
807+
``insertBatch()`` method, it is first converted to an array.
802808
afterInsertBatch **data** = the associative array of values being inserted.
803-
**result** = the results of the insertbatch() method used through the Query Builder.
809+
**result** = the results of the ``insertbatch()`` method used through the Query Builder.
804810
beforeUpdateBatch **data** = associative array of values that are being updated. If an object or Entity class is passed to the
805-
updateBatch method, it is first converted to an array.
811+
``updateBatch()`` method, it is first converted to an array.
806812
afterUpdateBatch **data** = the key/value pairs being updated.
807-
**result** = the results of the updateBatch() method used through the Query Builder.
813+
**result** = the results of the ``updateBatch()`` method used through the Query Builder.
808814
================= =========================================================================================================
809815

810816
Modifying Find* Data

0 commit comments

Comments
 (0)