Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding foreign keys with only string fields #4988

Merged
merged 1 commit into from
Aug 6, 2021
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
19 changes: 16 additions & 3 deletions system/Database/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,25 @@ public function addField($field)
throw new InvalidArgumentException('Field information is required for that operation.');
}

$this->fields[] = $field;
$fieldName = explode(' ', $field, 2)[0];
$fieldName = trim($fieldName, '`\'"');

$this->fields[$fieldName] = $field;
}
}

if (is_array($field)) {
$this->fields = array_merge($this->fields, $field);
foreach ($field as $idx => $f) {
if (is_string($f)) {
$this->addField($f);

continue;
}

if (is_array($f)) {
$this->fields = array_merge($this->fields, [$idx => $f]);
}
}
}

return $this;
Expand Down Expand Up @@ -878,7 +891,7 @@ protected function _processFields(bool $createTable = false): array
$fields = [];

foreach ($this->fields as $key => $attributes) {
if (is_int($key) && ! is_array($attributes)) {
if (! is_array($attributes)) {
$fields[] = ['_literal' => $attributes];

continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public function up()
'data' => ['type' => 'BLOB', 'null' => false],
]);
$this->forge->addKey('id', true);
$this->forge->addKey('timestamp');
$this->forge->createTable('ci_sessions', true);
}

Expand All @@ -140,7 +139,6 @@ public function up()
"data bytea DEFAULT '' NOT NULL",
]);
$this->forge->addKey('id', true);
$this->forge->addKey('timestamp');
$this->forge->createTable('ci_sessions', true);
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,43 @@ public function testForeignKey()
$this->forge->dropTable('forge_test_users', true);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4986
*/
public function testForeignKeyAddingWithStringFields()
{
if ($this->db->DBDriver !== 'MySQLi') {
$this->markTestSkipped('Testing only on MySQLi but fix expands to all DBs.');
}

$attributes = ['ENGINE' => 'InnoDB'];

$this->forge->addField([
'`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
'`name` VARCHAR(255) NOT NULL',
])->createTable('forge_test_users', true, $attributes);

$this->forge
->addField([
'`id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY',
'`users_id` INT(11) NOT NULL',
'`name` VARCHAR(255) NOT NULL',
])
->addForeignKey('users_id', 'forge_test_users', 'id', 'CASCADE', 'CASCADE')
->createTable('forge_test_invoices', true, $attributes);

$foreignKeyData = $this->db->getForeignKeyData('forge_test_invoices')[0];

$this->assertSame($this->db->DBPrefix . 'forge_test_invoices_users_id_foreign', $foreignKeyData->constraint_name);
$this->assertSame('users_id', $foreignKeyData->column_name);
$this->assertSame('id', $foreignKeyData->foreign_column_name);
$this->assertSame($this->db->DBPrefix . 'forge_test_invoices', $foreignKeyData->table_name);
$this->assertSame($this->db->DBPrefix . 'forge_test_users', $foreignKeyData->foreign_table_name);

$this->forge->dropTable('forge_test_invoices', true);
$this->forge->dropTable('forge_test_users', true);
}

public function testForeignKeyFieldNotExistException()
{
$this->expectException(DatabaseException::class);
Expand Down