File: SQLServerSchemaManagerTest.php

package info (click to toggle)
php-doctrine-dbal 4.3.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,092 kB
  • sloc: php: 60,293; xml: 618; makefile: 23
file content (210 lines) | stat: -rw-r--r-- 8,380 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
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Schema;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnEditor;
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Types;

use function array_shift;

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

    public function testColumnCollation(): void
    {
        $table = Table::editor()
            ->setUnquotedName('test_collation')
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('test')
                    ->setTypeName(Types::STRING)
                    ->setLength(32)
                    ->create(),
            )
            ->create();

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

        // SQL Server should report a default collation on the column
        self::assertNotNull($columns['test']->getCollation());

        $table = $table->edit()
            ->modifyColumnByUnquotedName('test', static function (ColumnEditor $editor): void {
                $editor->setCollation('Icelandic_CS_AS');
            })
            ->create();

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

        self::assertEquals('Icelandic_CS_AS', $columns['test']->getCollation());
    }

    public function testDefaultConstraints(): void
    {
        $oldTable = Table::editor()
            ->setUnquotedName('sqlsrv_default_constraints')
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('no_default')
                    ->setTypeName(Types::STRING)
                    ->setLength(32)
                    ->create(),
                Column::editor()
                    ->setUnquotedName('df_integer')
                    ->setTypeName(Types::INTEGER)
                    ->setDefaultValue(666)
                    ->create(),
                Column::editor()
                    ->setUnquotedName('df_string_1')
                    ->setTypeName(Types::STRING)
                    ->setLength(32)
                    ->setDefaultValue('foobar')
                    ->create(),
                Column::editor()
                    ->setUnquotedName('df_string_2')
                    ->setTypeName(Types::STRING)
                    ->setLength(32)
                    ->setDefaultValue('Doctrine rocks!!!')
                    ->create(),
                Column::editor()
                    ->setUnquotedName('df_string_3')
                    ->setTypeName(Types::STRING)
                    ->setLength(32)
                    ->setDefaultValue('another default value')
                    ->create(),
                Column::editor()
                    ->setUnquotedName('df_string_4')
                    ->setTypeName(Types::STRING)
                    ->setLength(32)
                    ->setDefaultValue('column to rename')
                    ->create(),
                Column::editor()
                    ->setUnquotedName('df_boolean')
                    ->setTypeName(Types::BOOLEAN)
                    ->setDefaultValue(true)
                    ->create(),
            )
            ->create();

        $this->schemaManager->createTable($oldTable);
        $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 = $oldTable->edit()
            ->modifyColumnByUnquotedName('df_integer', static function (ColumnEditor $editor): void {
                $editor->setDefaultValue(0);
            })
            ->modifyColumnByUnquotedName('df_string_2', static function (ColumnEditor $editor): void {
                $editor->setDefaultValue(null);
            })
            ->modifyColumnByUnquotedName('df_boolean', static function (ColumnEditor $editor): void {
                $editor->setDefaultValue(false);
            })
            ->dropColumnByUnquotedName('df_string_1')
            ->dropColumnByUnquotedName('df_string_4')
            ->addColumn(
                Column::editor()
                    ->setUnquotedName('df_string_4_renamed')
                    ->setTypeName(Types::STRING)
                    ->setLength(32)
                    ->setDefaultValue('column to rename')
                    ->create(),
            )
            ->create();

        $diff = $this->schemaManager->createComparator()
            ->compareTables(
                $this->schemaManager->introspectTable('sqlsrv_default_constraints'),
                $newTable,
            );

        $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_4_renamed']->getDefault());
    }

    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 = Table::editor()
            ->setUnquotedName('sqlsrv_pk_ordering')
            ->setColumns(
                Column::editor()
                    ->setUnquotedName('colA')
                    ->setTypeName(Types::INTEGER)
                    ->create(),
                Column::editor()
                    ->setUnquotedName('colB')
                    ->setTypeName(Types::INTEGER)
                    ->create(),
            )
            ->setPrimaryKeyConstraint(
                PrimaryKeyConstraint::editor()
                    ->setUnquotedColumnNames('colB', 'colA')
                    ->create(),
            )
            ->create();

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

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

        self::assertCount(1, $indexes);
        $firstIndex = array_shift($indexes);
        self::assertNotNull($firstIndex);

        self::assertSame(['colB', 'colA'], $firstIndex->getColumns());
    }

    public function testNvarcharMaxIsLengthMinus1(): void
    {
        $sql = 'CREATE TABLE test_nvarchar_max (
            col_nvarchar_max NVARCHAR(MAX),
            col_nvarchar NVARCHAR(128)
        )';

        $this->connection->executeStatement($sql);

        $table = $this->schemaManager->introspectTable('test_nvarchar_max');

        self::assertSame(-1, $table->getColumn('col_nvarchar_max')->getLength());
        self::assertSame(128, $table->getColumn('col_nvarchar')->getLength());
    }

    /** @link https://learn.microsoft.com/en-us/sql/relational-databases/security/authentication-access/ownership-and-user-schema-separation?view=sql-server-ver16#the-dbo-schema */
    public function getExpectedDefaultSchemaName(): string
    {
        return 'dbo';
    }
}