/** * Drop table if it's created by this instance. * Truncate staging tables passed by user. * {@inheritDoc} * @see org.apache.gobblin.Initializer#close() */ @Override public void close() { LOG.info("Closing " + this.getClass().getSimpleName()); try (Connection conn = createConnection()) { JdbcWriterCommands commands = createJdbcWriterCommands(conn); if (!this.createdStagingTables.isEmpty()) { for (String stagingTable : this.createdStagingTables) { LOG.info("Dropping staging table " + this.createdStagingTables); commands.drop(database, stagingTable); } } if (this.userCreatedStagingTable != null) { LOG.info("Truncating staging table " + this.userCreatedStagingTable); commands.truncate(database, this.userCreatedStagingTable); } } catch (SQLException e) { throw new RuntimeException("Failed to close", e); } }
commands.createTableStructure(database, destinationTable, tmp); LOG.info("Test if staging table can be dropped. Test by dropping and Creating staging table."); commands.drop(database, tmp); commands.createTableStructure(database, destinationTable, tmp); stagingTable = tmp;
public void userCreatedStagingTable() throws SQLException { this.state.setProp(ConfigurationKeys.WRITER_STAGING_TABLE, STAGING_TABLE); when(this.commands.isEmpty(DB, STAGING_TABLE)).thenReturn(Boolean.TRUE); this.initializer.initialize(); Assert.assertEquals(STAGING_TABLE, this.workUnit.getProp(ConfigurationKeys.WRITER_STAGING_TABLE)); verify(this.commands, never()).createTableStructure(anyString(), anyString(), anyString()); verify(this.commands, never()).truncate(anyString(), anyString()); verify(this.commands, never()).drop(anyString(), anyString()); }
public void skipStagingTable() throws SQLException { this.state.setProp(ConfigurationKeys.JOB_COMMIT_POLICY_KEY, "partial"); this.state.setProp(ConfigurationKeys.PUBLISH_DATA_AT_JOB_LEVEL, Boolean.toString(false)); this.initializer.initialize(); this.initializer.close(); Assert.assertEquals(DEST_TABLE, this.workUnit.getProp(ConfigurationKeys.WRITER_STAGING_TABLE)); verify(this.commands, never()).createTableStructure(anyString(), anyString(), anyString()); verify(this.commands, never()).truncate(anyString(), anyString()); verify(this.commands, never()).drop(anyString(), anyString()); }
public void initializeWithCreatingStagingTable() throws SQLException { when(this.commands.isEmpty(DB, STAGING_TABLE)).thenReturn(Boolean.TRUE); DatabaseMetaData metadata = mock(DatabaseMetaData.class); when(this.conn.getMetaData()).thenReturn(metadata); ResultSet rs = mock(ResultSet.class); when(metadata.getTables(anyString(), anyString(), anyString(), any(String[].class))).thenReturn(rs); when(rs.next()).thenReturn(Boolean.FALSE); this.initializer.initialize(); Assert.assertTrue(!StringUtils.isEmpty(this.workUnit.getProp(ConfigurationKeys.WRITER_STAGING_TABLE))); InOrder inOrder = inOrder(this.commands); inOrder.verify(this.commands, times(1)).createTableStructure(anyString(), anyString(), anyString()); inOrder.verify(this.commands, times(1)).drop(anyString(), anyString()); inOrder.verify(this.commands, times(1)).createTableStructure(anyString(), anyString(), anyString()); this.initializer.close(); inOrder.verify(this.commands, times(1)).drop(anyString(), anyString()); inOrder.verify(this.commands, never()).truncate(anyString(), anyString()); } }
public void userCreatedStagingTableTruncate() throws SQLException { this.state.setProp(ConfigurationKeys.WRITER_STAGING_TABLE, STAGING_TABLE); this.state.setProp(ConfigurationKeys.WRITER_TRUNCATE_STAGING_TABLE, Boolean.toString(true)); when(this.commands.isEmpty(DB, STAGING_TABLE)).thenReturn(Boolean.TRUE); this.initializer.initialize(); Assert.assertEquals(STAGING_TABLE, this.workUnit.getProp(ConfigurationKeys.WRITER_STAGING_TABLE)); InOrder inOrder = inOrder(this.commands); inOrder.verify(this.commands, times(1)).truncate(DB, STAGING_TABLE); this.initializer.close(); inOrder.verify(this.commands, times(1)).truncate(DB, STAGING_TABLE); verify(this.commands, never()).createTableStructure(anyString(), anyString(), anyString()); verify(this.commands, never()).drop(anyString(), anyString()); }
public void skipStagingTableTruncateDestTable() throws SQLException { this.state.setProp(ConfigurationKeys.JOB_COMMIT_POLICY_KEY, "partial"); this.state.setProp(ConfigurationKeys.PUBLISH_DATA_AT_JOB_LEVEL, Boolean.toString(false)); this.state.setProp(JdbcPublisher.JDBC_PUBLISHER_REPLACE_FINAL_TABLE, Boolean.toString(true)); this.initializer.initialize(); Assert.assertEquals(DEST_TABLE, this.workUnit.getProp(ConfigurationKeys.WRITER_STAGING_TABLE)); verify(this.commands, never()).createTableStructure(anyString(), anyString(), anyString()); InOrder inOrder = inOrder(this.commands); inOrder.verify(this.commands, times(1)).truncate(DB, DEST_TABLE); this.initializer.close(); inOrder.verify(this.commands, never()).truncate(anyString(), anyString()); verify(this.commands, never()).drop(anyString(), anyString()); }
/** * Drop table if it's created by this instance. * Truncate staging tables passed by user. * {@inheritDoc} * @see org.apache.gobblin.Initializer#close() */ @Override public void close() { LOG.info("Closing " + this.getClass().getSimpleName()); try (Connection conn = createConnection()) { JdbcWriterCommands commands = createJdbcWriterCommands(conn); if (!this.createdStagingTables.isEmpty()) { for (String stagingTable : this.createdStagingTables) { LOG.info("Dropping staging table " + this.createdStagingTables); commands.drop(database, stagingTable); } } if (this.userCreatedStagingTable != null) { LOG.info("Truncating staging table " + this.userCreatedStagingTable); commands.truncate(database, this.userCreatedStagingTable); } } catch (SQLException e) { throw new RuntimeException("Failed to close", e); } }
commands.createTableStructure(database, destinationTable, tmp); LOG.info("Test if staging table can be dropped. Test by dropping and Creating staging table."); commands.drop(database, tmp); commands.createTableStructure(database, destinationTable, tmp); stagingTable = tmp;