|
15 | 15 | import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; |
16 | 16 | import static org.openmetadata.common.utils.CommonUtil.nullOrEmpty; |
17 | 17 |
|
| 18 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | +import java.sql.Timestamp; |
| 20 | +import java.util.ArrayList; |
18 | 21 | import java.util.List; |
| 22 | +import java.util.Map; |
19 | 23 | import java.util.Set; |
20 | 24 | import lombok.extern.slf4j.Slf4j; |
| 25 | +import org.jdbi.v3.core.Handle; |
| 26 | +import org.jdbi.v3.core.statement.PreparedBatch; |
21 | 27 | import org.openmetadata.schema.api.search.Aggregation; |
22 | 28 | import org.openmetadata.schema.api.search.AllowedSearchFields; |
23 | 29 | import org.openmetadata.schema.api.search.AssetTypeConfiguration; |
24 | 30 | import org.openmetadata.schema.api.search.FieldBoost; |
25 | 31 | import org.openmetadata.schema.api.search.SearchSettings; |
26 | 32 | import org.openmetadata.schema.settings.Settings; |
| 33 | +import org.openmetadata.schema.type.TagLabel; |
| 34 | +import org.openmetadata.schema.utils.JsonUtils; |
| 35 | +import org.openmetadata.service.jdbi3.locator.ConnectionType; |
27 | 36 | import org.openmetadata.service.migration.utils.SearchSettingsMergeUtil; |
| 37 | +import org.openmetadata.service.util.FullyQualifiedName; |
28 | 38 |
|
29 | 39 | @Slf4j |
30 | 40 | public class MigrationUtil { |
@@ -164,4 +174,209 @@ private static boolean removeStaleSearchField(List<FieldBoost> searchFields) { |
164 | 174 | } |
165 | 175 | return removed; |
166 | 176 | } |
| 177 | + |
| 178 | + // Entity tables that v1125 attempted to drain of inline certification into tag_usage. |
| 179 | + private static final String[] CERTIFIED_ENTITY_TABLES = { |
| 180 | + "table_entity", |
| 181 | + "dashboard_entity", |
| 182 | + "topic_entity", |
| 183 | + "pipeline_entity", |
| 184 | + "storage_container_entity", |
| 185 | + "search_index_entity", |
| 186 | + "ml_model_entity", |
| 187 | + "stored_procedure_entity", |
| 188 | + "dashboard_data_model_entity", |
| 189 | + "api_endpoint_entity", |
| 190 | + "api_collection_entity", |
| 191 | + "database_entity", |
| 192 | + "database_schema_entity", |
| 193 | + "data_product_entity", |
| 194 | + "domain_entity", |
| 195 | + "chart_entity", |
| 196 | + "metric_entity", |
| 197 | + "file_entity", |
| 198 | + "directory_entity", |
| 199 | + "spreadsheet_entity", |
| 200 | + "worksheet_entity", |
| 201 | + "llm_model_entity", |
| 202 | + "ai_application_entity" |
| 203 | + }; |
| 204 | + |
| 205 | + private static final int HEAL_BATCH_SIZE = 500; |
| 206 | + private static final int CERT_SOURCE = TagLabel.TagSource.CLASSIFICATION.ordinal(); |
| 207 | + private static final int CERT_LABEL_TYPE = TagLabel.LabelType.AUTOMATED.ordinal(); |
| 208 | + private static final int CERT_STATE = TagLabel.State.CONFIRMED.ordinal(); |
| 209 | + |
| 210 | + /** |
| 211 | + * Heals installs where certification is still inlined in entity JSON instead of being in |
| 212 | + * tag_usage. v1125 on PostgreSQL silently failed on a missing varchar->json cast for the |
| 213 | + * metadata column: the insert threw, the per-table catch swallowed it, and v1125 was still |
| 214 | + * recorded as DONE — stranding certification in entity JSON with no tag_usage row. v1125 never |
| 215 | + * re-runs, so this heal (self-contained with the correct cast, independent of the v1125 SQL) |
| 216 | + * recovers those clusters. Idempotent: the select only matches rows that still carry |
| 217 | + * certification in JSON, so fresh installs, healthy MySQL, and already-healed PG databases exit |
| 218 | + * at zero rows. |
| 219 | + */ |
| 220 | + public static void healStuckCertificationOnEntityJson(Handle handle, ConnectionType connType) { |
| 221 | + int totalHealed = 0; |
| 222 | + for (String table : CERTIFIED_ENTITY_TABLES) { |
| 223 | + try { |
| 224 | + int healed = healCertificationForTable(handle, connType, table); |
| 225 | + totalHealed += healed; |
| 226 | + if (healed > 0) { |
| 227 | + LOG.info("v11210 heal: moved {} stuck certification rows from {}", healed, table); |
| 228 | + } |
| 229 | + } catch (Exception e) { |
| 230 | + // Per-table failure shouldn't stop the rest; logged at ERROR so it isn't lost. |
| 231 | + LOG.error("v11210 heal failed for table '{}': {}", table, e.getMessage(), e); |
| 232 | + } |
| 233 | + } |
| 234 | + LOG.info("v11210 heal: total stuck certification rows healed: {}", totalHealed); |
| 235 | + } |
| 236 | + |
| 237 | + private record BatchResult(int rowsFetched, int healed) {} |
| 238 | + |
| 239 | + private static int healCertificationForTable( |
| 240 | + Handle handle, ConnectionType connType, String table) { |
| 241 | + int totalHealed = 0; |
| 242 | + boolean morePages = true; |
| 243 | + while (morePages) { |
| 244 | + BatchResult batch = healCertificationBatch(handle, connType, table); |
| 245 | + totalHealed += batch.healed(); |
| 246 | + morePages = batch.rowsFetched() == HEAL_BATCH_SIZE; |
| 247 | + } |
| 248 | + return totalHealed; |
| 249 | + } |
| 250 | + |
| 251 | + private static BatchResult healCertificationBatch( |
| 252 | + Handle handle, ConnectionType connType, String table) { |
| 253 | + boolean isPostgres = connType == ConnectionType.POSTGRES; |
| 254 | + int healed = 0; |
| 255 | + List<Map<String, Object>> rows = |
| 256 | + handle.createQuery(buildSelectStuckCertificationSql(table, isPostgres)).mapToMap().list(); |
| 257 | + if (!nullOrEmpty(rows)) { |
| 258 | + List<String> selectedIds = new ArrayList<>(); |
| 259 | + PreparedBatch batch = handle.prepareBatch(buildInsertTagUsageSql(isPostgres)); |
| 260 | + for (Map<String, Object> row : rows) { |
| 261 | + String tagFQN = (String) row.get("tagfqn"); |
| 262 | + // SELECT already filters tagFQN NOT NULL, but stay defensive against concurrent edits. |
| 263 | + if (tagFQN != null) { |
| 264 | + selectedIds.add(row.get("id").toString()); |
| 265 | + batch |
| 266 | + .bind("source", CERT_SOURCE) |
| 267 | + .bind("tagFQN", tagFQN) |
| 268 | + .bind("tagFQNHash", FullyQualifiedName.buildHash(tagFQN)) |
| 269 | + .bind("targetFQNHash", row.get("fqnhash").toString()) |
| 270 | + .bind("labelType", CERT_LABEL_TYPE) |
| 271 | + .bind("state", CERT_STATE) |
| 272 | + .bind("appliedAt", parseEpochMillisToTimestamp(row.get("applieddate"))) |
| 273 | + .bind("metadata", buildCertMetadataJson(row.get("expirydate"))) |
| 274 | + .add(); |
| 275 | + } |
| 276 | + } |
| 277 | + if (!nullOrEmpty(selectedIds)) { |
| 278 | + batch.execute(); |
| 279 | + handle |
| 280 | + .createUpdate(buildStripCertificationFromJsonSql(table, isPostgres)) |
| 281 | + .bindList("ids", selectedIds) |
| 282 | + .execute(); |
| 283 | + healed = selectedIds.size(); |
| 284 | + } |
| 285 | + } |
| 286 | + return new BatchResult(rows.size(), healed); |
| 287 | + } |
| 288 | + |
| 289 | + private static String buildSelectStuckCertificationSql(String table, boolean isPostgres) { |
| 290 | + String sql; |
| 291 | + if (isPostgres) { |
| 292 | + sql = |
| 293 | + String.format( |
| 294 | + "SELECT id, fqnHash, " |
| 295 | + + "json::json -> 'certification' -> 'tagLabel' ->> 'tagFQN' AS tagFQN, " |
| 296 | + + "json::json -> 'certification' ->> 'expiryDate' AS expiryDate, " |
| 297 | + + "json::json -> 'certification' ->> 'appliedDate' AS appliedDate " |
| 298 | + + "FROM %s WHERE json::jsonb ?? 'certification' " |
| 299 | + + "AND json::json -> 'certification' -> 'tagLabel' ->> 'tagFQN' IS NOT NULL " |
| 300 | + + "LIMIT %d", |
| 301 | + table, HEAL_BATCH_SIZE); |
| 302 | + } else { |
| 303 | + sql = |
| 304 | + String.format( |
| 305 | + "SELECT id, fqnHash, " |
| 306 | + + "JSON_UNQUOTE(JSON_EXTRACT(json, '$.certification.tagLabel.tagFQN')) AS tagFQN, " |
| 307 | + + "JSON_UNQUOTE(JSON_EXTRACT(json, '$.certification.expiryDate')) AS expiryDate, " |
| 308 | + + "JSON_UNQUOTE(JSON_EXTRACT(json, '$.certification.appliedDate')) AS appliedDate " |
| 309 | + + "FROM %s WHERE JSON_CONTAINS_PATH(json, 'one', '$.certification') = 1 " |
| 310 | + + "AND JSON_UNQUOTE(JSON_EXTRACT(json, '$.certification.tagLabel.tagFQN')) IS NOT NULL " |
| 311 | + + "LIMIT %d", |
| 312 | + table, HEAL_BATCH_SIZE); |
| 313 | + } |
| 314 | + return sql; |
| 315 | + } |
| 316 | + |
| 317 | + private static String buildInsertTagUsageSql(boolean isPostgres) { |
| 318 | + // PG won't auto-cast varchar -> json on the metadata column; MySQL JSON accepts a string. |
| 319 | + String sql; |
| 320 | + if (isPostgres) { |
| 321 | + sql = |
| 322 | + "INSERT INTO tag_usage " |
| 323 | + + "(source, tagFQN, tagFQNHash, targetFQNHash, labelType, state, appliedBy, appliedAt, metadata) " |
| 324 | + + "VALUES (:source, :tagFQN, :tagFQNHash, :targetFQNHash, :labelType, :state, 'admin', :appliedAt, :metadata::json) " |
| 325 | + + "ON CONFLICT (source, tagFQNHash, targetFQNHash) DO NOTHING"; |
| 326 | + } else { |
| 327 | + sql = |
| 328 | + "INSERT IGNORE INTO tag_usage " |
| 329 | + + "(source, tagFQN, tagFQNHash, targetFQNHash, labelType, state, appliedBy, appliedAt, metadata) " |
| 330 | + + "VALUES (:source, :tagFQN, :tagFQNHash, :targetFQNHash, :labelType, :state, 'admin', :appliedAt, :metadata)"; |
| 331 | + } |
| 332 | + return sql; |
| 333 | + } |
| 334 | + |
| 335 | + private static String buildStripCertificationFromJsonSql(String table, boolean isPostgres) { |
| 336 | + String sql; |
| 337 | + if (isPostgres) { |
| 338 | + sql = |
| 339 | + "UPDATE " |
| 340 | + + table |
| 341 | + + " SET json = (json::jsonb - 'certification')::json WHERE id IN (<ids>)"; |
| 342 | + } else { |
| 343 | + sql = |
| 344 | + "UPDATE " |
| 345 | + + table |
| 346 | + + " SET json = JSON_REMOVE(json, '$.certification') WHERE id IN (<ids>)"; |
| 347 | + } |
| 348 | + return sql; |
| 349 | + } |
| 350 | + |
| 351 | + private static Timestamp parseEpochMillisToTimestamp(Object val) { |
| 352 | + Timestamp result = null; |
| 353 | + if (val != null) { |
| 354 | + try { |
| 355 | + long epochMillis = |
| 356 | + val instanceof Number num ? num.longValue() : Long.parseLong(val.toString()); |
| 357 | + result = new Timestamp(epochMillis); |
| 358 | + } catch (NumberFormatException e) { |
| 359 | + LOG.warn("Unparseable appliedDate '{}' on heal; binding null", val); |
| 360 | + } |
| 361 | + } |
| 362 | + return result; |
| 363 | + } |
| 364 | + |
| 365 | + private static String buildCertMetadataJson(Object expiryDateVal) { |
| 366 | + ObjectNode node = JsonUtils.getObjectNode(); |
| 367 | + if (expiryDateVal != null) { |
| 368 | + if (expiryDateVal instanceof Long longVal) { |
| 369 | + node.put("expiryDate", longVal); |
| 370 | + } else if (expiryDateVal instanceof Number numberVal) { |
| 371 | + node.put("expiryDate", numberVal.longValue()); |
| 372 | + } else { |
| 373 | + try { |
| 374 | + node.put("expiryDate", Long.parseLong(expiryDateVal.toString())); |
| 375 | + } catch (NumberFormatException e) { |
| 376 | + LOG.warn("Unparseable expiryDate '{}' on heal; omitting from metadata", expiryDateVal); |
| 377 | + } |
| 378 | + } |
| 379 | + } |
| 380 | + return node.toString(); |
| 381 | + } |
167 | 382 | } |
0 commit comments