public TableDto deleteAndReturn()

in metacat-main/src/main/java/com/netflix/metacat/main/services/impl/TableServiceImpl.java [343:440]


    public TableDto deleteAndReturn(final QualifiedName name, final boolean isMView) {
        final MetacatRequestContext metacatRequestContext = MetacatContextManager.getContext();
        validate(name);
        this.authorizationService.checkPermission(metacatRequestContext.getUserName(),
            name, MetacatOperation.DELETE);

        eventBus.post(new MetacatDeleteTablePreEvent(name, metacatRequestContext, this));

        TableDto tableDto = new TableDto();
        tableDto.setName(name);

        try {
            final Optional<TableDto> oTable = get(name,
                GetTableServiceParameters.builder()
                    .includeInfo(true)
                    .disableOnReadMetadataIntercetor(false)
                    .includeDefinitionMetadata(true)
                    .includeDataMetadata(true)
                    .build());
            tableDto = oTable.orElse(tableDto);
        } catch (Exception e) {
            handleException(name, true, "deleteAndReturn_get", e);
        }

        // Fail if the table is tagged not to be deleted.
        if (hasTags(tableDto, config.getNoTableDeleteOnTags())) {
            if (MetacatUtils.hasDoNotModifyForIcebergMigrationTag(tableDto, config.getNoTableDeleteOnTags())) {
                throw new TableMigrationInProgressException(
                        MetacatUtils.getIcebergMigrationExceptionMsg("Delete", name.toString()));
            } else {
                throw new IllegalArgumentException(
                        String.format("Table %s cannot be deleted because it is tagged with %s.", name,
                                config.getNoTableDeleteOnTags()));
            }
        }

        if (!config.isParentChildDropEnabled()) {
            if (parentChildRelMetadataService.isChildTable(name)) {
                throw new RuntimeException(name + " is a child table and "
                    + "dropping a child table is currently disabled");
            }
            if (parentChildRelMetadataService.isParentTable(name)) {
                throw new RuntimeException(name + " is a parent table and "
                    + "dropping a parent table is currently disabled");
            }
        }

        final Set<ChildInfo> childInfos = parentChildRelMetadataService.getChildren(name);
        if (childInfos != null && !childInfos.isEmpty()) {
            final StringBuilder errorSb = new StringBuilder();
            errorSb.append("Failed to drop ").append(name)
                .append(" because it still has ")
                .append(childInfos.size())
                .append(" child table(s). One example is:");

            errorSb.append("\n").append(childInfos.iterator().next().getName());

            throw new RuntimeException(errorSb.toString());
        }
        try {
            connectorTableServiceProxy.delete(name);
            // If this is a common view, the storage_table if present
            // should also be deleted.
            if (MetacatUtils.isCommonView(tableDto.getMetadata())
                    && config.deleteCommonViewStorageTable()) {
                final Optional<String> storageTableName = MetacatUtils
                        .getCommonViewStorageTable(tableDto.getMetadata());
                if (storageTableName.isPresent()) {
                    final QualifiedName qualifiedStorageTableName = QualifiedName.ofTable(name.getCatalogName(),
                            name.getDatabaseName(), storageTableName.get());
                    deleteCommonViewStorageTable(name, qualifiedStorageTableName);
                }
            }
        } catch (NotFoundException ignored) {
            log.debug("NotFoundException ignored for table {}", name);
        }

        try {
            parentChildRelMetadataService.drop(name);
        } catch (Exception e) {
            log.error("parentChildRelMetadataService: Failed to drop relation for table={}", name, e);
        }

        if (canDeleteMetadata(name)) {
            // Delete the metadata.  Type doesn't matter since we discard the result
            log.info("Deleting user metadata for table {}", name);
            userMetadataService.deleteMetadata(metacatRequestContext.getUserName(), Lists.newArrayList(tableDto));
            log.info("Deleting tags for table {}", name);
            tagService.delete(name, false);
        } else {
            if (config.canSoftDeleteDataMetadata() && tableDto.isDataExternal()) {
                userMetadataService.softDeleteDataMetadata(metacatRequestContext.getUserName(),
                    Lists.newArrayList(tableDto.getDataUri()));
            }
        }
        eventBus.post(new MetacatDeleteTablePostEvent(name, metacatRequestContext, this, tableDto, isMView));
        return tableDto;
    }