Skip to content

Commit 4721884

Browse files
committed
fix: Forge::modifyColumn() for SQLSRV handler
1 parent 27770e7 commit 4721884

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

system/Database/SQLSRV/Forge.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,27 @@ protected function _alterTable(string $alterType, string $table, $processedField
253253
}
254254

255255
if (! empty($field['default'])) {
256-
$sqls[] = $sql . ' ALTER COLUMN ADD CONSTRAINT ' . $this->db->escapeIdentifiers($field['name']) . '_def'
257-
. " DEFAULT {$field['default']} FOR " . $this->db->escapeIdentifiers($field['name']);
256+
$fullTable = $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->escapeIdentifiers($table);
257+
$colName = $field['name']; // bare, for sys.columns lookup
258+
259+
// find the existing default constraint name for this column
260+
$findSql = <<<SQL
261+
SELECT dc.name AS constraint_name
262+
FROM sys.default_constraints dc
263+
JOIN sys.columns c
264+
ON dc.parent_object_id = c.object_id
265+
AND dc.parent_column_id = c.column_id
266+
WHERE dc.parent_object_id = OBJECT_ID(N'{$fullTable}')
267+
AND c.name = N'{$colName}';
268+
SQL;
269+
270+
$toDrop = $this->db->query($findSql)->getRowArray();
271+
if (isset($toDrop['constraint_name']) && $toDrop['constraint_name'] !== '') {
272+
$sqls[] = $sql . ' DROP CONSTRAINT ' . $this->db->escapeIdentifiers($toDrop['constraint_name']);
273+
}
274+
275+
$sqls[] = $sql . ' ADD CONSTRAINT ' . $this->db->escapeIdentifiers($field['name'] . '_def')
276+
. "{$field['default']} FOR " . $this->db->escapeIdentifiers($field['name']);
258277
}
259278

260279
$nullable = true; // Nullable by default.

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 2836 errors
1+
# total 2837 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/property.notFound.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 58 errors
1+
# total 59 errors
22

33
parameters:
44
ignoreErrors:
@@ -14,7 +14,7 @@ parameters:
1414

1515
-
1616
message: '#^Access to an undefined property CodeIgniter\\Database\\BaseConnection\:\:\$schema\.$#'
17-
count: 13
17+
count: 14
1818
path: ../../system/Database/SQLSRV/Forge.php
1919

2020
-

0 commit comments

Comments
 (0)