File: SQLServerSchemaManagerTest.php

package info (click to toggle)
php-doctrine-dbal 3.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,500 kB
  • sloc: php: 54,704; xml: 485; makefile: 42; sh: 24
file content (255 lines) | stat: -rw-r--r-- 13,330 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php

namespace Doctrine\DBAL\Tests\Functional\Schema;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;

use function current;

class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
{
    protected function supportsPlatform(AbstractPlatform $platform): bool
    {
        return $platform instanceof SQLServerPlatform;
    }

    public function testColumnCollation(): void
    {
        $table  = new Table($tableName = 'test_collation');
        $column = $table->addColumn($columnName = 'test', 'string');

        $this->dropAndCreateTable($table);
        $columns = $this->schemaManager->listTableColumns($tableName);

        // SQL Server should report a default collation on the column
        self::assertTrue($columns[$columnName]->hasPlatformOption('collation'));

        $column->setPlatformOption('collation', $collation = 'Icelandic_CS_AS');

        $this->dropAndCreateTable($table);
        $columns = $this->schemaManager->listTableColumns($tableName);

        self::assertEquals($collation, $columns[$columnName]->getPlatformOption('collation'));
    }

    public function testDefaultConstraints(): void
    {
        $table = new Table('sqlsrv_default_constraints');
        $table->addColumn('no_default', 'string');
        $table->addColumn('df_integer', 'integer', ['default' => 666]);
        $table->addColumn('df_string_1', 'string', ['default' => 'foobar']);
        $table->addColumn('df_string_2', 'string', ['default' => 'Doctrine rocks!!!']);
        $table->addColumn('df_string_3', 'string', ['default' => 'another default value']);
        $table->addColumn('df_string_4', 'string', ['default' => 'column to rename']);
        $table->addColumn('df_boolean', 'boolean', ['default' => true]);

        $this->schemaManager->createTable($table);
        $columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');

        self::assertNull($columns['no_default']->getDefault());
        self::assertEquals(666, $columns['df_integer']->getDefault());
        self::assertEquals('foobar', $columns['df_string_1']->getDefault());
        self::assertEquals('Doctrine rocks!!!', $columns['df_string_2']->getDefault());
        self::assertEquals('another default value', $columns['df_string_3']->getDefault());
        self::assertEquals(1, $columns['df_boolean']->getDefault());

        $newTable = clone $table;
        $newTable->changeColumn('df_integer', ['default' => 0]);
        $newTable->changeColumn('df_string_2', ['default' => null]);
        $newTable->changeColumn('df_string_3', ['length' => 50]);
        $newTable->changeColumn('df_boolean', ['default' => false]);
        $newTable->dropColumn('df_string_1');
        $newTable->dropColumn('df_string_4');
        $newTable->addColumn('df_string_renamed', 'string', ['default' => 'column to rename']);

        $comparator = $this->schemaManager->createComparator();

        $diff = $comparator->diffTable($table, $newTable);
        self::assertNotFalse($diff);

        $this->schemaManager->alterTable($diff);
        $columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');

        self::assertNull($columns['no_default']->getDefault());
        self::assertEquals(0, $columns['df_integer']->getDefault());
        self::assertNull($columns['df_string_2']->getDefault());
        self::assertEquals('another default value', $columns['df_string_3']->getDefault());
        self::assertEquals(0, $columns['df_boolean']->getDefault());
        self::assertEquals('column to rename', $columns['df_string_renamed']->getDefault());

        /**
         * Test that column default constraints can still be referenced after table rename
         */
        $table    = $newTable;
        $newTable = clone $table;
        $newTable->changeColumn('df_integer', ['default' => 666]);

        $diff = $comparator->diffTable($table, $newTable);
        self::assertNotFalse($diff);

        $this->schemaManager->alterTable($diff);
        $columns = $this->schemaManager->listTableColumns('sqlsrv_default_constraints');

        self::assertEquals(666, $columns['df_integer']->getDefault());
    }

