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

Postgre & SQLSRV - Should Never Have A Field Length For TEXT #6405

Merged
merged 7 commits into from
Aug 30, 2022
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
2 changes: 1 addition & 1 deletion system/Database/Postgre/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
22 changes: 20 additions & 2 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<SQL
SELECT name
FROM SYS.DEFAULT_CONSTRAINTS
WHERE PARENT_OBJECT_ID = OBJECT_ID('{$fullTable}')
AND PARENT_COLUMN_ID IN (
SELECT column_id FROM sys.columns WHERE NAME IN ({$fields}) AND object_id = OBJECT_ID(N'{$fullTable}')
)
SQL;

foreach ($this->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);

Expand Down Expand Up @@ -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']
Expand Down
17 changes: 17 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, this commit is not good. Because the document change has nothing to do with the code change.

We added the explanation for commits and commit messages:
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#committing

- Added :ref:`Time::toDatabase() <time-todatabase>` to get a datetime string that can be used with databases regardless of locale.

Changes
Expand Down