Skip to content

Commit

Permalink
Fix default null not being reflected by SqlServer
Browse files Browse the repository at this point in the history
Apply patch from 'Josh Rehm' to fix null default values from being
stomped on when reflecting table schema.

Fixes #3615
  • Loading branch information
markstory committed Mar 20, 2013
1 parent b821505 commit 729ef8f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/Cake/Model/Datasource/Database/Sqlserver.php
Expand Up @@ -216,14 +216,20 @@ public function describe($model) {
$fields[$field] = array(
'type' => $this->column($column),
'null' => ($column->Null === 'YES' ? true : false),
'default' => preg_replace("/^[(]{1,2}'?([^')]*)?'?[)]{1,2}$/", "$1", $column->Default),
'default' => $column->Default,
'length' => $this->length($column),
'key' => ($column->Key == '1') ? 'primary' : false
);

if ($fields[$field]['default'] === 'null') {
$fields[$field]['default'] = null;
} else {
}
if ($fields[$field]['default'] !== null) {
$fields[$field]['default'] = preg_replace(
"/^[(]{1,2}'?([^')]*)?'?[)]{1,2}$/",
"$1",
$fields[$field]['default']
);
$this->value($fields[$field]['default'], $fields[$field]['type']);
}

Expand Down
20 changes: 18 additions & 2 deletions lib/Cake/Test/Case/Model/Datasource/Database/SqlserverTest.php
Expand Up @@ -448,7 +448,16 @@ public function testDescribe() {
'Length' => 72,
'Null' => 'NO',
'Size' => ''
)
),
(object)array(
'Default' => null,
'Field' => 'parent_id',
'Key' => '0',
'Type' => 'bigint',
'Length' => 8,
'Null' => 'YES',
'Size' => '0',
),
));
$this->db->executeResultsStack = array($SqlserverTableDescription);
$dummyModel = $this->model;
Expand Down Expand Up @@ -478,9 +487,16 @@ public function testDescribe() {
'default' => '',
'length' => 36,
'key' => 'primary'
)
),
'parent_id' => array(
'type' => 'biginteger',
'null' => true,
'default' => null,
'length' => 8,
),
);
$this->assertEquals($expected, $result);
$this->assertSame($expected['parent_id'], $result['parent_id']);
}

/**
Expand Down

0 comments on commit 729ef8f

Please sign in to comment.