    public function testColumnComments(): void
    {
        $table = new Table('sqlsrv_column_comment');
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
        $table->addColumn('comment_null', 'integer', ['comment' => null]);
        $table->addColumn('comment_false', 'integer', ['comment' => false]);
        $table->addColumn('comment_empty_string', 'integer', ['comment' => '']);
        $table->addColumn('comment_integer_0', 'integer', ['comment' => 0]);
        $table->addColumn('comment_float_0', 'integer', ['comment' => 0.0]);
        $table->addColumn('comment_string_0', 'integer', ['comment' => '0']);
        $table->addColumn('comment', 'integer', ['comment' => 'Doctrine 0wnz you!']);
        $table->addColumn(
            '`comment_quoted`',
            'integer',
            ['comment' => 'Doctrine 0wnz comments for explicitly quoted columns!'],
        );
        $table->addColumn('create', 'integer', ['comment' => 'Doctrine 0wnz comments for reserved keyword columns!']);
        $table->addColumn('commented_type', 'object');
        $table->addColumn('commented_type_with_comment', 'array', ['comment' => 'Doctrine array type.']);
        $table->addColumn('commented_req_change_column', 'integer', ['comment' => 'Some comment', 'notnull' => true]);
        $table->setPrimaryKey(['id']);

        $this->schemaManager->createTable($table);

        $columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment');
        self::assertCount(13, $columns);
        self::assertNull($columns['id']->getComment());
        self::assertNull($columns['comment_null']->getComment());
        self::assertNull($columns['comment_false']->getComment());
        self::assertNull($columns['comment_empty_string']->getComment());
        self::assertEquals('0', $columns['comment_integer_0']->getComment());
        self::assertEquals('0', $columns['comment_float_0']->getComment());
        self::assertEquals('0', $columns['comment_string_0']->getComment());
        self::assertEquals('Doctrine 0wnz you!', $columns['comment']->getComment());
        self::assertEquals(
            'Doctrine 0wnz comments for explicitly quoted columns!',
            $columns['comment_quoted']->getComment(),
        );
        self::assertEquals('Doctrine 0wnz comments for reserved keyword columns!', $columns['[create]']->getComment());
        self::assertNull($columns['commented_type']->getComment());
        self::assertEquals('Doctrine array type.', $columns['commented_type_with_comment']->getComment());
        self::assertEquals('Some comment', $columns['commented_req_change_column']->getComment());

        $newTable = clone $table;
        $newTable->addColumn('added_comment_none', 'integer');
        $newTable->addColumn('added_comment_null', 'integer', ['comment' => null]);
        $newTable->addColumn('added_comment_false', 'integer', ['comment' => false]);
        $newTable->addColumn('added_comment_empty_string', 'integer', ['comment' => '']);
        $newTable->addColumn('added_comment_integer_0', 'integer', ['comment' => 0]);
        $newTable->addColumn('added_comment_float_0', 'integer', ['comment' => 0.0]);
        $newTable->addColumn('added_comment_string_0', 'integer', ['comment' => '0']);
        $newTable->addColumn('added_comment', 'integer', ['comment' => 'Doctrine']);
        $newTable->addColumn('`added_comment_quoted`', 'integer', ['comment' => 'rulez']);
        $newTable->addColumn('`select`', 'integer', ['comment' => '666']);
        $newTable->addColumn('added_commented_type', 'object');
        $newTable->addColumn('added_commented_type_with_comment', 'array', ['comment' => '666']);
        $newTable->dropColumn('comment_float_0');
        $newTable->addColumn('comment_double_0', 'decimal', ['comment' => '0']);

        // Add comment to non-commented column.
        $newTable->changeColumn('id', ['comment' => 'primary']);

        // Remove comment from null-commented column.
        $newTable->changeColumn('comment_null', ['comment' => null]);

        // Add comment to false-commented column.
        $newTable->changeColumn('comment_false', ['comment' => 'false']);

        // Change type to custom type from empty string commented column.
        $newTable->changeColumn('comment_empty_string', ['type' => Type::getType('object')]);

        // Change comment to false-comment from zero-string commented column.
        $newTable->changeColumn('comment_string_0', ['comment' => false]);

        // Remove comment from regular commented column.
        $newTable->changeColumn('comment', ['comment' => null]);

        // Change comment and change type to custom type from regular commented column.
        $newTable->changeColumn('`comment_quoted', [
            'type' => Type::getType('array'),
            'comment' => 'Doctrine array.',
        ]);

        // Remove comment and change type to custom type from regular commented column.
        $newTable->changeColumn('`create', ['type' => Type::getType('object'), 'comment' => null]);

        // Add comment and change custom type to regular type from non-commented column.
        $newTable->changeColumn('commented_type', ['type' => Type::getType('integer'), 'comment' => 'foo']);

        // Remove comment from commented custom type column.
        $newTable->changeColumn('commented_type_with_comment', ['comment' => null]);

        // Change column requirements without changing comment.
        $newTable->changeColumn('commented_req_change_column', ['notnull' => true]);

        $newTable->dropColumn('comment_integer_0');

        $diff = $this->schemaManager->createComparator()
            ->diffTable($table, $newTable);
        self::assertNotFalse($diff);

        $this->schemaManager->alterTable($diff);

        $columns = $this->schemaManager->listTableColumns('sqlsrv_column_comment');
        self::assertCount(24, $columns);
        self::assertEquals('primary', $columns['id']->getComment());
        self::assertNull($columns['comment_null']->getComment());
        self::assertEquals('false', $columns['comment_false']->getComment());
        self::assertNull($columns['comment_empty_string']->getComment());
        self::assertEquals('0', $columns['comment_double_0']->getComment());
        self::assertNull($columns['comment_string_0']->getComment());
        self::assertNull($columns['comment']->getComment());
        self::assertEquals('Doctrine array.', $columns['comment_quoted']->getComment());
        self::assertNull($columns['[create]']->getComment());
        self::assertEquals('foo', $columns['commented_type']->getComment());
        self::assertNull($columns['commented_type_with_comment']->getComment());
        self::assertNull($columns['added_comment_none']->getComment());
        self::assertNull($columns['added_comment_null']->getComment());
        self::assertNull($columns['added_comment_false']->getComment());
        self::assertNull($columns['added_comment_empty_string']->getComment());
        self::assertEquals('0', $columns['added_comment_integer_0']->getComment());
        self::assertEquals('0', $columns['added_comment_float_0']->getComment());
        self::assertEquals('0', $columns['added_comment_string_0']->getComment());
        self::assertEquals('Doctrine', $columns['added_comment']->getComment());
        self::assertEquals('rulez', $columns['added_comment_quoted']->getComment());
        self::assertEquals('666', $columns['[select]']->getComment());
        self::assertNull($columns['added_commented_type']->getComment());
        self::assertEquals('666', $columns['added_commented_type_with_comment']->getComment());
        self::assertEquals('Some comment', $columns['commented_req_change_column']->getComment());
    }

    public function testPkOrdering(): void
    {
        // SQL Server stores index column information in a system table with two
        // columns that almost always have the same value: index_column_id and key_ordinal.
        // The only situation when the two values doesn't match up is when a clustered index
        // is declared that references columns in a different order from which they are
        // declared in the table. In that case, key_ordinal != index_column_id.
        // key_ordinal holds the index ordering. index_column_id is just a unique identifier
        // for index columns within the given index.
        $table = new Table('sqlsrv_pk_ordering');
        $table->addColumn('colA', 'integer', ['notnull' => true]);
        $table->addColumn('colB', 'integer', ['notnull' => true]);
        $table->setPrimaryKey(['colB', 'colA']);
        $this->schemaManager->createTable($table);

        $indexes = $this->schemaManager->listTableIndexes('sqlsrv_pk_ordering');

        self::assertCount(1, $indexes);

        $firstIndex = current($indexes);
        $columns    = $firstIndex->getColumns();
        self::assertCount(2, $columns);
        self::assertEquals('colB', $columns[0]);
        self::assertEquals('colA', $columns[1]);
    }
}