Repository navigation
fix(Database): escape table identifiers in SQLite3 foreign_key_list PRAGMA #10732
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
Merged
paulbalandan
merged 2 commits into
codeigniter4:develop
from
gr8man:fix/sqlite3-foreign-key-data-table-escape
Nov 14, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
tests/system/Database/Live/SQLite3/GetForeignKeyDataTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?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 CodeIgniter\Database\Live\SQLite3; | ||
|
|
||
| use CodeIgniter\Test\CIUnitTestCase; | ||
| use Config\Database; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| #[Group('DatabaseLive')] | ||
| final class GetForeignKeyDataTest extends CIUnitTestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->db = Database::connect($this->DBGroup); | ||
| if ($this->db->DBDriver !== 'SQLite3') { | ||
| $this->markTestSkipped('This test is only for SQLite3.'); | ||
| } | ||
|
|
||
| $config = [ | ||
| 'DBDriver' => 'SQLite3', | ||
| 'database' => 'database.db', | ||
| 'DBDebug' => true, | ||
| ]; | ||
| $this->db = db_connect($config, false); | ||
| } | ||
|
|
||
| public function testGetForeignKeyDataWithSpecialTableName(): void | ||
| { | ||
| $this->db->query('CREATE TABLE `test-parent` (id INTEGER PRIMARY KEY)'); | ||
| $this->db->query('CREATE TABLE `test-child` (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES `test-parent`(id))'); | ||
|
|
||
| $foreignKeys = $this->db->getForeignKeyData('test-child'); | ||
|
paulbalandan marked this conversation as resolved.
Resolved
|
||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS `test-child`'); | ||
| $this->db->query('DROP TABLE IF EXISTS `test-parent`'); | ||
|
|
||
| $this->assertCount(1, $foreignKeys); | ||
| $firstKey = reset($foreignKeys); | ||
| $this->assertSame('test-child', $firstKey->table_name); | ||
| $this->assertSame(['parent_id'], $firstKey->column_name); | ||
| $this->assertSame('test-parent', $firstKey->foreign_table_name); | ||
| $this->assertSame(['id'], $firstKey->foreign_column_name); | ||
| } | ||
|
|
||
| public function testGetForeignKeyDataWithAttachedDatabase(): void | ||
| { | ||
| $this->db->query('ATTACH DATABASE ":memory:" AS `test_schema`'); | ||
|
|
||
| $this->db->query('CREATE TABLE `test_schema`.`attached-parent` (id INTEGER PRIMARY KEY)'); | ||
| $this->db->query('CREATE TABLE `test_schema`.`attached-child` (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES `attached-parent`(id))'); | ||
|
|
||
| $foreignKeys = $this->db->getForeignKeyData('test_schema.attached-child'); | ||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-child`'); | ||
| $this->db->query('DROP TABLE IF EXISTS `test_schema`.`attached-parent`'); | ||
| $this->db->query('DETACH DATABASE `test_schema`'); | ||
|
|
||
| $this->assertCount(1, $foreignKeys); | ||
| $firstKey = reset($foreignKeys); | ||
| $this->assertSame('test_schema.attached-child', $firstKey->table_name); | ||
| $this->assertSame(['p_id'], $firstKey->column_name); | ||
| $this->assertSame('attached-parent', $firstKey->foreign_table_name); | ||
| $this->assertSame(['id'], $firstKey->foreign_column_name); | ||
| } | ||
|
|
||
| public function testGetForeignKeyDataWithParenthesesInTableName(): void | ||
| { | ||
| $this->db->query('CREATE TABLE "parent(v1)" (id INTEGER PRIMARY KEY)'); | ||
| $this->db->query('CREATE TABLE "child(v2)" (id INTEGER PRIMARY KEY, parent_id INTEGER, FOREIGN KEY (parent_id) REFERENCES "parent(v1)"(id))'); | ||
|
|
||
| $foreignKeys = $this->db->getForeignKeyData('child(v2)'); | ||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS "child(v2)"'); | ||
| $this->db->query('DROP TABLE IF EXISTS "parent(v1)"'); | ||
|
|
||
| $this->assertCount(1, $foreignKeys); | ||
| $firstKey = reset($foreignKeys); | ||
| $this->assertSame('child(v2)', $firstKey->table_name); | ||
| $this->assertSame(['parent_id'], $firstKey->column_name); | ||
| $this->assertSame('parent(v1)', $firstKey->foreign_table_name); | ||
| $this->assertSame(['id'], $firstKey->foreign_column_name); | ||
| } | ||
|
|
||
| public function testGetForeignKeyDataWithQuotesAndSpacesInTableName(): void | ||
| { | ||
| $this->db->query('CREATE TABLE "parent table" (id INTEGER PRIMARY KEY)'); | ||
| $this->db->query('CREATE TABLE "child\'quote with space" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent table"(id))'); | ||
|
|
||
| $foreignKeys = $this->db->getForeignKeyData("child'quote with space"); | ||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS "child\'quote with space"'); | ||
| $this->db->query('DROP TABLE IF EXISTS "parent table"'); | ||
|
|
||
| $this->assertCount(1, $foreignKeys); | ||
| $firstKey = reset($foreignKeys); | ||
| $this->assertSame("child'quote with space", $firstKey->table_name); | ||
| $this->assertSame(['p_id'], $firstKey->column_name); | ||
| $this->assertSame('parent table', $firstKey->foreign_table_name); | ||
| $this->assertSame(['id'], $firstKey->foreign_column_name); | ||
| } | ||
|
|
||
| public function testGetForeignKeyDataWithNumericAndReservedWordTableName(): void | ||
| { | ||
| $this->db->query('CREATE TABLE "order" (id INTEGER PRIMARY KEY)'); | ||
| $this->db->query('CREATE TABLE "12345" (id INTEGER PRIMARY KEY, order_id INTEGER, FOREIGN KEY (order_id) REFERENCES "order"(id))'); | ||
|
|
||
| $foreignKeys = $this->db->getForeignKeyData('12345'); | ||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS "12345"'); | ||
| $this->db->query('DROP TABLE IF EXISTS "order"'); | ||
|
|
||
| $this->assertCount(1, $foreignKeys); | ||
| $firstKey = reset($foreignKeys); | ||
| $this->assertSame('12345', $firstKey->table_name); | ||
| $this->assertSame(['order_id'], $firstKey->column_name); | ||
| $this->assertSame('order', $firstKey->foreign_table_name); | ||
| $this->assertSame(['id'], $firstKey->foreign_column_name); | ||
| } | ||
|
|
||
| public function testGetForeignKeyDataWithAttachedDatabaseSpecialIdentifiers(): void | ||
| { | ||
| $this->db->query('ATTACH DATABASE ":memory:" AS "schema-db(1)"'); | ||
|
|
||
| $this->db->query('CREATE TABLE "schema-db(1)"."parent(v1)" (id INTEGER PRIMARY KEY)'); | ||
| $this->db->query('CREATE TABLE "schema-db(1)"."child(v2)" (id INTEGER PRIMARY KEY, p_id INTEGER, FOREIGN KEY (p_id) REFERENCES "parent(v1)"(id))'); | ||
|
|
||
| $foreignKeys = $this->db->getForeignKeyData('schema-db(1).child(v2)'); | ||
|
|
||
| $this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."child(v2)"'); | ||
| $this->db->query('DROP TABLE IF EXISTS "schema-db(1)"."parent(v1)"'); | ||
| $this->db->query('DETACH DATABASE "schema-db(1)"'); | ||
|
|
||
| $this->assertCount(1, $foreignKeys); | ||
| $firstKey = reset($foreignKeys); | ||
| $this->assertSame('schema-db(1).child(v2)', $firstKey->table_name); | ||
| $this->assertSame(['p_id'], $firstKey->column_name); | ||
| $this->assertSame('parent(v1)', $firstKey->foreign_table_name); | ||
| $this->assertSame(['id'], $firstKey->foreign_column_name); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.