1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Tests\Functional\Schema; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Schema\Column; |
8
|
|
|
use Doctrine\DBAL\Schema\ColumnDiff; |
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
10
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
11
|
|
|
use Doctrine\DBAL\Types\Type; |
12
|
|
|
use function current; |
13
|
|
|
|
14
|
|
|
class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase |
15
|
|
|
{ |
16
|
|
|
protected function getPlatformName() : string |
17
|
|
|
{ |
18
|
|
|
return 'mssql'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @group DBAL-255 |
23
|
|
|
*/ |
24
|
|
|
public function testDropColumnConstraints() : void |
25
|
|
|
{ |
26
|
|
|
$table = new Table('sqlsrv_drop_column'); |
27
|
|
|
$table->addColumn('id', 'integer'); |
28
|
|
|
$table->addColumn('todrop', 'decimal', ['default' => 10.2]); |
29
|
|
|
|
30
|
|
|
$this->schemaManager->createTable($table); |
31
|
|
|
|
32
|
|
|
$diff = new TableDiff('sqlsrv_drop_column', [], [], ['todrop' => new Column('todrop', Type::getType('decimal'))]); |
33
|
|
|
$this->schemaManager->alterTable($diff); |
34
|
|
|
|
35
|
|
|
$columns = $this->schemaManager->listTableColumns('sqlsrv_drop_column'); |
36
|
|
|
self::assertCount(1, $columns); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testColumnCollation() : void |
40
|
|
|
{ |
41
|
|
|
$table = new Table($tableName = 'test_collation'); |
42
|
|
|
$column = $table->addColumn('test', 'string', ['length' => 32]); |
43
|
|
|
|
44
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
45
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
46
|
|
|
|
47
|
|
|
self::assertTrue($columns['test']->hasPlatformOption('collation')); // SQL Server should report a default collation on the column |
48
|
|
|
|
49
|
|
|
$column->setPlatformOption('collation', $collation = 'Icelandic_CS_AS'); |
50
|
|
|
|
51
|
|
|
$this->schemaManager->dropAndCreateTable($table); |
52
|
|
|
$columns = $this->schemaManager->listTableColumns($tableName); |
53
|
|
|
|
54
|
|
|
self::assertEquals($collation, $columns['test']->getPlatformOption('collation')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testDefaultConstraints() : void |
58
|
|
|
{ |
59
|
|
|
$table = new Table('sqlsrv_default_constraints'); |
60
|
|
|
$table->addColumn('no_default', 'string', ['length' => 32]); |
61
|
|
|
$table->addColumn('df_integer', 'integer', ['default' => 666]); |
62
|
|
|
$table->addColumn('df_string_1', 'string', [ |
63
|
|
|
'length' => 32, |
64
|
|
|
'default' => 'foobar', |
65
|
|
|
]); |
66
|
|
|
$table->addColumn('df_string_2', 'string', [ |
67
|
|
|
'length' => 32, |
68
|
|
|
'default' => 'Doctrine rocks!!!', |
69
|
|
|
]); |
70
|
|
|
$table->addColumn('df_string_3', 'string', [ |
71
|
|
|
'length' => 32, |
72
|
|
|
'default' => 'another default value', |
73
|
|
|
]); |
74
|
|
|
$table->addColumn('df_string_4', 'string', [ |
75
|
|
|
'length' => 32, |
76
|
|
|
'default' => 'column to rename', |
77
|
|
|
]); |
78
|
|
|
$table->addColumn('df_boolean', 'boolean', ['default' => true]); |
79
|
|
|
|
80
|
|
|
$this->schemaManager->createTable($table); |
81
|
|
|
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints'); |
82
|
|
|
|
83
|
|
|
self::assertNull($columns['no_default']->getDefault()); |
84
|
|
|
self::assertEquals(666, $columns['df_integer']->getDefault()); |
85
|
|
|
self::assertEquals('foobar', $columns['df_string_1']->getDefault()); |
86
|
|
|
self::assertEquals('Doctrine rocks!!!', $columns['df_string_2']->getDefault()); |
87
|
|
|
self::assertEquals('another default value', $columns['df_string_3']->getDefault()); |
88
|
|
|
self::assertEquals(1, $columns['df_boolean']->getDefault()); |
89
|
|
|
|
90
|
|
|
$diff = new TableDiff( |
91
|
|
|
'sqlsrv_default_constraints', |
92
|
|
|
[], |
93
|
|
|
[ |
94
|
|
|
'df_integer' => new ColumnDiff( |
95
|
|
|
'df_integer', |
96
|
|
|
new Column('df_integer', Type::getType('integer'), ['default' => 0]), |
97
|
|
|
['default'], |
98
|
|
|
new Column('df_integer', Type::getType('integer'), ['default' => 666]) |
99
|
|
|
), |
100
|
|
|
'df_string_2' => new ColumnDiff( |
101
|
|
|
'df_string_2', |
102
|
|
|
new Column('df_string_2', Type::getType('string'), ['length' => 32]), |
103
|
|
|
['default'], |
104
|
|
|
new Column('df_string_2', Type::getType('string'), [ |
105
|
|
|
'length' => 32, |
106
|
|
|
'default' => 'Doctrine rocks!!!', |
107
|
|
|
]) |
108
|
|
|
), |
109
|
|
|
'df_string_3' => new ColumnDiff( |
110
|
|
|
'df_string_3', |
111
|
|
|
new Column('df_string_3', Type::getType('string'), ['length' => 50, 'default' => 'another default value']), |
112
|
|
|
['length'], |
113
|
|
|
new Column('df_string_3', Type::getType('string'), ['length' => 50, 'default' => 'another default value']) |
114
|
|
|
), |
115
|
|
|
'df_boolean' => new ColumnDiff( |
116
|
|
|
'df_boolean', |
117
|
|
|
new Column('df_boolean', Type::getType('boolean'), ['default' => false]), |
118
|
|
|
['default'], |
119
|
|
|
new Column('df_boolean', Type::getType('boolean'), ['default' => true]) |
120
|
|
|
), |
121
|
|
|
], |
122
|
|
|
[ |
123
|
|
|
'df_string_1' => new Column('df_string_1', Type::getType('string'), ['length' => 32]), |
124
|
|
|
], |
125
|
|
|
[], |
126
|
|
|
[], |
127
|
|
|
[], |
128
|
|
|
$table |
129
|
|
|
); |
130
|
|
|
$diff->newName = 'sqlsrv_default_constraints'; |
131
|
|
|
$diff->renamedColumns['df_string_4'] = new Column( |
132
|
|
|
'df_string_renamed', |
133
|
|
|
Type::getType('string'), |
134
|
|
|
['default' => 'column to rename'] |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->schemaManager->alterTable($diff); |
138
|
|
|
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints'); |
139
|
|
|
|
140
|
|
|
self::assertNull($columns['no_default']->getDefault()); |
141
|
|
|
self::assertEquals(0, $columns['df_integer']->getDefault()); |
142
|
|
|
self::assertNull($columns['df_string_2']->getDefault()); |
143
|
|
|
self::assertEquals('another default value', $columns['df_string_3']->getDefault()); |
144
|
|
|
self::assertEquals(0, $columns['df_boolean']->getDefault()); |
145
|
|
|
self::assertEquals('column to rename', $columns['df_string_renamed']->getDefault()); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Test that column default constraints can still be referenced after table rename |
149
|
|
|
*/ |
150
|
|
|
$diff = new TableDiff( |
151
|
|
|
'sqlsrv_default_constraints', |
152
|
|
|
[], |
153
|
|
|
[ |
154
|
|
|
'df_integer' => new ColumnDiff( |
155
|
|
|
'df_integer', |
156
|
|
|
new Column('df_integer', Type::getType('integer'), ['default' => 666]), |
157
|
|
|
['default'], |
158
|
|
|
new Column('df_integer', Type::getType('integer'), ['default' => 0]) |
159
|
|
|
), |
160
|
|
|
], |
161
|
|
|
[], |
162
|
|
|
[], |
163
|
|
|
[], |
164
|
|
|
[], |
165
|
|
|
$table |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
$this->schemaManager->alterTable($diff); |
169
|
|
|
$columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints'); |
170
|
|
|
|
171
|
|
|
self::assertEquals(666, $columns['df_integer']->getDefault()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testPkOrdering() : void |
175
|
|
|
{ |
176
|
|
|
// SQL Server stores index column information in a system table with two |
177
|
|
|
// columns that almost always have the same value: index_column_id and key_ordinal. |
178
|
|
|
// The only situation when the two values doesn't match up is when a clustered index |
179
|
|
|
// is declared that references columns in a different order from which they are |
180
|
|
|
// declared in the table. In that case, key_ordinal != index_column_id. |
181
|
|
|
// key_ordinal holds the index ordering. index_column_id is just a unique identifier |
182
|
|
|
// for index columns within the given index. |
183
|
|
|
$table = new Table('sqlsrv_pk_ordering'); |
184
|
|
|
$table->addColumn('colA', 'integer', ['notnull' => true]); |
185
|
|
|
$table->addColumn('colB', 'integer', ['notnull' => true]); |
186
|
|
|
$table->setPrimaryKey(['colB', 'colA']); |
187
|
|
|
$this->schemaManager->createTable($table); |
188
|
|
|
|
189
|
|
|
$indexes = $this->schemaManager->listTableIndexes('sqlsrv_pk_ordering'); |
190
|
|
|
|
191
|
|
|
self::assertCount(1, $indexes); |
192
|
|
|
|
193
|
|
|
$firstIndex = current($indexes); |
194
|
|
|
$columns = $firstIndex->getColumns(); |
195
|
|
|
self::assertCount(2, $columns); |
196
|
|
|
self::assertEquals('colB', $columns[0]); |
197
|
|
|
self::assertEquals('colA', $columns[1]); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|