Skip to content
Closed
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 @@ -13,6 +13,7 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
Expand All @@ -29,13 +30,15 @@
import org.openmetadata.schema.api.services.CreateDatabaseService;
import org.openmetadata.schema.api.services.CreateDatabaseService.DatabaseServiceType;
import org.openmetadata.schema.api.services.DatabaseConnection;
import org.openmetadata.schema.entity.Type;
import org.openmetadata.schema.entity.data.Database;
import org.openmetadata.schema.entity.data.DatabaseSchema;
import org.openmetadata.schema.entity.data.Table;
import org.openmetadata.schema.entity.domains.Domain;
import org.openmetadata.schema.entity.services.DatabaseService;
import org.openmetadata.schema.entity.services.connections.TestConnectionResult;
import org.openmetadata.schema.entity.services.connections.TestConnectionResultStatus;
import org.openmetadata.schema.entity.type.CustomProperty;
import org.openmetadata.schema.services.connections.database.ConnectionArguments;
import org.openmetadata.schema.services.connections.database.ConnectionOptions;
import org.openmetadata.schema.services.connections.database.MysqlConnection;
Expand All @@ -47,10 +50,14 @@
import org.openmetadata.schema.type.Column;
import org.openmetadata.schema.type.ColumnDataType;
import org.openmetadata.schema.type.EntityHistory;
import org.openmetadata.schema.type.TableConstraint;
import org.openmetadata.schema.type.TagLabel;
import org.openmetadata.schema.type.csv.CsvImportResult;
import org.openmetadata.schema.utils.JsonUtils;
import org.openmetadata.sdk.client.OpenMetadataClient;
import org.openmetadata.sdk.models.ListParams;
import org.openmetadata.sdk.models.ListResponse;
import org.openmetadata.sdk.network.HttpMethod;

