Skip to content

Commit f7dbcf6

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.5
2 parents 4fed864 + f05859e commit f7dbcf6

File tree

9 files changed

+105
-35
lines changed

9 files changed

+105
-35
lines changed

system/Database/SQLite3/Connection.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,14 @@ protected function _fieldData(string $table): array
271271
for ($i = 0, $c = count($query); $i < $c; $i++) {
272272
$retVal[$i] = new stdClass();
273273

274-
$retVal[$i]->name = $query[$i]->name;
275-
$retVal[$i]->type = $query[$i]->type;
276-
$retVal[$i]->max_length = null;
277-
$retVal[$i]->default = $query[$i]->dflt_value;
278-
$retVal[$i]->primary_key = isset($query[$i]->pk) && (bool) $query[$i]->pk;
274+
$retVal[$i]->name = $query[$i]->name;
275+
$retVal[$i]->type = $query[$i]->type;
276+
$retVal[$i]->max_length = null;
277+
$retVal[$i]->default = $query[$i]->dflt_value;
278+
// "pk" (either zero for columns that are not part of the primary key,
279+
// or the 1-based index of the column within the primary key).
280+
// https://www.sqlite.org/pragma.html#pragma_table_info
281+
$retVal[$i]->primary_key = ($query[$i]->pk === 0) ? 0 : 1;
279282
$retVal[$i]->nullable = isset($query[$i]->notnull) && ! (bool) $query[$i]->notnull;
280283
}
281284

tests/system/Database/Live/ForgeTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -988,31 +988,31 @@ public function testAddFields(): void
988988
'type' => 'INTEGER',
989989
'max_length' => null,
990990
'default' => null,
991-
'primary_key' => true,
991+
'primary_key' => 1,
992992
'nullable' => true,
993993
],
994994
1 => [
995995
'name' => 'username',
996996
'type' => 'VARCHAR',
997997
'max_length' => null,
998998
'default' => null,
999-
'primary_key' => false,
999+
'primary_key' => 0,
10001000
'nullable' => false,
10011001
],
10021002
2 => [
10031003
'name' => 'name',
10041004
'type' => 'VARCHAR',
10051005
'max_length' => null,
10061006
'default' => null,
1007-
'primary_key' => false,
1007+
'primary_key' => 0,
10081008
'nullable' => true,
10091009
],
10101010
3 => [
10111011
'name' => 'active',
10121012
'type' => 'INTEGER',
10131013
'max_length' => null,
10141014
'default' => '0',
1015-
'primary_key' => false,
1015+
'primary_key' => 0,
10161016
'nullable' => false,
10171017
],
10181018
];

