Completed
Pull Request — master (#3499)
by David
18:49
created

SQLServerSchemaManagerTest::testCommentInTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Functional\Schema;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\ColumnDiff;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Schema\TableDiff;
9
use Doctrine\DBAL\Types\Type;
10
use function current;
11
12
class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
13
{
14
    protected function getPlatformName()
15
    {
16
        return 'mssql';
17
    }
18
19
    /**
20
     * @group DBAL-255
21
     */
22
    public function testDropColumnConstraints()
23
    {
24
        $table = new Table('sqlsrv_drop_column');
25
        $table->addColumn('id', 'integer');
26
        $table->addColumn('todrop', 'decimal', ['default' => 10.2]);
27
28
        $this->schemaManager->createTable($table);
29
30
        $diff = new TableDiff('sqlsrv_drop_column', [], [], [new Column('todrop', Type::getType('decimal'))]);
31
        $this->schemaManager->alterTable($diff);
32
33
        $columns = $this->schemaManager->listTableColumns('sqlsrv_drop_column');
34
        self::assertCount(1, $columns);
35
    }
36
37
    public function testColumnCollation()
38
    {
39
        $table  = new Table($tableName = 'test_collation');
40
        $column = $table->addColumn($columnName = 'test', 'string');
41
42
        $this->schemaManager->dropAndCreateTable($table);
43
        $columns = $this->schemaManager->listTableColumns($tableName);
44
45
        self::assertTrue($columns[$columnName]->hasPlatformOption('collation')); // SQL Server should report a default collation on the column
46
47
        $column->setPlatformOption('collation', $collation = 'Icelandic_CS_AS');
48
49
        $this->schemaManager->dropAndCreateTable($table);
50
        $columns = $this->schemaManager->listTableColumns($tableName);
51
52
        self::assertEquals($collation, $columns[$columnName]->getPlatformOption('collation'));
53
    }
54
55
    public function testDefaultConstraints()
56
    {
57
        $platform = $this->schemaManager->getDatabasePlatform();
58
        $table    = new Table('sqlsrv_default_constraints');
59
        $table->addColumn('no_default', 'string');
60
        $table->addColumn('df_integer', 'integer', ['default' => 666]);
61
        $table->addColumn('df_string_1', 'string', ['default' => 'foobar']);
62
        $table->addColumn('df_string_2', 'string', ['default' => 'Doctrine rocks!!!']);
63
        $table->addColumn('df_string_3', 'string', ['default' => 'another default value']);
64
        $table->addColumn('df_string_4', 'string', ['default' => 'column to rename']);
65
        $table->addColumn('df_boolean', 'boolean', ['default' => true]);
66
        $table->addColumn('df_current_date', 'date', ['default' => $platform->getCurrentDateSQL()]);
67
        $table->addColumn('df_current_time', 'time', ['default' => $platform->getCurrentTimeSQL()]);
68
69
        $this->schemaManager->createTable($table);
70
        $columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');
71
72
        self::assertNull($columns['no_default']->getDefault());
73
        self::assertEquals(666, $columns['df_integer']->getDefault());
74
        self::assertEquals('foobar', $columns['df_string_1']->getDefault());
75
        self::assertEquals('Doctrine rocks!!!', $columns['df_string_2']->getDefault());
76
        self::assertEquals('another default value', $columns['df_string_3']->getDefault());
77
        self::assertEquals(1, $columns['df_boolean']->getDefault());
78
        self::assertSame($platform->getCurrentDateSQL(), $columns['df_current_date']->getDefault());
79
        self::assertSame($platform->getCurrentTimeSQL(), $columns['df_current_time']->getDefault());
80
81
        $diff                                = new TableDiff(
82
            'sqlsrv_default_constraints',
83
            [new Column('df_current_timestamp', Type::getType('datetime'), ['default' => 'CURRENT_TIMESTAMP'])],
84
            [
85
                'df_integer' => new ColumnDiff(
86
                    'df_integer',
87
                    new Column('df_integer', Type::getType('integer'), ['default' => 0]),
88
                    ['default'],
89
                    new Column('df_integer', Type::getType('integer'), ['default' => 666])
90
                ),
91
                'df_string_2' => new ColumnDiff(
92
                    'df_string_2',
93
                    new Column('df_string_2', Type::getType('string')),
94
                    ['default'],
95
                    new Column('df_string_2', Type::getType('string'), ['default' => 'Doctrine rocks!!!'])
96
                ),
97
                'df_string_3' => new ColumnDiff(
98
                    'df_string_3',
99
                    new Column('df_string_3', Type::getType('string'), ['length' => 50, 'default' => 'another default value']),
100
                    ['length'],
101
                    new Column('df_string_3', Type::getType('string'), ['length' => 50, 'default' => 'another default value'])
102
                ),
103
                'df_boolean' => new ColumnDiff(
104
                    'df_boolean',
105
                    new Column('df_boolean', Type::getType('boolean'), ['default' => false]),
106
                    ['default'],
107
                    new Column('df_boolean', Type::getType('boolean'), ['default' => true])
108
                ),
109
            ],
110
            [
111
                'df_string_1' => new Column('df_string_1', Type::getType('string')),
112
            ],
113
            [],
114
            [],
115
            [],
116
            $table
117
        );
118
        $diff->newName                       = 'sqlsrv_default_constraints';
119
        $diff->renamedColumns['df_string_4'] = new Column(
120
            'df_string_renamed',
121
            Type::getType('string'),
122
            ['default' => 'column to rename']
123
        );
124
125
        $this->schemaManager->alterTable($diff);
126
        $columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');
127
128
        self::assertNull($columns['no_default']->getDefault());
129
        self::assertEquals('CURRENT_TIMESTAMP', $columns['df_current_timestamp']->getDefault());
130
        self::assertEquals(0, $columns['df_integer']->getDefault());
131
        self::assertNull($columns['df_string_2']->getDefault());
132
        self::assertEquals('another default value', $columns['df_string_3']->getDefault());
133
        self::assertEquals(0, $columns['df_boolean']->getDefault());
134
        self::assertEquals('column to rename', $columns['df_string_renamed']->getDefault());
135
136
        /**
137
         * Test that column default constraints can still be referenced after table rename
138
         */
139
        $diff = new TableDiff(
140
            'sqlsrv_default_constraints',
141
            [],
142
            [
143
                'df_current_timestamp' => new ColumnDiff(
144
                    'df_current_timestamp',
145
                    new Column('df_current_timestamp', Type::getType('datetime')),
146
                    ['default'],
147
                    new Column('df_current_timestamp', Type::getType('datetime'), ['default' => 'CURRENT_TIMESTAMP'])
148
                ),
149
                'df_integer' => new ColumnDiff(
150
                    'df_integer',
151
                    new Column('df_integer', Type::getType('integer'), ['default' => 666]),
152
                    ['default'],
153
                    new Column('df_integer', Type::getType('integer'), ['default' => 0])
154
                ),
155
            ],
156
            [],
157
            [],
158
            [],
159
            [],
160
            $table
161
        );
162
163
        $this->schemaManager->alterTable($diff);
164
        $columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');
165
166
        self::assertNull($columns['df_current_timestamp']->getDefault());
167
        self::assertEquals(666, $columns['df_integer']->getDefault());
168
    }
169
170
    /**
171
     * @group DBAL-543
172
     */
173
    public function testColumnComments()
174
    {
175
        $table = new Table('sqlsrv_column_comment');
176
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
177
        $table->addColumn('comment_null', 'integer', ['comment' => null]);
178
        $table->addColumn('comment_false', 'integer', ['comment' => false]);
179
        $table->addColumn('comment_empty_string', 'integer', ['comment' => '']);
180
        $table->addColumn('comment_integer_0', 'integer', ['comment' => 0]);
181
        $table->addColumn('comment_float_0', 'integer', ['comment' => 0.0]);
182
        $table->addColumn('comment_string_0', 'integer', ['comment' => '0']);
183
        $table->addColumn('comment', 'integer', ['comment' => 'Doctrine 0wnz you!']);
184
        $table->addColumn('`comment_quoted`', 'integer', ['comment' => 'Doctrine 0wnz comments for explicitly quoted columns!']);
185
        $table->addColumn('create', 'integer', ['comment' => 'Doctrine 0wnz comments for reserved keyword columns!']);
186
        $table->addColumn('commented_type', 'object');
187
        $table->addColumn('commented_type_with_comment', 'array', ['comment' => 'Doctrine array type.']);
188
        $table->setPrimaryKey(['id']);
189
190
        $this->schemaManager->createTable($table);
191
192
        $columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment');
193
        self::assertCount(12, $columns);
194
        self::assertNull($columns['id']->getComment());
195
        self::assertNull($columns['comment_null']->getComment());
196
        self::assertNull($columns['comment_false']->getComment());
197
        self::assertNull($columns['comment_empty_string']->getComment());
198
        self::assertEquals('0', $columns['comment_integer_0']->getComment());
199
        self::assertEquals('0', $columns['comment_float_0']->getComment());
200
        self::assertEquals('0', $columns['comment_string_0']->getComment());
201
        self::assertEquals('Doctrine 0wnz you!', $columns['comment']->getComment());
202
        self::assertEquals('Doctrine 0wnz comments for explicitly quoted columns!', $columns['comment_quoted']->getComment());
203
        self::assertEquals('Doctrine 0wnz comments for reserved keyword columns!', $columns['[create]']->getComment());
204
        self::assertNull($columns['commented_type']->getComment());
205
        self::assertEquals('Doctrine array type.', $columns['commented_type_with_comment']->getComment());
206
207
        $tableDiff                                                    = new TableDiff('sqlsrv_column_comment');
208
        $tableDiff->fromTable                                         = $table;
209
        $tableDiff->addedColumns['added_comment_none']                = new Column('added_comment_none', Type::getType('integer'));
210
        $tableDiff->addedColumns['added_comment_null']                = new Column('added_comment_null', Type::getType('integer'), ['comment' => null]);
211
        $tableDiff->addedColumns['added_comment_false']               = new Column('added_comment_false', Type::getType('integer'), ['comment' => false]);
212
        $tableDiff->addedColumns['added_comment_empty_string']        = new Column('added_comment_empty_string', Type::getType('integer'), ['comment' => '']);
213
        $tableDiff->addedColumns['added_comment_integer_0']           = new Column('added_comment_integer_0', Type::getType('integer'), ['comment' => 0]);
214
        $tableDiff->addedColumns['added_comment_float_0']             = new Column('added_comment_float_0', Type::getType('integer'), ['comment' => 0.0]);
215
        $tableDiff->addedColumns['added_comment_string_0']            = new Column('added_comment_string_0', Type::getType('integer'), ['comment' => '0']);
216
        $tableDiff->addedColumns['added_comment']                     = new Column('added_comment', Type::getType('integer'), ['comment' => 'Doctrine']);
217
        $tableDiff->addedColumns['`added_comment_quoted`']            = new Column('`added_comment_quoted`', Type::getType('integer'), ['comment' => 'rulez']);
218
        $tableDiff->addedColumns['select']                            = new Column('select', Type::getType('integer'), ['comment' => '666']);
219
        $tableDiff->addedColumns['added_commented_type']              = new Column('added_commented_type', Type::getType('object'));
220
        $tableDiff->addedColumns['added_commented_type_with_comment'] = new Column('added_commented_type_with_comment', Type::getType('array'), ['comment' => '666']);
221
222
        $tableDiff->renamedColumns['comment_float_0'] = new Column('comment_double_0', Type::getType('decimal'), ['comment' => 'Double for real!']);
223
224
        // Add comment to non-commented column.
225
        $tableDiff->changedColumns['id'] = new ColumnDiff(
226
            'id',
227
            new Column('id', Type::getType('integer'), ['autoincrement' => true, 'comment' => 'primary']),
228
            ['comment'],
229
            new Column('id', Type::getType('integer'), ['autoincrement' => true])
230
        );
231
232
        // Remove comment from null-commented column.
233
        $tableDiff->changedColumns['comment_null'] = new ColumnDiff(
234
            'comment_null',
235
            new Column('comment_null', Type::getType('string')),
236
            ['type'],
237
            new Column('comment_null', Type::getType('integer'), ['comment' => null])
238
        );
239
240
        // Add comment to false-commented column.
241
        $tableDiff->changedColumns['comment_false'] = new ColumnDiff(
242
            'comment_false',
243
            new Column('comment_false', Type::getType('integer'), ['comment' => 'false']),
244
            ['comment'],
245
            new Column('comment_false', Type::getType('integer'), ['comment' => false])
246
        );
247
248
        // Change type to custom type from empty string commented column.
249
        $tableDiff->changedColumns['comment_empty_string'] = new ColumnDiff(
250
            'comment_empty_string',
251
            new Column('comment_empty_string', Type::getType('object')),
252
            ['type'],
253
            new Column('comment_empty_string', Type::getType('integer'), ['comment' => ''])
254
        );
255
256
        // Change comment to false-comment from zero-string commented column.
257
        $tableDiff->changedColumns['comment_string_0'] = new ColumnDiff(
258
            'comment_string_0',
259
            new Column('comment_string_0', Type::getType('integer'), ['comment' => false]),
260
            ['comment'],
261
            new Column('comment_string_0', Type::getType('integer'), ['comment' => '0'])
262
        );
263
264
        // Remove comment from regular commented column.
265
        $tableDiff->changedColumns['comment'] = new ColumnDiff(
266
            'comment',
267
            new Column('comment', Type::getType('integer')),
268
            ['comment'],
269
            new Column('comment', Type::getType('integer'), ['comment' => 'Doctrine 0wnz you!'])
270
        );
271
272
        // Change comment and change type to custom type from regular commented column.
273
        $tableDiff->changedColumns['`comment_quoted`'] = new ColumnDiff(
274
            '`comment_quoted`',
275
            new Column('`comment_quoted`', Type::getType('array'), ['comment' => 'Doctrine array.']),
276
            ['comment', 'type'],
277
            new Column('`comment_quoted`', Type::getType('integer'), ['comment' => 'Doctrine 0wnz you!'])
278
        );
279
280
        // Remove comment and change type to custom type from regular commented column.
281
        $tableDiff->changedColumns['create'] = new ColumnDiff(
282
            'create',
283
            new Column('create', Type::getType('object')),
284
            ['comment', 'type'],
285
            new Column('create', Type::getType('integer'), ['comment' => 'Doctrine 0wnz comments for reserved keyword columns!'])
286
        );
287
288
        // Add comment and change custom type to regular type from non-commented column.
289
        $tableDiff->changedColumns['commented_type'] = new ColumnDiff(
290
            'commented_type',
291
            new Column('commented_type', Type::getType('integer'), ['comment' => 'foo']),
292
            ['comment', 'type'],
293
            new Column('commented_type', Type::getType('object'))
294
        );
295
296
        // Remove comment from commented custom type column.
297
        $tableDiff->changedColumns['commented_type_with_comment'] = new ColumnDiff(
298
            'commented_type_with_comment',
299
            new Column('commented_type_with_comment', Type::getType('array')),
300
            ['comment'],
301
            new Column('commented_type_with_comment', Type::getType('array'), ['comment' => 'Doctrine array type.'])
302
        );
303
304
        $tableDiff->removedColumns['comment_integer_0'] = new Column('comment_integer_0', Type::getType('integer'), ['comment' => 0]);
305
306
        $this->schemaManager->alterTable($tableDiff);
307
308
        $columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment');
309
        self::assertCount(23, $columns);
310
        self::assertEquals('primary', $columns['id']->getComment());
311
        self::assertNull($columns['comment_null']->getComment());
312
        self::assertEquals('false', $columns['comment_false']->getComment());
313
        self::assertNull($columns['comment_empty_string']->getComment());
314
        self::assertEquals('0', $columns['comment_double_0']->getComment());
315
        self::assertNull($columns['comment_string_0']->getComment());
316
        self::assertNull($columns['comment']->getComment());
317
        self::assertEquals('Doctrine array.', $columns['comment_quoted']->getComment());
318
        self::assertNull($columns['[create]']->getComment());
319
        self::assertEquals('foo', $columns['commented_type']->getComment());
320
        self::assertNull($columns['commented_type_with_comment']->getComment());
321
        self::assertNull($columns['added_comment_none']->getComment());
322
        self::assertNull($columns['added_comment_null']->getComment());
323
        self::assertNull($columns['added_comment_false']->getComment());
324
        self::assertNull($columns['added_comment_empty_string']->getComment());
325
        self::assertEquals('0', $columns['added_comment_integer_0']->getComment());
326
        self::assertEquals('0', $columns['added_comment_float_0']->getComment());
327
        self::assertEquals('0', $columns['added_comment_string_0']->getComment());
328
        self::assertEquals('Doctrine', $columns['added_comment']->getComment());
329
        self::assertEquals('rulez', $columns['added_comment_quoted']->getComment());
330
        self::assertEquals('666', $columns['[select]']->getComment());
331
        self::assertNull($columns['added_commented_type']->getComment());
332
        self::assertEquals('666', $columns['added_commented_type_with_comment']->getComment());
333
    }
334
335
    public function testPkOrdering()
336
    {
337
        // SQL Server stores index column information in a system table with two
338
        // columns that almost always have the same value: index_column_id and key_ordinal.
339
        // The only situation when the two values doesn't match up is when a clustered index
340
        // is declared that references columns in a different order from which they are
341
        // declared in the table. In that case, key_ordinal != index_column_id.
342
        // key_ordinal holds the index ordering. index_column_id is just a unique identifier
343
        // for index columns within the given index.
344
        $table = new Table('sqlsrv_pk_ordering');
345
        $table->addColumn('colA', 'integer', ['notnull' => true]);
346
        $table->addColumn('colB', 'integer', ['notnull' => true]);
347
        $table->setPrimaryKey(['colB', 'colA']);
348
        $this->schemaManager->createTable($table);
349
350
        $indexes = $this->schemaManager->listTableIndexes('sqlsrv_pk_ordering');
351
352
        self::assertCount(1, $indexes);
353
354
        $firstIndex = current($indexes);
355
        $columns    = $firstIndex->getColumns();
356
        self::assertCount(2, $columns);
357
        self::assertEquals('colB', $columns[0]);
358
        self::assertEquals('colA', $columns[1]);
359
    }
360
361
    public function testCommentInTable() : void
362
    {
363
        self::markTestSkipped('Table level comments are not supported on SQLServer');
364
    }
365
}
366