From 27770e7b1eddb2b65b335554388b459a992f3c4a Mon Sep 17 00:00:00 2001 From: michalsn Date: Tue, 12 Aug 2025 18:55:26 +0200 Subject: [PATCH 1/2] fix: Forge::modifyColumn() for Postgre handler --- system/Database/Postgre/Forge.php | 2 +- tests/system/Database/Live/ForgeTest.php | 22 +++++++++++++++++++++ user_guide_src/source/changelogs/v4.6.4.rst | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/system/Database/Postgre/Forge.php b/system/Database/Postgre/Forge.php index 8b18d7d48ca7..d8e7d5886604 100644 --- a/system/Database/Postgre/Forge.php +++ b/system/Database/Postgre/Forge.php @@ -109,7 +109,7 @@ protected function _alterTable(string $alterType, string $table, $processedField if (! empty($field['default'])) { $sqls[] = $sql . ' ALTER COLUMN ' . $this->db->escapeIdentifiers($field['name']) - . " SET DEFAULT {$field['default']}"; + . " SET {$field['default']}"; } $nullable = true; // Nullable by default. diff --git a/tests/system/Database/Live/ForgeTest.php b/tests/system/Database/Live/ForgeTest.php index c2e1e92b0357..9faf9d79afb7 100644 --- a/tests/system/Database/Live/ForgeTest.php +++ b/tests/system/Database/Live/ForgeTest.php @@ -1818,6 +1818,28 @@ public function testProcessIndexesWithForeignKeyOnly(): void $this->forge->dropTable('user2', true); } + public function testChangeDefaultFieldValueWithModifyColumn(): void + { + $this->forge->addField([ + 'delivery_sum' => ['type' => 'int', 'constraint' => 6, 'default' => 10], + 'delivery_free_sum' => ['type' => 'int', 'constraint' => 6, 'default' => 10], + ])->addKey('id', true)->createTable('test_stores', true); + + $this->forge->modifyColumn('test_stores', [ + 'delivery_sum' => ['type' => 'int', 'constraint' => 6, 'default' => 100], + 'delivery_free_sum' => ['type' => 'int', 'constraint' => 6, 'default' => 1000], + ]); + + $expected = ['delivery_sum' => '100', 'delivery_free_sum' => '1000']; + + $fields = $this->db->getFieldData('test_stores'); + $results = array_column($fields, 'default', 'name'); + + $this->assertSame($expected, $results); + + $this->forge->dropTable('test_stores', true); + } + private function createUser2TableWithKeys(): void { $fields = [ diff --git a/user_guide_src/source/changelogs/v4.6.4.rst b/user_guide_src/source/changelogs/v4.6.4.rst index 705901e231af..c5e259894c43 100644 --- a/user_guide_src/source/changelogs/v4.6.4.rst +++ b/user_guide_src/source/changelogs/v4.6.4.rst @@ -32,6 +32,7 @@ Bugs Fixed - **Database:** Fixed a bug in ``Database::connect()`` which was causing to store non-shared connection instances in shared cache. - **Database:** Fixed a bug in ``Connection::getFieldData()`` for ``SQLSRV`` and ``OCI8`` where extra characters were returned in column default values (specific to those handlers), instead of following the convention used by other drivers. +- **Forge:** Fixed a bug in ``Postgre`` and ``SQLSRV`` where changing a column's default value using ``Forge::modifyColumn()`` method produced incorrect SQL syntax. See the repo's `CHANGELOG.md `_ From 472188497b04f79cd8fbe420793fef343737066d Mon Sep 17 00:00:00 2001 From: michalsn Date: Thu, 14 Aug 2025 08:49:26 +0200 Subject: [PATCH 2/2] fix: Forge::modifyColumn() for SQLSRV handler --- system/Database/SQLSRV/Forge.php | 23 +++++++++++++++++-- utils/phpstan-baseline/loader.neon | 2 +- utils/phpstan-baseline/property.notFound.neon | 4 ++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/system/Database/SQLSRV/Forge.php b/system/Database/SQLSRV/Forge.php index f35d8e7bbca2..1a0756f51de7 100644 --- a/system/Database/SQLSRV/Forge.php +++ b/system/Database/SQLSRV/Forge.php @@ -253,8 +253,27 @@ protected function _alterTable(string $alterType, string $table, $processedField } if (! empty($field['default'])) { - $sqls[] = $sql . ' ALTER COLUMN ADD CONSTRAINT ' . $this->db->escapeIdentifiers($field['name']) . '_def' - . " DEFAULT {$field['default']} FOR " . $this->db->escapeIdentifiers($field['name']); + $fullTable = $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table); + $colName = $field['name']; // bare, for sys.columns lookup + + // find the existing default constraint name for this column + $findSql = <<db->query($findSql)->getRowArray(); + if (isset($toDrop['constraint_name']) && $toDrop['constraint_name'] !== '') { + $sqls[] = $sql . ' DROP CONSTRAINT ' . $this->db->escapeIdentifiers($toDrop['constraint_name']); + } + + $sqls[] = $sql . ' ADD CONSTRAINT ' . $this->db->escapeIdentifiers($field['name'] . '_def') + . "{$field['default']} FOR " . $this->db->escapeIdentifiers($field['name']); } $nullable = true; // Nullable by default. diff --git a/utils/phpstan-baseline/loader.neon b/utils/phpstan-baseline/loader.neon index 08c8f5c3fcef..a92562caf39d 100644 --- a/utils/phpstan-baseline/loader.neon +++ b/utils/phpstan-baseline/loader.neon @@ -1,4 +1,4 @@ -# total 2836 errors +# total 2837 errors includes: - argument.type.neon - assign.propertyType.neon diff --git a/utils/phpstan-baseline/property.notFound.neon b/utils/phpstan-baseline/property.notFound.neon index f7f0db25ce63..282a9234fddd 100644 --- a/utils/phpstan-baseline/property.notFound.neon +++ b/utils/phpstan-baseline/property.notFound.neon @@ -1,4 +1,4 @@ -# total 58 errors +# total 59 errors parameters: ignoreErrors: @@ -14,7 +14,7 @@ parameters: - message: '#^Access to an undefined property CodeIgniter\\Database\\BaseConnection\:\:\$schema\.$#' - count: 13 + count: 14 path: ../../system/Database/SQLSRV/Forge.php -