diff --git a/src/Backend/Synapse/ToFinalTable/FullImporter.php b/src/Backend/Synapse/ToFinalTable/FullImporter.php index 43bfe000..f3c9db9e 100644 --- a/src/Backend/Synapse/ToFinalTable/FullImporter.php +++ b/src/Backend/Synapse/ToFinalTable/FullImporter.php @@ -11,6 +11,7 @@ use Keboola\Db\ImportExport\Backend\Synapse\SynapseImportOptions; use Keboola\Db\ImportExport\Backend\ToFinalTableImporterInterface; use Keboola\Db\ImportExport\ImportOptionsInterface; +use Keboola\TableBackendUtils\Schema\SynapseSchemaReflection; use Keboola\TableBackendUtils\Table\SynapseTableDefinition; use Keboola\TableBackendUtils\Table\TableDefinitionInterface; @@ -114,21 +115,37 @@ private function doFullLoadWithDedup( $tmpDestinationToRemove = $destinationTableDefinition->getTableName() . self::OPTIMIZED_LOAD_RENAME_TABLE_SUFFIX; - $this->connection->executeStatement( - $this->sqlBuilder->getRenameTableCommand( + try { + $this->connection->executeStatement( + $this->sqlBuilder->getRenameTableCommand( + $destinationTableDefinition->getSchemaName(), + $destinationTableDefinition->getTableName(), + $tmpDestinationToRemove + ) + ); + $this->connection->executeStatement( + $this->sqlBuilder->getRenameTableCommand( + $tmpDestination->getSchemaName(), + $tmpDestination->getTableName(), + $destinationTableDefinition->getTableName() + ) + ); + } catch (\Throwable $e) { + if (!$this->isTableInSchema( $destinationTableDefinition->getSchemaName(), - $destinationTableDefinition->getTableName(), - $tmpDestinationToRemove - ) - ); - - $this->connection->executeStatement( - $this->sqlBuilder->getRenameTableCommand( - $tmpDestination->getSchemaName(), - $tmpDestination->getTableName(), $destinationTableDefinition->getTableName() - ) - ); + )) { + // in case of error ensure original table is renamed back + $this->connection->executeStatement( + $this->sqlBuilder->getRenameTableCommand( + $destinationTableDefinition->getSchemaName(), + $tmpDestinationToRemove, + $destinationTableDefinition->getTableName() + ) + ); + } + throw $e; + } $this->connection->executeStatement( $this->sqlBuilder->getDropCommand( @@ -168,21 +185,37 @@ private function doLoadFullWithoutDedup( $tmpDestinationToRemove = $destinationTableDefinition->getTableName() . self::OPTIMIZED_LOAD_RENAME_TABLE_SUFFIX; - $this->connection->executeStatement( - $this->sqlBuilder->getRenameTableCommand( + try { + $this->connection->executeStatement( + $this->sqlBuilder->getRenameTableCommand( + $destinationTableDefinition->getSchemaName(), + $destinationTableDefinition->getTableName(), + $tmpDestinationToRemove + ) + ); + $this->connection->executeStatement( + $this->sqlBuilder->getRenameTableCommand( + $tmpDestination->getSchemaName(), + $tmpDestination->getTableName(), + $destinationTableDefinition->getTableName() + ) + ); + } catch (\Throwable $e) { + if (!$this->isTableInSchema( $destinationTableDefinition->getSchemaName(), - $destinationTableDefinition->getTableName(), - $tmpDestinationToRemove - ) - ); - - $this->connection->executeStatement( - $this->sqlBuilder->getRenameTableCommand( - $tmpDestination->getSchemaName(), - $tmpDestination->getTableName(), $destinationTableDefinition->getTableName() - ) - ); + )) { + // in case of error ensure original table is renamed back + $this->connection->executeStatement( + $this->sqlBuilder->getRenameTableCommand( + $destinationTableDefinition->getSchemaName(), + $tmpDestinationToRemove, + $destinationTableDefinition->getTableName() + ) + ); + } + throw $e; + } $this->connection->executeStatement( $this->sqlBuilder->getDropCommand( @@ -191,4 +224,13 @@ private function doLoadFullWithoutDedup( ) ); } + + private function isTableInSchema(string $schemaName, string $tableName): bool + { + return in_array( + $tableName, + (new SynapseSchemaReflection($this->connection, $schemaName))->getTablesNames(), + true + ); + } }