Skip to content

Commit 9c5e393

Browse files
committed
Add support add/remove column
1 parent 385bcf4 commit 9c5e393

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

src/Table/Bigquery/BigqueryTableQueryBuilder.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,33 @@ public function getCreateTableCommandFromDefinition(
8383
: []
8484
);
8585
}
86+
87+
public function getAddColumnCommand(string $schemaName, string $tableName, BigqueryColumn $columnDefinition): string
88+
{
89+
assert(
90+
$columnDefinition->getColumnDefinition()->getDefault() === null,
91+
'You cannot add a REQUIRED column to an existing table schema.'
92+
);
93+
assert(
94+
$columnDefinition->getColumnDefinition()->isNullable() === true,
95+
'You cannot add a REQUIRED column to an existing table schema.'
96+
);
97+
return sprintf(
98+
'ALTER TABLE %s.%s ADD COLUMN %s %s',
99+
BigqueryQuote::quoteSingleIdentifier($schemaName),
100+
BigqueryQuote::quoteSingleIdentifier($tableName),
101+
BigqueryQuote::quoteSingleIdentifier($columnDefinition->getColumnName()),
102+
$columnDefinition->getColumnDefinition()->getSQLDefinition()
103+
);
104+
}
105+
106+
public function getDropColumnCommand(string $schemaName, string $tableName, string $columnName): string
107+
{
108+
return sprintf(
109+
'ALTER TABLE %s.%s DROP COLUMN %s',
110+
BigqueryQuote::quoteSingleIdentifier($schemaName),
111+
BigqueryQuote::quoteSingleIdentifier($tableName),
112+
BigqueryQuote::quoteSingleIdentifier($columnName)
113+
);
114+
}
86115
}

tests/Functional/Bigquery/Table/BigqueryTableQueryBuilderTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Generator;
88
use Google\Cloud\BigQuery\Exception\JobException;
99
use Google\Cloud\Core\Exception\NotFoundException;
10+
use Keboola\Datatype\Definition\Bigquery;
1011
use Keboola\TableBackendUtils\Column\Bigquery\BigqueryColumn;
1112
use Keboola\TableBackendUtils\Column\ColumnCollection;
1213
use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableQueryBuilder;
@@ -106,4 +107,63 @@ public function testGetDropTableCommand(): void
106107
$this->expectException(TableNotExistsReflectionException::class);
107108
$ref->getRowsCount();
108109
}
110+
111+
public function testAddAndDropColumn(): void
112+
{
113+
$this->cleanDataset(self::TEST_SCHEMA);
114+
$this->createDataset(self::TEST_SCHEMA);
115+
116+
$columns = [BigqueryColumn::createGenericColumn('col1'),
117+
BigqueryColumn::createGenericColumn('col2')];
118+
119+
$sql = $this->qb->getCreateTableCommand(
120+
self::TEST_SCHEMA,
121+
self::TABLE_GENERIC,
122+
new ColumnCollection($columns),
123+
[] // primary keys aren't supported in BQ
124+
);
125+
126+
$this->bqClient->runQuery($this->bqClient->query($sql));
127+
128+
// add column
129+
$sql = $this->qb->getAddColumnCommand(
130+
self::TEST_SCHEMA,
131+
self::TABLE_GENERIC,
132+
new BigqueryColumn('col3', new Bigquery(
133+
Bigquery::TYPE_STRING
134+
))
135+
);
136+
$this->assertEquals(
137+
sprintf(
138+
'ALTER TABLE `%s`.`%s` ADD COLUMN `col3` STRING',
139+
self::TEST_SCHEMA,
140+
self::TABLE_GENERIC
141+
),
142+
$sql
143+
);
144+
$this->bqClient->runQuery($this->bqClient->query($sql));
145+
146+
$tableReflection = new BigqueryTableReflection(
147+
$this->bqClient,
148+
self::TEST_SCHEMA,
149+
self::TABLE_GENERIC
150+
);
151+
self::assertSame(['col1', 'col2', 'col3'], $tableReflection->getColumnsNames());
152+
153+
// drop column
154+
$sql = $this->qb->getDropColumnCommand(self::TEST_SCHEMA, self::TABLE_GENERIC, 'col2');
155+
$this->assertEquals(sprintf(
156+
'ALTER TABLE `%s`.`%s` DROP COLUMN `col2`',
157+
self::TEST_SCHEMA,
158+
self::TABLE_GENERIC
159+
), $sql);
160+
$this->bqClient->runQuery($this->bqClient->query($sql));
161+
162+
$tableReflection = new BigqueryTableReflection(
163+
$this->bqClient,
164+
self::TEST_SCHEMA,
165+
self::TABLE_GENERIC
166+
);
167+
self::assertSame(['col1', 'col3'], $tableReflection->getColumnsNames());
168+
}
109169
}

0 commit comments

Comments
 (0)