Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
Expand Down Expand Up @@ -29,6 +30,7 @@
import org.openmetadata.schema.entity.services.DatabaseService;
import org.openmetadata.schema.type.Column;
import org.openmetadata.schema.type.ColumnDataType;
import org.openmetadata.schema.type.TableConstraint;
import org.openmetadata.schema.type.TagLabel;

/**
Expand Down Expand Up @@ -81,6 +83,23 @@ void test_botOverwritesDescription_withOverride(TestNamespace ns) throws Excepti
"overrideMetadata=true lets a bot PUT overwrite the description");
}

@Test
void test_overrideDoesNotBlankDescription(TestNamespace ns) throws Exception {
String schemaFqn = setupSchema(ns);
String botToken = BulkApi.botToken();
CreateTable original = table(ns, schemaFqn, "ovr_desc_blank", "curated description", "hash-v1");
BulkApi.upsert("tables", List.of(original), false, botToken);

String fqn = schemaFqn + "." + original.getName();
CreateTable changed = table(ns, schemaFqn, "ovr_desc_blank", null, "hash-v2");
BulkApi.upsert("tables", List.of(changed), true, botToken);

assertEquals(
"curated description",
getTable(fqn).getDescription(),
"overrideMetadata=true must not blank a description when none is supplied");
}

@Test
void test_overrideMetadata_disablesSourceHashFastPath(TestNamespace ns) throws Exception {
String schemaFqn = setupSchema(ns);
Expand Down Expand Up @@ -120,6 +139,43 @@ void test_botCannotOverwriteDisplayName_withoutOverride(TestNamespace ns) throws
"a bot PUT must not overwrite a non-empty displayName without overrideMetadata");
}

@Test
void test_botOverwritesDisplayName_withOverride(TestNamespace ns) throws Exception {
String schemaFqn = setupSchema(ns);
String botToken = BulkApi.botToken();
CreateTable original = table(ns, schemaFqn, "ovr_dn_on", "desc", "hash-v1");
original.setDisplayName("Curated Display Name");
BulkApi.upsert("tables", List.of(original), false, botToken);

String fqn = schemaFqn + "." + original.getName();
CreateTable changed = table(ns, schemaFqn, "ovr_dn_on", "desc", "hash-v2");
changed.setDisplayName("Connector Display Name");
BulkApi.upsert("tables", List.of(changed), true, botToken);

assertEquals(
"Connector Display Name",
getTable(fqn).getDisplayName(),
"overrideMetadata=true lets a bot PUT overwrite the displayName");
}

@Test
void test_overrideDoesNotBlankDisplayName(TestNamespace ns) throws Exception {
String schemaFqn = setupSchema(ns);
String botToken = BulkApi.botToken();
CreateTable original = table(ns, schemaFqn, "ovr_dn_blank", "desc", "hash-v1");
original.setDisplayName("Curated Display Name");
BulkApi.upsert("tables", List.of(original), false, botToken);

String fqn = schemaFqn + "." + original.getName();
CreateTable changed = table(ns, schemaFqn, "ovr_dn_blank", "desc", "hash-v2");
BulkApi.upsert("tables", List.of(changed), true, botToken);

assertEquals(
"Curated Display Name",
getTable(fqn).getDisplayName(),
"overrideMetadata=true must not blank a displayName when none is supplied");
}

@Test
void test_botCannotOverwriteColumnDescription_withoutOverride(TestNamespace ns) throws Exception {
String schemaFqn = setupSchema(ns);
Expand Down Expand Up @@ -246,10 +302,68 @@ void test_overrideDoesNotRemoveTagsWhenNoneSupplied(TestNamespace ns) throws Exc
assertEquals(List.of(tag.getTagFQN()), tagFqns(getTable(fqn).getTags()));
}

@Test
void test_botPutWithoutConstraintsKeepsThem_withoutOverride(TestNamespace ns) throws Exception {
assertConstraintsSurviveBotPutWithoutThem(ns, "ovr_cons_off", false);
}

@Test
void test_botPutWithoutConstraintsKeepsThem_withOverride(TestNamespace ns) throws Exception {
assertConstraintsSurviveBotPutWithoutThem(ns, "ovr_cons_on", true);
}

@Test
void test_botPutDropsConstraintOnRemovedColumn(TestNamespace ns) throws Exception {
String schemaFqn = setupSchema(ns);
String botToken = BulkApi.botToken();
CreateTable original = table(ns, schemaFqn, "ovr_cons_col", "desc", "hash-v1");
original.setColumns(
List.of(
new Column().withName("c1").withDataType(ColumnDataType.STRING),
new Column().withName("c2").withDataType(ColumnDataType.STRING)));
original.setTableConstraints(List.of(primaryKey("c2")));
BulkApi.upsert("tables", List.of(original), false, botToken);

String fqn = schemaFqn + "." + original.getName();
CreateTable changed = table(ns, schemaFqn, "ovr_cons_col", "desc", "hash-v2");
BulkApi.upsert("tables", List.of(changed), true, botToken);

List<TableConstraint> constraints = getTable(fqn).getTableConstraints();
assertTrue(
constraints == null || constraints.isEmpty(),
"a constraint on a column the source dropped must still be removed: " + constraints);
}

// ===================================================================
// HELPERS
// ===================================================================

private void assertConstraintsSurviveBotPutWithoutThem(
TestNamespace ns, String baseName, boolean overrideMetadata) throws Exception {
String schemaFqn = setupSchema(ns);
String botToken = BulkApi.botToken();
CreateTable original = table(ns, schemaFqn, baseName, "desc", "hash-v1");
original.setTableConstraints(List.of(primaryKey("c1")));
BulkApi.upsert("tables", List.of(original), false, botToken);

String fqn = schemaFqn + "." + original.getName();
CreateTable changed = table(ns, schemaFqn, baseName, "desc", "hash-v2");
BulkApi.upsert("tables", List.of(changed), overrideMetadata, botToken);

List<TableConstraint> constraints = getTable(fqn).getTableConstraints();
assertNotNull(constraints, "a bot PUT without constraints must not remove the stored ones");
assertEquals(1, constraints.size());
assertEquals(
TableConstraint.ConstraintType.PRIMARY_KEY, constraints.getFirst().getConstraintType());
assertEquals(List.of("c1"), constraints.getFirst().getColumns());
}

private TableConstraint primaryKey(String column) {
return new TableConstraint()
.withConstraintType(TableConstraint.ConstraintType.PRIMARY_KEY)
.withColumns(List.of(column));
}

private void setColumnDescription(CreateTable createTable, String description) {
createTable.getColumns().getFirst().withDescription(description);
}
Expand Down Expand Up @@ -315,7 +429,10 @@ private Table getTable(String fqn) throws Exception {
HttpRequest.newBuilder()
.uri(
URI.create(
SdkClients.getServerUrl() + "/v1/tables/name/" + fqn + "?fields=columns,tags"))
SdkClients.getServerUrl()
+ "/v1/tables/name/"
+ fqn
+ "?fields=columns,tags,certification,tableConstraints"))
.header("Authorization", "Bearer " + SdkClients.getAdminToken())
.GET()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@
@Getter protected final Set<String> allowedFields;
public final boolean supportsSoftDelete;
@Getter protected final boolean supportsTags;
@Getter protected final boolean supportsOwners;

Check warning on line 545 in openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java

View workflow job for this annotation

GitHub Actions / Build Integration Test Runtime

Not generating isSupportsOwners(): A method with that name already exists

Check warning on line 545 in openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java

View workflow job for this annotation

GitHub Actions / Build Integration Test Runtime

Not generating isSupportsOwners(): A method with that name already exists

Check warning on line 545 in openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java

View workflow job for this annotation

GitHub Actions / Build Integration Test Runtime

Not generating isSupportsOwners(): A method with that name already exists

Check warning on line 545 in openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java

View workflow job for this annotation

GitHub Actions / maven-sonarcloud-ci

Not generating isSupportsOwners(): A method with that name already exists

Check warning on line 545 in openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java

View workflow job for this annotation

GitHub Actions / py-run-build-tests

Not generating isSupportsOwners(): A method with that name already exists
@Getter protected final boolean supportsStyle;
@Getter protected final boolean supportsLifeCycle;
@Getter protected final boolean supportsCertification;
Expand Down Expand Up @@ -9259,10 +9259,12 @@
if (operation.isPut()
&& !nullOrEmpty(original.getDescription())
&& updatedByBot()
&& !overrideMetadata) {
&& (!overrideMetadata || nullOrEmpty(updated.getDescription()))) {
// Revert change to non-empty description if it is being updated by a bot
// This is to prevent bots from overwriting the description. Description need to be
// updated with a PATCH request, or via the bulk path with overrideMetadata=true
// updated with a PATCH request, or via the bulk path with overrideMetadata=true. Even
// then an empty value never blanks it: a source with no comment omits the field, and an
// override run must not read that absence as "delete the description".
updated.setDescription(original.getDescription());
return;
}
Expand Down Expand Up @@ -9299,11 +9301,11 @@
// authorizes with the coarse EDIT_ALL operation, which does not intersect that field-level
// deny, so re-apply it here. Bots the policy allows - for example the SCIM bot syncing
// identity attributes through the repository - fall through and update it. A bulk force-sync
// (overrideMetadata=true) also bypasses this guard.
// (overrideMetadata=true) also bypasses this guard, unless it would blank the displayName.
boolean preserveUserDisplayName =
updatedByBot()
&& !nullOrEmpty(original.getDisplayName())
&& !overrideMetadata
&& (!overrideMetadata || nullOrEmpty(updated.getDisplayName()))
&& !Objects.equals(original.getDisplayName(), updated.getDisplayName())
&& updatingBotDeniedOperation(MetadataOperation.EDIT_DISPLAY_NAME);
if (preserveUserDisplayName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,15 @@ private void updateAliases(Table origTable, Table updatedTable) {
}

private void updateTableConstraints(Table origTable, Table updatedTable, Operation operation) {
// Many sources (e.g. Trino) never report constraints, so a bot PUT without any must not
// read that absence as "delete them" and wipe user-curated ones. Constraints on columns the
// source dropped are still cleaned up below.
if (operation.isPut()
&& updatedByBot()
&& nullOrEmpty(updatedTable.getTableConstraints())
&& !nullOrEmpty(origTable.getTableConstraints())) {
updatedTable.setTableConstraints(new ArrayList<>(origTable.getTableConstraints()));
}
// Detect columns that were removed (exist in original but not in updated).
// This also handles null column entries produced by JSON patch operations.
Set<String> removedColumns = detectRemovedColumns(origTable, updatedTable);
Expand Down
Loading