|
7 | 7 | use Generator; |
8 | 8 | use Google\Cloud\BigQuery\Exception\JobException; |
9 | 9 | use Google\Cloud\Core\Exception\NotFoundException; |
| 10 | +use Keboola\Datatype\Definition\Bigquery; |
10 | 11 | use Keboola\TableBackendUtils\Column\Bigquery\BigqueryColumn; |
11 | 12 | use Keboola\TableBackendUtils\Column\ColumnCollection; |
12 | 13 | use Keboola\TableBackendUtils\Table\Bigquery\BigqueryTableQueryBuilder; |
@@ -106,4 +107,63 @@ public function testGetDropTableCommand(): void |
106 | 107 | $this->expectException(TableNotExistsReflectionException::class); |
107 | 108 | $ref->getRowsCount(); |
108 | 109 | } |
| 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 | + } |
109 | 169 | } |
0 commit comments