Skip to content

Commit

Permalink
TER-82 Assert column types on typed tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkasemmler committed Dec 8, 2022
1 parent 75e1f2f commit b5ae9db
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
12 changes: 12 additions & 0 deletions src/Backend/Teradata/ToStage/FromTableInsertIntoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
79 changes: 71 additions & 8 deletions tests/functional/Teradata/ToStage/StageImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -32,15 +36,15 @@ 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)
)
);

$importer = new ToStageImporter($this->connection);
$ref = new TeradataTableReflection(
$this->connection,
self::TEST_DATABASE,
$this->getDestinationDbName(),
self::TABLE_GENERIC
);

Expand Down Expand Up @@ -68,15 +72,15 @@ 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)
)
);

$importer = new ToStageImporter($this->connection);
$ref = new TeradataTableReflection(
$this->connection,
self::TEST_DATABASE,
$this->getDestinationDbName(),
self::TABLE_GENERIC
);

Expand All @@ -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);
}
Expand All @@ -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');

Expand All @@ -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
)
);
}
}

0 comments on commit b5ae9db

Please sign in to comment.