Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ public function getColumns(string $tableName): array
$column->setPrecision((int)$columnInfo['precision']);
}

$columns[$columnInfo['name']] = $column;
$columns[] = $column;
}

return $columns;
Expand Down Expand Up @@ -626,10 +626,10 @@ protected function getChangeDefault(string $tableName, Column $newColumn): Alter
*/
protected function getChangeColumnInstructions(string $tableName, string $columnName, Column $newColumn): AlterInstructions
{
$columns = $this->getColumns($tableName);
$column = $this->getColumn($tableName, $columnName);
$changeDefault =
$newColumn->getDefault() !== $columns[$columnName]->getDefault() ||
$newColumn->getType() !== $columns[$columnName]->getType();
$newColumn->getDefault() !== $column?->getDefault() ||
$newColumn->getType() !== $column?->getType();

$instructions = new AlterInstructions();

Expand Down Expand Up @@ -1382,4 +1382,25 @@ public function getDecoratedConnection(): Connection

return $this->decoratedConnection = $this->buildConnection(SqlServerDriver::class, $options);
}

/**
* Gets a table column
*
* @param string $tableName
* @param string $columnName
* @return \Phinx\Db\Table\Column|null
*/
private function getColumn(string $tableName, string $columnName): ?Column
{
$columns = $this->getColumns($tableName);

$filteredColumns = array_filter(
$columns,
static function ($column) use ($columnName) {
return $column->getName() === $columnName;
},
);

return array_pop($filteredColumns);
}
}
81 changes: 50 additions & 31 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public function testAddColumnWithDefaultNull()
}
}

public function testAddColumnWithNotNullableNoDefault()
public function testAddColumnWithNotNullableNoDefault(): void
{
$table = new Table('table1', [], $this->adapter);
$table
Expand All @@ -488,10 +488,12 @@ public function testAddColumnWithNotNullableNoDefault()

$columns = $this->adapter->getColumns('table1');
$this->assertCount(2, $columns);
$this->assertArrayHasKey('id', $columns);
$this->assertArrayHasKey('col', $columns);
$this->assertFalse($columns['col']->isNull());
$this->assertNull($columns['col']->getDefault());
$firstColumn = $columns[0];
$this->assertSame('id', $firstColumn->getName());
$secondColumn = $columns[1];
$this->assertSame('col', $secondColumn->getName());
$this->assertFalse($secondColumn->isNull());
$this->assertNull($secondColumn->getDefault());
}

public function testAddColumnWithDefaultBool()
Expand Down Expand Up @@ -522,15 +524,15 @@ public function testAddColumnWithLiteralTypeAndDefault()
->addColumn('checked', Literal::from('bit'), ['default' => 0])
->save();

$column = $this->adapter->getColumns('table1')['checked'];
$column = $this->adapter->getColumns('table1')[1];

$this->assertSame('checked', $column->getName());
$this->assertSame('boolean', $column->getType());
$this->assertSame(0, $column->getDefault());
$this->assertTrue($column->getNull());
}

