diff --git a/src/Backend/Teradata/ToStage/FromTableInsertIntoAdapter.php b/src/Backend/Teradata/ToStage/FromTableInsertIntoAdapter.php index 13795a42..c59fad29 100644 --- a/src/Backend/Teradata/ToStage/FromTableInsertIntoAdapter.php +++ b/src/Backend/Teradata/ToStage/FromTableInsertIntoAdapter.php @@ -5,6 +5,7 @@ namespace Keboola\Db\ImportExport\Backend\Teradata\ToStage; use Doctrine\DBAL\Connection; +use Keboola\Db\ImportExport\Backend\Assert; use Keboola\Db\ImportExport\Backend\CopyAdapterInterface; use Keboola\Db\ImportExport\Backend\Teradata\TeradataImportOptions; use Keboola\Db\ImportExport\ImportOptionsInterface; @@ -46,6 +47,17 @@ public function runCopyCommand( $source->getFromStatement() ); + if ($source instanceof Table && $importOptions->usingUserDefinedTypes()) { + Assert::assertSameColumns( + (new TeradataTableReflection( + $this->connection, + $source->getSchema(), + $source->getTableName() + ))->getColumnsDefinitions(), + $destination->getColumnsDefinitions() + ); + } + if ($source instanceof SelectSource) { $this->connection->executeQuery($sql, $source->getQueryBindings(), $source->getDataTypes()); } else { diff --git a/tests/functional/Teradata/ToStage/StageImportTest.php b/tests/functional/Teradata/ToStage/StageImportTest.php index 35583412..3e4ff014 100644 --- a/tests/functional/Teradata/ToStage/StageImportTest.php +++ b/tests/functional/Teradata/ToStage/StageImportTest.php @@ -5,9 +5,13 @@ namespace Tests\Keboola\Db\ImportExportFunctional\Teradata\ToStage; use Keboola\CsvOptions\CsvOptions; +use Keboola\Db\ImportExport\Backend\Teradata\TeradataImportOptions; use Keboola\Db\ImportExport\Backend\Teradata\ToStage\Exception\FailedTPTLoadException; use Keboola\Db\ImportExport\Backend\Teradata\ToStage\Exception\NoMoreRoomInTDException; use Keboola\Db\ImportExport\Backend\Teradata\ToStage\ToStageImporter; +use Keboola\Db\ImportExport\Exception\ColumnsMismatchException; +use Keboola\Db\ImportExport\ImportOptionsInterface; +use Keboola\Db\ImportExport\Storage\Teradata\Table; use Keboola\TableBackendUtils\Escaping\Teradata\TeradataQuote; use Keboola\TableBackendUtils\Schema\Teradata\TeradataSchemaReflection; use Keboola\TableBackendUtils\Table\Teradata\TeradataTableReflection; @@ -18,8 +22,8 @@ class StageImportTest extends TeradataBaseTestCase protected function setUp(): void { parent::setUp(); - $this->cleanDatabase(self::TEST_DATABASE); - $this->createDatabase(self::TEST_DATABASE); + $this->cleanDatabase($this->getDestinationDbName()); + $this->createDatabase($this->getDestinationDbName()); } public function testSimpleStageImport(): void @@ -32,7 +36,7 @@ public function testSimpleStageImport(): void "first_name" CHAR(50), "last_name" CHAR(50) );', - TeradataQuote::quoteSingleIdentifier(self::TEST_DATABASE), + TeradataQuote::quoteSingleIdentifier($this->getDestinationDbName()), TeradataQuote::quoteSingleIdentifier(self::TABLE_GENERIC) ) ); @@ -40,7 +44,7 @@ public function testSimpleStageImport(): void $importer = new ToStageImporter($this->connection); $ref = new TeradataTableReflection( $this->connection, - self::TEST_DATABASE, + $this->getDestinationDbName(), self::TABLE_GENERIC ); @@ -68,7 +72,7 @@ public function testFailingImport(): void "id" INTEGER NOT NULL, "first_name" CHAR(1) );', - TeradataQuote::quoteSingleIdentifier(self::TEST_DATABASE), + TeradataQuote::quoteSingleIdentifier($this->getDestinationDbName()), TeradataQuote::quoteSingleIdentifier(self::TABLE_GENERIC) ) ); @@ -76,7 +80,7 @@ public function testFailingImport(): void $importer = new ToStageImporter($this->connection); $ref = new TeradataTableReflection( $this->connection, - self::TEST_DATABASE, + $this->getDestinationDbName(), self::TABLE_GENERIC ); @@ -94,7 +98,7 @@ public function testFailingImport(): void self::fail('should fail'); } catch (FailedTPTLoadException $e) { // nor target table nor LOG/ERR tables should be present - $scheRef = new TeradataSchemaReflection($this->connection, self::TEST_DATABASE); + $scheRef = new TeradataSchemaReflection($this->connection, $this->getDestinationDbName()); $tables = $scheRef->getTablesNames(); self::assertCount(0, $tables); } @@ -103,7 +107,7 @@ public function testFailingImport(): void public function testItWontFitIn(): void { // trying to immport big table to small DB via TPT -> should fail and throw custom exception - $dbName = self::TEST_DATABASE . '_small_db'; + $dbName = $this->getDestinationDbName() . '_small_db'; $this->cleanDatabase($dbName); $this->createDatabase($dbName, '1e5', '1e5'); @@ -123,4 +127,63 @@ public function testItWontFitIn(): void $this->getImportOptions() ); } + + + public function testMoveDataFromAToBRequireSameTablesFailColumnNameMismatch(): void + { + $this->connection->executeQuery( + sprintf( + 'CREATE MULTISET TABLE %s.%s + ( + "id" INTEGER, + "first_name" VARCHAR(100), + "last_name" VARCHAR(100) + );', + TeradataQuote::quoteSingleIdentifier($this->getDestinationDbName()), + TeradataQuote::quoteSingleIdentifier('sourceTable') + ) + ); + + $this->connection->executeQuery( + sprintf( + 'CREATE MULTISET TABLE %s.%s + ( + "id" INTEGER, + "first_name" VARCHAR(100), + "middle_name" VARCHAR(100), + "last_name" VARCHAR(100) + );', + TeradataQuote::quoteSingleIdentifier($this->getDestinationDbName()), + TeradataQuote::quoteSingleIdentifier('targetTable') + ) + ); + + $importer = new ToStageImporter($this->connection); + $targetTableRef = new TeradataTableReflection( + $this->connection, + $this->getDestinationDbName(), + 'targetTable' + ); + + $source = new Table( + $this->getDestinationDbName(), + 'sourceTable', + ['id', 'first_name', 'last_name'], + [] + ); + + $this->expectException(ColumnsMismatchException::class); + $this->expectExceptionMessage('Source destination columns name mismatch. "last_name"->"middle_name"'); + $importer->importToStagingTable( + $source, + $targetTableRef->getTableDefinition(), + $this->getImportOptions( + [], + false, + false, + 0, + ImportOptionsInterface::USING_TYPES_USER + ) + ); + } }