Skip to content
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 @@ -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.
Expand Down
23 changes: 21 additions & 2 deletions system/Database/SQLSRV/Forge.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<<SQL
SELECT dc.name AS constraint_name
FROM sys.default_constraints dc
JOIN sys.columns c
ON dc.parent_object_id = c.object_id
AND dc.parent_column_id = c.column_id
WHERE dc.parent_object_id = OBJECT_ID(N'{$fullTable}')
AND c.name = N'{$colName}';
SQL;

$toDrop = $this->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.
Expand Down
22 changes: 22 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.6.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
Expand Down
2 changes: 1 addition & 1 deletion utils/phpstan-baseline/loader.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 2836 errors
# total 2837 errors
includes:
- argument.type.neon
- assign.propertyType.neon
Expand Down
4 changes: 2 additions & 2 deletions utils/phpstan-baseline/property.notFound.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 58 errors
# total 59 errors

parameters:
ignoreErrors:
Expand All @@ -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

-
Expand Down