public function testAddColumnWithCustomType()
public function testAddColumnWithCustomType(): void
{
$this->adapter->setDataDomain([
'custom' => [
Expand All @@ -549,18 +551,17 @@ public function testAddColumnWithCustomType()
$this->assertTrue($this->adapter->hasTable('table1'));

$columns = $this->adapter->getColumns('table1');
$this->assertArrayHasKey('custom', $columns);
$this->assertArrayHasKey('custom_ext', $columns);
$this->assertCount(3, $columns);

$column = $this->adapter->getColumns('table1')['custom'];
$this->assertSame('custom', $column->getName());
$this->assertSame('geometry', (string)$column->getType());
$this->assertTrue($column->getNull());
$customColumn = $columns[1];
$this->assertSame('custom', $customColumn->getName());
$this->assertSame('geometry', (string)$customColumn->getType());
$this->assertTrue($customColumn->getNull());

$column = $this->adapter->getColumns('table1')['custom_ext'];
$this->assertSame('custom_ext', $column->getName());
$this->assertSame('geometry', (string)$column->getType());
$this->assertFalse($column->getNull());
$customExtColumn = $columns[2];
$this->assertSame('custom_ext', $customExtColumn->getName());
$this->assertSame('geometry', (string)$customExtColumn->getType());
$this->assertFalse($customExtColumn->getNull());
}

public function testRenameColumn()
Expand Down Expand Up @@ -632,15 +633,15 @@ public function testChangeColumnNameAndNull()
}
}

public function testChangeColumnDefaults()
public function testChangeColumnDefaults(): void
{
$table = new Table('t', [], $this->adapter);
$table->addColumn('column1', 'string', ['default' => 'test'])
->save();
$this->assertTrue($this->adapter->hasColumn('t', 'column1'));

$columns = $this->adapter->getColumns('t');
$this->assertSame('test', $columns['column1']->getDefault());
$this->assertSame('test', $columns[1]->getDefault());

$newColumn1 = new Column();
$newColumn1
Expand All @@ -650,10 +651,10 @@ public function testChangeColumnDefaults()
$this->assertTrue($this->adapter->hasColumn('t', 'column1'));

$columns = $this->adapter->getColumns('t');
$this->assertSame('another test', $columns['column1']->getDefault());
$this->assertSame('another test', $columns[1]->getDefault());
}

public function testChangeColumnDefaultToNull()
public function testChangeColumnDefaultToNull(): void
{
$table = new Table('t', [], $this->adapter);
$table->addColumn('column1', 'string', ['null' => true, 'default' => 'test'])
Expand All @@ -664,10 +665,10 @@ public function testChangeColumnDefaultToNull()
->setDefault(null);
$table->changeColumn('column1', $newColumn1)->save();
$columns = $this->adapter->getColumns('t');
$this->assertNull($columns['column1']->getDefault());
$this->assertNull($columns[1]->getDefault());
}

public function testChangeColumnDefaultToZero()
public function testChangeColumnDefaultToZero(): void
{
$table = new Table('t', [], $this->adapter);
$table->addColumn('column1', 'integer')
Expand All @@ -678,7 +679,7 @@ public function testChangeColumnDefaultToZero()
->setDefault(0);
$table->changeColumn('column1', $newColumn1)->save();
$columns = $this->adapter->getColumns('t');
$this->assertSame(0, $columns['column1']->getDefault());
$this->assertSame(0, $columns[1]->getDefault());
}

public function testDropColumn()
Expand All @@ -691,7 +692,7 @@ public function testDropColumn()
$this->assertFalse($this->adapter->hasColumn('t', 'column1'));
}

public function columnsProvider()
public static function columnsProvider(): array
{
return [
['column1', 'string', ['null' => true, 'default' => null]],
Expand All @@ -718,7 +719,7 @@ public function columnsProvider()
/**
* @dataProvider columnsProvider
*/
public function testGetColumns($colName, $type, $options)
public function testGetColumns($colName, $type, $options): void
{
$table = new Table('t', [], $this->adapter);
$table
Expand All @@ -727,19 +728,23 @@ public function testGetColumns($colName, $type, $options)

$columns = $this->adapter->getColumns('t');
$this->assertCount(2, $columns);
$this->assertEquals($colName, $columns[$colName]->getName());
$this->assertEquals($type, $columns[$colName]->getType());

$specificColumn = $this->getColumn('t', $colName);
$this->assertNotNull($specificColumn);

$this->assertEquals($colName, $specificColumn->getName());
$this->assertEquals($type, $specificColumn->getType());

if (isset($options['limit'])) {
$this->assertEquals($options['limit'], $columns[$colName]->getLimit());
$this->assertEquals($options['limit'], $specificColumn->getLimit());
}

if (isset($options['precision'])) {
$this->assertEquals($options['precision'], $columns[$colName]->getPrecision());
$this->assertEquals($options['precision'], $specificColumn->getPrecision());
}

if (isset($options['scale'])) {
$this->assertEquals($options['scale'], $columns[$colName]->getScale());
$this->assertEquals($options['scale'], $specificColumn->getScale());
}
}

Expand Down Expand Up @@ -1663,4 +1668,18 @@ public function testInvalidPdoAttribute($attribute)
$this->expectExceptionMessage('Invalid PDO attribute: ' . $attribute . ' (\PDO::' . strtoupper($attribute) . ')');
$adapter->connect();
}

private function getColumn(string $tableName, string $columnName): ?Column
{
$columns = $this->adapter->getColumns($tableName);

$filteredColumns = array_filter(
$columns,
static function ($column) use ($columnName) {
return $column->getName() === $columnName;
},
);

return array_pop($filteredColumns);
}
}
Loading