/**
* Integration tests for DatabaseService entity operations.
Expand Down Expand Up @@ -736,6 +743,250 @@ void test_importExportRecursive_withColumnTagsAndGlossaryTerms(TestNamespace ns)
"Street column should STILL have glossary term (not removed)");
}

@Test
void test_importExportRecursive_preservesTableConstraints(TestNamespace ns)
throws IOException, InterruptedException {
String serviceName = ns.prefix("import_export_recursive_constraints_service");
DatabaseService service = createEntity(createMinimalRequest(ns).withName(serviceName));

Database database =
SdkClients.adminClient()
.databases()
.create(
new CreateDatabase()
.withName(ns.prefix("db1"))
.withService(service.getFullyQualifiedName()));

DatabaseSchema schema =
SdkClients.adminClient()
.databaseSchemas()
.create(
new CreateDatabaseSchema()
.withName(ns.prefix("schema1"))
.withDatabase(database.getFullyQualifiedName()));

// Referenced table for the FOREIGN_KEY (exercises the table-to-table RELATED_TO edge path).
Table refTable =
SdkClients.adminClient()
.tables()
.create(
new CreateTable()
.withName(ns.prefix("ref_table"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withColumns(
List.of(
new Column().withName("ref_id").withDataType(ColumnDataType.BIGINT))));
String refIdFqn = refTable.getFullyQualifiedName() + ".ref_id";

List<TableConstraint> constraints =
List.of(
new TableConstraint()
.withConstraintType(TableConstraint.ConstraintType.PRIMARY_KEY)
.withColumns(List.of("id")),
new TableConstraint()
.withConstraintType(TableConstraint.ConstraintType.UNIQUE)
.withColumns(List.of("email")),
new TableConstraint()
.withConstraintType(TableConstraint.ConstraintType.FOREIGN_KEY)
.withColumns(List.of("ref_fk"))
.withReferredColumns(List.of(refIdFqn)));

Table table =
SdkClients.adminClient()
.tables()
.create(
new CreateTable()
.withName(ns.prefix("constrained_table"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withColumns(
List.of(
new Column().withName("id").withDataType(ColumnDataType.BIGINT),
new Column()
.withName("email")
.withDataType(ColumnDataType.VARCHAR)
.withDataLength(255),
new Column().withName("ref_fk").withDataType(ColumnDataType.BIGINT)))
.withTableConstraints(constraints));

assertEquals(
3, table.getTableConstraints().size(), "Table should start with 3 table constraints");

// Recursive export then re-import unchanged: this is the whole-tree path the UI uses when
// importing at service/database/schema level. The recursive CSV has no column for table
// constraints, so a round trip must not drop them.
String exportedCsv = exportCsvRecursive(service.getFullyQualifiedName());
assertNotNull(exportedCsv);

CsvImportResult result =
importCsvRecursive(service.getFullyQualifiedName(), exportedCsv, false);
assertEquals(ApiStatus.SUCCESS, result.getStatus());

Table reloaded =
SdkClients.adminClient()
.tables()
.getByName(table.getFullyQualifiedName(), "tableConstraints");
assertNotNull(
reloaded.getTableConstraints(),
"Table constraints must survive a recursive CSV round trip");
assertEquals(
3,
reloaded.getTableConstraints().size(),
"PRIMARY_KEY, UNIQUE and FOREIGN_KEY constraints must all be preserved after recursive import");
assertTrue(
reloaded.getTableConstraints().stream()
.anyMatch(c -> c.getConstraintType() == TableConstraint.ConstraintType.PRIMARY_KEY),
"PRIMARY_KEY constraint must be preserved after a recursive import");
assertTrue(
reloaded.getTableConstraints().stream()
.anyMatch(c -> c.getConstraintType() == TableConstraint.ConstraintType.UNIQUE),
"UNIQUE constraint must be preserved after a recursive import");
// FOREIGN_KEY carries the referenced-table linkage (referredColumns); it must survive intact.
TableConstraint fk =
reloaded.getTableConstraints().stream()
.filter(c -> c.getConstraintType() == TableConstraint.ConstraintType.FOREIGN_KEY)
.findFirst()
.orElse(null);
assertNotNull(fk, "FOREIGN_KEY constraint must be preserved after a recursive import");
assertEquals(
List.of(refIdFqn),
fk.getReferredColumns(),
"FOREIGN_KEY referredColumns (referenced-table linkage) must be preserved");
}

@Test
void test_importExportRecursive_preservesColumnCustomProperties(TestNamespace ns)
throws Exception {
String serviceName = ns.prefix("import_export_recursive_col_ext_service");
DatabaseService service = createEntity(createMinimalRequest(ns).withName(serviceName));

Database database =
SdkClients.adminClient()
.databases()
.create(
new CreateDatabase()
.withName(ns.prefix("db1"))
.withService(service.getFullyQualifiedName()));

DatabaseSchema schema =
SdkClients.adminClient()
.databaseSchemas()
.create(
new CreateDatabaseSchema()
.withName(ns.prefix("schema1"))
.withDatabase(database.getFullyQualifiedName()));

// A table-level custom property is defined and set alongside column-level custom properties.
// This is the realistic governance scenario AND the regression trigger: the bulk import update
// path used to blanket-delete every entity_extension row for a table that carried table-level
// extension, which also wiped the column-level custom properties.
OpenMetadataClient client = SdkClients.adminClient();
String tableProp = "csvRoundTripGovOwner";
addStringCustomPropertyToEntityType(client, "table", tableProp);

// Columns carry free-form custom properties (column.extension).
Column idColumn =
new Column()
.withName("id")
.withDataType(ColumnDataType.BIGINT)
.withExtension(Map.of("colGovOwner", "id-col@example.com"));
Column emailColumn =
new Column()
.withName("email")
.withDataType(ColumnDataType.VARCHAR)
.withDataLength(255)
.withExtension(Map.of("colGovOwner", "email-col@example.com"));

Table table =
SdkClients.adminClient()
.tables()
.create(
new CreateTable()
.withName(ns.prefix("col_ext_table"))
.withDatabaseSchema(schema.getFullyQualifiedName())
.withColumns(List.of(idColumn, emailColumn))
.withExtension(Map.of(tableProp, "table-gov-owner@example.com")));

// Precondition: table-level and column-level custom properties persisted on create.
Table created =
SdkClients.adminClient()
.tables()
.getByName(table.getFullyQualifiedName(), "columns,extension");
assertNotNull(created.getExtension(), "Table custom property should persist on create");
assertNotNull(
columnByName(created, "id").getExtension(),
"Column custom properties should persist on create");

// Recursive export then re-import unchanged (the whole-tree path the UI uses at
// service/database/schema level). The recursive CSV has no column for column custom
// properties, so a round trip must not drop them.
String exportedCsv = exportCsvRecursive(service.getFullyQualifiedName());
assertNotNull(exportedCsv);

CsvImportResult result =
importCsvRecursive(service.getFullyQualifiedName(), exportedCsv, false);
assertEquals(ApiStatus.SUCCESS, result.getStatus());

Table reloaded =
SdkClients.adminClient()
.tables()
.getByName(table.getFullyQualifiedName(), "columns,extension");
// Table-level custom property must survive (and must not take column-level ones down with it).
assertNotNull(
reloaded.getExtension(), "Table custom property must survive a recursive CSV round trip");
assertTrue(
reloaded.getExtension().toString().contains("table-gov-owner@example.com"),
Comment thread
sonika-shah marked this conversation as resolved.
Resolved
"Table custom property value must be preserved after a recursive import");
assertNotNull(
columnByName(reloaded, "id").getExtension(),
"Column 'id' custom properties must survive a recursive CSV round trip");
assertTrue(
columnByName(reloaded, "id").getExtension().toString().contains("id-col@example.com"),
"Column 'id' custom property value must be preserved after a recursive import");
assertNotNull(
columnByName(reloaded, "email").getExtension(),
"Column 'email' custom properties must survive a recursive CSV round trip");
assertTrue(
columnByName(reloaded, "email").getExtension().toString().contains("email-col@example.com"),
"Column 'email' custom property value must be preserved after a recursive import");
}

private Column columnByName(Table table, String name) {
return table.getColumns().stream()
.filter(c -> name.equals(c.getName()))
.findFirst()
.orElseThrow(() -> new AssertionError("Column not found: " + name));
}

/** Idempotently defines a String custom property on an entity type (e.g. "table"). */
private void addStringCustomPropertyToEntityType(
OpenMetadataClient client, String entityTypeName, String propertyName) throws Exception {
Type stringType =
JsonUtils.readValue(
client
.getHttpClient()
.executeForString(HttpMethod.GET, "/v1/metadata/types/name/string", null),
Type.class);
Type entityType =
JsonUtils.readValue(
client
.getHttpClient()
.executeForString(
HttpMethod.GET, "/v1/metadata/types/name/" + entityTypeName, null),
Type.class);
CustomProperty customProperty =
new CustomProperty()
.withName(propertyName)
.withDescription("CSV round-trip test custom property: " + propertyName)
.withPropertyType(stringType.getEntityReference());
client
.getHttpClient()
.execute(
HttpMethod.PUT,
"/v1/metadata/types/" + entityType.getId().toString(),
customProperty,
Type.class);
}