tests/system/Database/Live/SQLite3/GetFieldDataTest.php

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,55 +49,114 @@ public function testGetFieldData(): void
4949
'type' => 'INTEGER',
5050
'max_length' => null,
5151
'default' => null, // The default value is not defined.
52-
'primary_key' => true,
52+
'primary_key' => 1,
5353
'nullable' => true,
5454
],
5555
(object) [
5656
'name' => 'text_not_null',
5757
'type' => 'VARCHAR',
5858
'max_length' => null,
5959
'default' => null, // The default value is not defined.
60-
'primary_key' => false,
60+
'primary_key' => 0,
6161
'nullable' => false,
6262
],
6363
(object) [
6464
'name' => 'text_null',
6565
'type' => 'VARCHAR',
6666
'max_length' => null,
6767
'default' => null, // The default value is not defined.
68-
'primary_key' => false,
68+
'primary_key' => 0,
6969
'nullable' => true,
7070
],
7171
(object) [
7272
'name' => 'int_default_0',
7373
'type' => 'INT',
7474
'max_length' => null,
7575
'default' => '0', // int 0
76-
'primary_key' => false,
76+
'primary_key' => 0,
7777
'nullable' => false,
7878
],
7979
(object) [
8080
'name' => 'text_default_null',
8181
'type' => 'VARCHAR',
8282
'max_length' => null,
8383
'default' => 'NULL', // NULL value
84-
'primary_key' => false,
84+
'primary_key' => 0,
8585
'nullable' => true,
8686
],
8787
(object) [
8888
'name' => 'text_default_text_null',
8989
'type' => 'VARCHAR',
9090
'max_length' => null,
9191
'default' => "'null'", // string "null"
92-
'primary_key' => false,
92+
'primary_key' => 0,
9393
'nullable' => false,
9494
],
9595
(object) [
9696
'name' => 'text_default_abc',
9797
'type' => 'VARCHAR',
9898
'max_length' => null,
9999
'default' => "'abc'", // string "abc"
100-
'primary_key' => false,
100+
'primary_key' => 0,
101+
'nullable' => false,
102+
],
103+
]),
104+
json_encode($fields)
105+
);
106+
}
107+
108+
protected function createTableCompositePrimaryKey()
109+
{
110+
$this->forge->dropTable('test1', true);
111+
112+
$this->forge->addField([
113+
'pk1' => [
114+
'type' => 'VARCHAR',
115+
'constraint' => 64,
116+
],
117+
'pk2' => [
118+
'type' => 'VARCHAR',
119+
'constraint' => 64,
120+
],
121+
'text' => [
122+
'type' => 'VARCHAR',
123+
'constraint' => 64,
124+
],
125+
]);
126+
$this->forge->addPrimaryKey(['pk1', 'pk2']);
127+
$this->forge->createTable('test1');
128+
}
129+
130+
public function testGetFieldDataCompositePrimaryKey(): void
131+
{
132+
$this->createTableCompositePrimaryKey();
133+
134+
$fields = $this->db->getFieldData('test1');
135+
136+
$this->assertJsonStringEqualsJsonString(
137+
json_encode([
138+
(object) [
139+
'name' => 'pk1',
140+
'type' => 'VARCHAR',
141+
'max_length' => null,
142+
'default' => null,
143+
'primary_key' => 1,
144+
'nullable' => false,
145+
],
146+
(object) [
147+
'name' => 'pk2',
148+
'type' => 'VARCHAR',
149+
'max_length' => null,
150+
'default' => null,
151+
'primary_key' => 1,
152+
'nullable' => false,
153+
],
154+
(object) [
155+
'name' => 'text',
156+
'type' => 'VARCHAR',
157+
'max_length' => null,
158+
'default' => null,
159+
'primary_key' => 0,
101160
'nullable' => false,
102161
],
103162
]),

user_guide_src/source/database/metadata.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ supplying the table name:
102102
The following data is available from this function if supported by your
103103
database:
104104

105-
- name - column name
106-
- type - the type of the column
107-
- max_length - maximum length of the column
108-
- primary_key - integer ``1`` if the column is a primary key (all integer ``1``, even if there are multiple primary keys), otherwise integer ``0`` (This field is currently only available for MySQL and SQLite3)
109-
- nullable - boolean ``true`` if the column is nullable, otherwise boolean ``false``
110-
- default - the default value
105+
- ``name`` - column name
106+
- ``type`` - the type of the column
107+
- ``max_length`` - maximum length of the column
108+
- ``primary_key`` - integer ``1`` if the column is a primary key (all integer ``1``, even if there are multiple primary keys), otherwise integer ``0`` (This field is currently only available for ``MySQLi`` and ``SQLite3``)
109+
- ``nullable`` - boolean ``true`` if the column is nullable, otherwise boolean ``false``
110+
- ``default`` - the default value
111111

112112
.. note:: Since v4.4.0, SQLSRV supported ``nullable``.
113113

user_guide_src/source/testing/fabricator.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Generating Test Data
33
####################
44

55
Often you will need sample data for your application to run its tests. The ``Fabricator`` class
6-
uses fzaninotto's `Faker <https://github.com/FakerPHP/Faker>`_ to turn models into generators
6+
uses `Faker <https://github.com/FakerPHP/Faker>`_ to turn models into generators
77
of random data. Use fabricators in your seeds or test cases to stage fake data for your unit tests.
88

99
.. contents::
@@ -56,6 +56,7 @@ method where you can define exactly what the faked data should look like:
5656
Notice in this example how the first three values are equivalent to the formatters from before. However for ``avatar``
5757
we have requested an image size other than the default and ``login`` uses a conditional based on app configuration,
5858
neither of which are possible using the ``$formatters`` parameter.
59+
5960
You may want to keep your test data separate from your production models, so it is a good practice to define
6061
a child class in your test support folder:
6162

user_guide_src/source/testing/fabricator/005.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22

33
namespace App\Models;
44

5+
use Faker\Generator;
6+
57
class UserModel
68
{
79
// ...
810

911
public function fake(Generator &$faker)
1012
{
1113
return [
12-
'first' => $faker->firstName,
13-
'email' => $faker->email,
14-
'phone' => $faker->phoneNumber,
15-
'avatar' => Faker\Provider\Image::imageUrl(800, 400),
14+
'first' => $faker->firstName(),
15+
'email' => $faker->email(),
16+
'phone' => $faker->phoneNumber(),
17+
'avatar' => \Faker\Provider\Image::imageUrl(800, 400),
1618
'login' => config('Auth')->allowRemembering ? date('Y-m-d') : null,
1719
];
1820

1921
/*
2022
* Or you can return a return type object.
2123
2224
return new User([
23-
'first' => $faker->firstName,
24-
'email' => $faker->email,
25-
'phone' => $faker->phoneNumber,
26-
'avatar' => Faker\Provider\Image::imageUrl(800, 400),
25+
'first' => $faker->firstName(),
26+
'email' => $faker->email(),
27+
'phone' => $faker->phoneNumber(),
28+
'avatar' => \Faker\Provider\Image::imageUrl(800, 400),
2729
'login' => config('Auth')->allowRemembering ? date('Y-m-d') : null,
2830
]);
2931

user_guide_src/source/testing/fabricator/006.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
namespace Tests\Support\Models;
44

55
use App\Models\UserModel;
6+
use Faker\Generator;
67

78
class UserFabricator extends UserModel
89
{
9-
public function fake(&$faker)
10+
public function fake(Generator &$faker)
1011
{
1112
// ...
1213
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22

33
use CodeIgniter\Test\Fabricator;
4+
use Tests\Support\Models\UserFabricator;
45

5-
$fabricator = new Fabricator(\UserFabricator::class);
6+
$fabricator = new Fabricator(UserFabricator::class);
67
$testUser = $fabricator->make();
78
print_r($testUser);

user_guide_src/source/testing/fabricator/021.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace App\Models;
44

5+
use CodeIgniter\Test\Fabricator;
6+
use Faker\Generator;
7+
58
class UserModel
69
{
710
protected $table = 'users';
811

912
public function fake(Generator &$faker)
1013
{
1114
return [
12-
'first' => $faker->firstName,
13-
'email' => $faker->email,
15+
'first' => $faker->firstName(),
16+
'email' => $faker->email(),
1417
'group_id' => mt_rand(1, Fabricator::getCount('groups')),
1518
];
1619
}

0 commit comments

Comments
 (0)