Skip to content

Commit cb284ba

Browse files
committed
TER-58 Add/drop column
1 parent 2f276b1 commit cb284ba

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/Table/Teradata/TeradataTableQueryBuilder.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,27 @@ public function getTruncateTableCommand(string $schemaName, string $tableName):
6060
);
6161
}
6262

63+
public function getAddColumnCommand(string $schemaName, string $tableName, TeradataColumn $columnDefinition): string
64+
{
65+
return sprintf(
66+
'ALTER TABLE %s.%s ADD %s %s',
67+
TeradataQuote::quoteSingleIdentifier($schemaName),
68+
TeradataQuote::quoteSingleIdentifier($tableName),
69+
TeradataQuote::quoteSingleIdentifier($columnDefinition->getColumnName()),
70+
$columnDefinition->getColumnDefinition()->getSQLDefinition()
71+
);
72+
}
73+
74+
public function getDropColumnCommand(string $schemaName, string $tableName, string $columnName): string
75+
{
76+
return sprintf(
77+
'ALTER TABLE %s.%s DROP %s',
78+
TeradataQuote::quoteSingleIdentifier($schemaName),
79+
TeradataQuote::quoteSingleIdentifier($tableName),
80+
TeradataQuote::quoteSingleIdentifier($columnName)
81+
);
82+
}
83+
6384
/**
6485
* @inheritDoc
6586
*/

tests/Functional/Teradata/Table/TeradataTableQueryBuilderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,44 @@ public function testGetCreateCommand(
132132
self::assertSame($expectedPKs, $tableReflection->getPrimaryKeysNames());
133133
}
134134

135+
public function testAddAndDropColumn(): void
136+
{
137+
$dbName = $this->getDatabaseName();
138+
$columns = [TeradataColumn::createGenericColumn('col1'),
139+
TeradataColumn::createGenericColumn('col2')];
140+
$sql = $this->qb->getCreateTableCommand(
141+
$this->getDatabaseName(),
142+
self::TABLE_GENERIC,
143+
new ColumnCollection($columns),
144+
145+
);
146+
$this->connection->executeQuery($sql);
147+
148+
// add column
149+
$sql = $this->qb->getAddColumnCommand($dbName, self::TABLE_GENERIC, TeradataColumn::createGenericColumn('col3'));
150+
$this->assertEquals(sprintf('ALTER TABLE "%s"."%s" ADD "col3" VARCHAR (32000) NOT NULL DEFAULT \'\' CHARACTER SET UNICODE', $dbName, self::TABLE_GENERIC), $sql);
151+
$this->connection->executeQuery($sql);
152+
153+
$tableReflection = new TeradataTableReflection(
154+
$this->connection,
155+
$this->getDatabaseName(),
156+
self::TABLE_GENERIC
157+
);
158+
self::assertSame(['col1', 'col2', 'col3'], $tableReflection->getColumnsNames());
159+
160+
// drop column
161+
$sql = $this->qb->getDropColumnCommand($dbName, self::TABLE_GENERIC, 'col2');
162+
$this->assertEquals(sprintf('ALTER TABLE "%s"."%s" DROP "col2"', $dbName, self::TABLE_GENERIC), $sql);
163+
$this->connection->executeQuery($sql);
164+
165+
$tableReflection = new TeradataTableReflection(
166+
$this->connection,
167+
$this->getDatabaseName(),
168+
self::TABLE_GENERIC
169+
);
170+
self::assertSame(['col1', 'col3'], $tableReflection->getColumnsNames());
171+
}
172+
135173
/**
136174
* @return \Generator<string, mixed, mixed, mixed>
137175
*/

0 commit comments

Comments
 (0)