diff --git a/system/Database/Postgre/Forge.php b/system/Database/Postgre/Forge.php index 4f7c75ea4520..6791da6169fa 100644 --- a/system/Database/Postgre/Forge.php +++ b/system/Database/Postgre/Forge.php @@ -130,7 +130,7 @@ protected function _alterTable(string $alterType, string $table, $field) protected function _processColumn(array $field): string { return $this->db->escapeIdentifiers($field['name']) - . ' ' . $field['type'] . $field['length'] + . ' ' . $field['type'] . ($field['type'] === 'text' ? '' : $field['length']) . $field['default'] . $field['null'] . $field['auto_increment'] diff --git a/system/Database/SQLSRV/Forge.php b/system/Database/SQLSRV/Forge.php index bb802149edaf..2ad8db1d2aba 100755 --- a/system/Database/SQLSRV/Forge.php +++ b/system/Database/SQLSRV/Forge.php @@ -144,7 +144,25 @@ protected function _alterTable(string $alterType, string $table, $field) } } - $sql = 'ALTER TABLE ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table) . ' DROP '; + $fullTable = $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table); + + // Drop default constraints + $fields = implode(',', $this->db->escape((array) $field)); + + $sql = <<db->query($sql)->getResultArray() as $index) { + $this->db->query('ALTER TABLE ' . $fullTable . ' DROP CONSTRAINT ' . $index['name'] . ''); + } + + $sql = 'ALTER TABLE ' . $fullTable . ' DROP '; $fields = array_map(static fn ($item) => 'COLUMN [' . trim($item) . ']', (array) $field); @@ -263,7 +281,7 @@ protected function _processColumn(array $field): string { return $this->db->escapeIdentifiers($field['name']) . (empty($field['new_name']) ? '' : ' ' . $this->db->escapeIdentifiers($field['new_name'])) - . ' ' . $field['type'] . $field['length'] + . ' ' . $field['type'] . ($field['type'] === 'text' ? '' : $field['length']) . $field['default'] . $field['null'] . $field['auto_increment'] diff --git a/tests/system/Database/Live/ForgeTest.php b/tests/system/Database/Live/ForgeTest.php index d0abc89987af..9cf4817d9f36 100644 --- a/tests/system/Database/Live/ForgeTest.php +++ b/tests/system/Database/Live/ForgeTest.php @@ -1355,4 +1355,21 @@ public function testDropKey() $this->forge->dropTable('key_test_users', true); } + + public function testAddTextColumnWithConstraint() + { + // some DBMS do not allow a constraint for type TEXT + $result = $this->forge->addColumn('user', [ + 'text_with_constraint' => ['type' => 'text', 'constraint' => 255, 'default' => ''], + ]); + + $this->assertTrue($this->db->fieldExists('text_with_constraint', 'user')); + + // SQLSRV requires dropping default constraint before dropping column + $result = $this->forge->dropColumn('user', 'text_with_constraint'); + + $this->db->resetDataCache(); + + $this->assertFalse($this->db->fieldExists('text_with_constraint', 'user')); + } } diff --git a/user_guide_src/source/changelogs/v4.3.0.rst b/user_guide_src/source/changelogs/v4.3.0.rst index eaf84cf07ff0..d259ed3a7c6c 100644 --- a/user_guide_src/source/changelogs/v4.3.0.rst +++ b/user_guide_src/source/changelogs/v4.3.0.rst @@ -70,6 +70,7 @@ Others - Added new Form helper function :php:func:`validation_errors()`, :php:func:`validation_list_errors()` and :php:func:`validation_show_error()` to display Validation Errors. - Now you can autoload helpers by **app/Config/Autoload.php**. - ``BaseConnection::escape()`` now excludes the ``RawSql`` data type. This allows passing SQL strings into data. +- SQLSRV now automatically drops ``DEFAULT`` constraint when using ``Forge::dropColumn()``. - Added :ref:`Time::toDatabase() ` to get a datetime string that can be used with databases regardless of locale. Changes