Issue #14067: DatabaseSchema Restore operation throws error EntityNotFound, if a table that belongs to schema is already restored (#14112)

* Issue #14067: DatabaseSchema Restore operation throws error EntityNotFound, if a table that belongs to schema is already restored

* Issue #14067: DatabaseSchema Restore operation throws error EntityNotFound, if a table that belongs to schema is already restored
This commit is contained in:
Sriharsha Chintalapani 2023-11-24 22:45:33 -08:00 committed by GitHub
parent b85f896e0f
commit d4fa62b3bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 8 deletions

View File

@ -1363,14 +1363,20 @@ public abstract class EntityRepository<T extends EntityInterface> {
// Finally set entity deleted flag to false // Finally set entity deleted flag to false
LOG.info("Restoring the {} {}", entityType, id); LOG.info("Restoring the {} {}", entityType, id);
T original = find(id, DELETED);
setFieldsInternal(original, putFields); try {
T updated = JsonUtils.readValue(JsonUtils.pojoToJson(original), entityClass); T original = find(id, DELETED);
updated.setUpdatedBy(updatedBy); setFieldsInternal(original, putFields);
updated.setUpdatedAt(System.currentTimeMillis()); T updated = JsonUtils.readValue(JsonUtils.pojoToJson(original), entityClass);
EntityUpdater updater = getUpdater(original, updated, Operation.PUT); updated.setUpdatedBy(updatedBy);
updater.update(); updated.setUpdatedAt(System.currentTimeMillis());
return new PutResponse<>(Status.OK, updated, RestUtil.ENTITY_RESTORED); EntityUpdater updater = getUpdater(original, updated, Operation.PUT);
updater.update();
return new PutResponse<>(Status.OK, updated, RestUtil.ENTITY_RESTORED);
} catch (EntityNotFoundException e) {
LOG.info("Entity is not in deleted state {} {}", entityType, id);
return null;
}
} }
public void addRelationship(UUID fromId, UUID toId, String fromEntity, String toEntity, Relationship relationship) { public void addRelationship(UUID fromId, UUID toId, String fromEntity, String toEntity, Relationship relationship) {

View File

@ -22,14 +22,18 @@ import static org.openmetadata.service.util.TestUtils.assertListNotNull;
import static org.openmetadata.service.util.TestUtils.assertListNull; import static org.openmetadata.service.util.TestUtils.assertListNull;
import static org.openmetadata.service.util.TestUtils.assertResponseContains; import static org.openmetadata.service.util.TestUtils.assertResponseContains;
import java.io.IOException;
import java.util.Map; import java.util.Map;
import javax.ws.rs.core.Response;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpResponseException; import org.apache.http.client.HttpResponseException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInfo;
import org.openmetadata.schema.api.data.CreateDatabaseSchema; import org.openmetadata.schema.api.data.CreateDatabaseSchema;
import org.openmetadata.schema.api.data.CreateTable; import org.openmetadata.schema.api.data.CreateTable;
import org.openmetadata.schema.api.data.RestoreEntity;
import org.openmetadata.schema.entity.data.DatabaseSchema; import org.openmetadata.schema.entity.data.DatabaseSchema;
import org.openmetadata.schema.entity.data.Table;
import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.type.EntityReference;
import org.openmetadata.service.Entity; import org.openmetadata.service.Entity;
import org.openmetadata.service.resources.EntityResourceTest; import org.openmetadata.service.resources.EntityResourceTest;
@ -55,6 +59,30 @@ class DatabaseSchemaResourceTest extends EntityResourceTest<DatabaseSchema, Crea
assertResponseContains(() -> createEntity(create, ADMIN_AUTH_HEADERS), BAD_REQUEST, "database must not be null"); assertResponseContains(() -> createEntity(create, ADMIN_AUTH_HEADERS), BAD_REQUEST, "database must not be null");
} }
@Test
void delete_schemaWithTables_200(TestInfo test) throws IOException {
CreateDatabaseSchema create = createRequest(test).withDatabase(DATABASE.getFullyQualifiedName());
DatabaseSchema createdSchema = createAndCheckEntity(create, ADMIN_AUTH_HEADERS);
TableResourceTest tableResourceTest = new TableResourceTest();
CreateTable createTable =
tableResourceTest.createRequest("t1", "", "", null).withDatabaseSchema(createdSchema.getFullyQualifiedName());
Table table1 = tableResourceTest.createEntity(createTable, ADMIN_AUTH_HEADERS);
createTable =
tableResourceTest.createRequest("t2", "", "", null).withDatabaseSchema(createdSchema.getFullyQualifiedName());
Table table2 = tableResourceTest.createEntity(createTable, ADMIN_AUTH_HEADERS);
// recursively soft delete schema
deleteAndCheckEntity(createdSchema, true, false, ADMIN_AUTH_HEADERS);
// Restore one of the tables.
tableResourceTest.restoreEntity(new RestoreEntity().withId(table2.getId()), Response.Status.OK, ADMIN_AUTH_HEADERS);
// Restore Schema
restoreEntity(new RestoreEntity().withId(createdSchema.getId()), Response.Status.OK, ADMIN_AUTH_HEADERS);
DatabaseSchema schema = getEntity(createdSchema.getId(), ADMIN_AUTH_HEADERS);
assertNotNull(schema);
}
@Override @Override
public DatabaseSchema validateGetWithDifferentFields(DatabaseSchema schema, boolean byName) public DatabaseSchema validateGetWithDifferentFields(DatabaseSchema schema, boolean byName)
throws HttpResponseException { throws HttpResponseException {