private String addColumnTags(String csvLine, String tagFQN) {
String[] parts = splitCsvRow(csvLine);
if (parts.length >= 5) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1721,11 +1721,16 @@ protected void createTableEntity(CSVPrinter printer, CSVRecord csvRecord, String
.withId(UUID.randomUUID());
}
} else {
// Dry Run = false, True Run: Use dependency resolution helper
// Dry Run = false, True Run: Use dependency resolution helper.
// Fetch tableConstraints/tablePartition too: the recursive CSV has no column for them, so
// they must be carried through the createOrUpdate below or they would be dropped.
try {
table =
getEntityWithDependencyResolution(
TABLE, tableFqn, "owners,tags,domains,extension", Include.NON_DELETED);
TABLE,
tableFqn,
"owners,tags,domains,extension,tableConstraints,tablePartition",
Comment thread
sonika-shah marked this conversation as resolved.
Resolved
Include.NON_DELETED);
} catch (EntityNotFoundException ex) {
// Table not found, create a new one
LOG.warn("Table not found: {}, it will be created with Import.", tableFqn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1719,6 +1719,19 @@ List<ExtensionRecord> getExtensionsWithOffset(
default void deleteAllBatch(List<String> ids) {
EntityDAO.updateInChunks(ids, this::deleteAllBatchInternal);
}

@SqlUpdate("DELETE FROM entity_extension WHERE id IN (<ids>) AND jsonSchema = :jsonSchema")
void deleteByJsonSchemaBatchInternal(
@BindList("ids") List<String> ids, @Bind("jsonSchema") String jsonSchema);

/**
* Batch-delete only the rows of a given jsonSchema for the ids -- one statement per chunk. Used
* to clear an entity's own custom-property rows ("customFieldSchema") without touching its
* columnExtension or version rows the way deleteAllBatch() would.
*/
default void deleteByJsonSchemaBatch(List<String> ids, String jsonSchema) {
EntityDAO.updateInChunks(ids, chunk -> deleteByJsonSchemaBatchInternal(chunk, jsonSchema));
}
}

class EntityVersionPair {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,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 467 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 467 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 467 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
@Getter protected final boolean supportsStyle;
@Getter protected final boolean supportsLifeCycle;
@Getter protected final boolean supportsCertification;
Expand Down Expand Up @@ -5241,8 +5241,12 @@
.map(entity -> entity.getId().toString())
.toList();

// Batch-delete only the entities' own custom-property rows (jsonSchema "customFieldSchema") in
// one statement per chunk. deleteAllBatch() would delete EVERY entity_extension row for the id
// -- including columnExtension rows -- so a bulk import update of a table that carries
// table-level extension would silently wipe its column-level custom properties.
if (!entityIds.isEmpty()) {
daoCollection.entityExtensionDAO().deleteAllBatch(entityIds);
daoCollection.entityExtensionDAO().deleteByJsonSchemaBatch(entityIds, "customFieldSchema");
Comment thread
sonika-shah marked this conversation as resolved.
Resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,7 @@ void test_createTableEntityQueuesNewTableUsingResolvedSchema() throws Exception
.getEntityWithDependencyResolution(
Entity.TABLE,
tableFqn,
"owners,tags,domains,extension",
"owners,tags,domains,extension,tableConstraints,tablePartition",
org.openmetadata.schema.type.Include.NON_DELETED);

try (MockedStatic<Entity> entity = Mockito.mockStatic(Entity.class);
Expand Down Expand Up @@ -3074,6 +3074,15 @@ private DataProductImportScenario importTableUpdateWithInheritedDomain(
relationshipRepository.getEntityReferences(
Mockito.anyList(), Mockito.eq(Include.NON_DELETED)))
.thenReturn(dataProduct.getDomains());
// On 1.13 the validateDataProductDomainMatch operator resolves a data product's domains via
// Entity.getEntities(...) (main resolves them through EntityRelationshipRepository instead).
// Stub that path so the rule sees the data product's real domains under the mocked Entity.
entityStatic
.when(
() ->
Entity.getEntities(
Mockito.anyList(), Mockito.eq("domains"), Mockito.eq(Include.NON_DELETED)))
.thenReturn(List.of(dataProduct));
settingsCache
.when(
() ->
Expand Down
Loading