OpenMetadata/scripts/datamodel_generation.py
Sriharsha Chintalapani e4d3e423e1
Feature #18173: Version API Improvements, Last x versions order by desc, versions from specific timeline, versions for specific metadata changes, sdk support and UI integration (#26307)
* Feature #18173: Improve Version API, through paginatio, get x latest versions, specifict time, specific metadata changes

* Feature #18173: Version API Improvements, Last x versions order by desc, versions from specific timeline, versions for specific metadata changes, sdk support and UI integration

* Update generated TypeScript types

* address comments

* fix py check

* Address comments

* Address comments

* Fix tests

* Fix tests

* Fix tests

* Better way to lookup versions

* Fix pytests

* Fix tests

* Address comments

* chore(migrations): move version API schema additions from 1.13.0 to 1.12.7

Moves the PR's new entity_extension columns (versionNum, changedFieldKeys),
indexes, and backfill scripts from the 1.13.0 migration directory into a
new 1.12.7 directory. Keeps 1.13.0 identical to upstream main; only this
PR's additions land in 1.12.7.

Also updates MigrationSqlStatementHashTest to exercise the relocated files.

* fix(versions): address CI failures and review feedback

- testAPI.test.ts: update getTestCaseVersionList mock expectation to include
  the new params argument (APIClient.get is called with { params } since the
  function now supports limit/offset/fieldChanged).

- PaginatedVersionHistory.spec.ts: replace banned networkidle waits and
  waitForSelector with web-first assertion on version-button visibility
  (satisfies playwright/no-networkidle and playwright/no-wait-for-selector).

- EntityVersionTimeLine.tsx: implement infinite scroll via IntersectionObserver
  on a sentinel element at the bottom of the version list. Hooks up the
  onLoadMore/hasMore/isLoadingMore props that were in the interface but
  previously unused.

- EntityVersionPage.component.tsx: fix stale-closure bugs in fetchMoreVersions
  (gitar-bot review). Use versionListRef for currentOffset and
  isLoadingMoreRef to gate concurrent invocations so IntersectionObserver
  double-firing does not cause duplicate appends.

- EntityResource.java: accept offset > 0 with default limit when no
  fieldChanged is provided, so pagination params are no longer silently
  ignored (Copilot review).

- datamodel_generation.py: raise explicit errors if generated files or
  expected replacement targets are missing, instead of silently succeeding
  when the generator output drifts (Copilot review).

* fix(checkstyle): format Java, ESLint/Prettier on UI, relax datamodel_generation strict check

- Java: spotless:apply on EntityResource.java (line-break formatting).
- Python: relax datamodel_generation.py DIRECT_IMPORT_FIXES check — replacement
  targets are alternative forms the generator may or may not emit. Only
  require the final marker ('from .paging import Paging') is present after
  replacements; the prior strict per-target check broke 'make generate'.
- UI lint: organize-imports, ESLint --fix, Prettier on all version-related
  files touched by the PR (resolves lint-src + lint-playwright CI checks).
- EntityVersionTimeLine: guard IntersectionObserver effect with isLoadingMore
  so the observer is torn down while a fetch is in flight (Copilot review).
- EntityVersionTimeline.test.tsx: add unit tests covering sentinel rendering
  conditions (hasMore, onLoadMore) and the isLoadingMore observer-guard
  (Copilot review).

* fix(ui-checkstyle): prettier+eslint on EntityVersionTimeline.test.tsx

Collapse import line and reorder JSX props (callbacks last) per repo
lint rules. Reruns ui-checkstyle-changed caught these in the new test
file from the previous commit.

* test(playwright): address @aniketkatkar97 review on PaginatedVersionHistory spec

- Add waitUntil: 'domcontentloaded' to every page.goto() call.
- Wait for loaders (waitForAllLoadersToDisappear) before asserting the
  version-button to avoid racing the initial entity render.
- Replace the manual { timeout: 15_000 } on versionSelectors.nth(1) with
  an explicit waitForResponse on the second paginated /versions call
  (offset > 0). This deterministically synchronises on the infinite-scroll
  fetch instead of a wall-clock timeout.

* fix: address Copilot review — one-shot observer + local SQL splitter

1. EntityVersionTimeLine.tsx: call observer.unobserve(entry.target) as
   soon as the sentinel first intersects so onLoadMore fires only once
   per attached observer. The effect reattaches a fresh observer after
   isLoadingMore flips back to false, so subsequent pages still load
   — we just no longer rely on the parent's in-flight ref as the sole
   stopgap against repeated fires for the same page.

2. MigrationSqlStatementHashTest.java: replace Flyway's non-public
   org.flywaydb.core.internal.* parser classes with a small, local SQL
   statement splitter. Handles line (--) and block comments, single-,
   double-, and backtick-quoted strings, backslash escapes, and doubled-
   quote escapes. Removes a brittle dependency on Flyway internals that
   could break on upgrades.

Tested:
- mvn test -pl openmetadata-service -Dtest=MigrationSqlStatementHashTest
  → 2 tests pass.
- yarn test EntityVersionTimeline.test.tsx → 8/8 tests pass.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: sonika-shah <sonika-shah@users.noreply.github.com>
Co-authored-by: mohitdeuex <mohit.y@deuexsolutions.com>
Co-authored-by: sonika-shah <sonikashah94@gmail.com>
Co-authored-by: Mohit Yadav <105265192+mohityadav766@users.noreply.github.com>
2026-04-23 12:17:40 +02:00

141 lines
6.4 KiB
Python

# Copyright 2021 Collate
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This script generates the Python models from the JSON Schemas definition. Additionally, it replaces the `SecretStr`
pydantic class used for the password fields with the `CustomSecretStr` pydantic class which retrieves the secrets
from a configured secrets' manager.
"""
import datamodel_code_generator.model.pydantic
from datamodel_code_generator.imports import Import
import os
import re
datamodel_code_generator.model.pydantic.types.IMPORT_SECRET_STR = Import.from_full_path(
"metadata.ingestion.models.custom_pydantic.CustomSecretStr"
)
from datamodel_code_generator.__main__ import main
current_directory = os.getcwd()
ingestion_path = "./" if current_directory.endswith("/ingestion") else "ingestion/"
directory_root = "../" if current_directory.endswith("/ingestion") else "./"
UTF_8 = "UTF-8"
UNICODE_REGEX_REPLACEMENT_FILE_PATHS = [
f"{ingestion_path}src/metadata/generated/schema/entity/classification/tag.py",
f"{ingestion_path}src/metadata/generated/schema/entity/events/webhook.py",
f"{ingestion_path}src/metadata/generated/schema/entity/teams/user.py",
f"{ingestion_path}src/metadata/generated/schema/entity/type.py",
f"{ingestion_path}src/metadata/generated/schema/type/basic.py",
]
args = f"--input {directory_root}openmetadata-spec/src/main/resources/json/schema --output-model-type pydantic_v2.BaseModel --use-annotated --base-class metadata.ingestion.models.custom_pydantic.BaseModel --input-file-type jsonschema --output {ingestion_path}src/metadata/generated/schema --set-default-enum-member".split(" ")
main(args)
for file_path in UNICODE_REGEX_REPLACEMENT_FILE_PATHS:
with open(file_path, "r", encoding=UTF_8) as file_:
content = file_.read()
# Python now requires to move the global flags at the very start of the expression
content = content.replace("(?U)", "(?u)")
with open(file_path, "w", encoding=UTF_8) as file_:
file_.write(content)
# Until https://github.com/koxudaxi/datamodel-code-generator/issues/1895
# TODO: This has been merged but `Union` is still not there. We'll need to validate
MISSING_IMPORTS = [f"{ingestion_path}src/metadata/generated/schema/entity/applications/app.py",]
WRITE_AFTER = "from __future__ import annotations"
for file_path in MISSING_IMPORTS:
with open(file_path, "r", encoding=UTF_8) as file_:
lines = file_.readlines()
with open(file_path, "w", encoding=UTF_8) as file_:
for line in lines:
file_.write(line)
if line.strip() == WRITE_AFTER:
file_.write("from typing import Union # custom generate import\n\n")
# datamodel-code-generator emits a module alias for paging.json that pydantic
# later fails to resolve while importing the generated model during pytest
# plugin bootstrap. Import the concrete type directly instead.
DIRECT_IMPORT_FIXES = {
f"{ingestion_path}src/metadata/generated/schema/type/entityHistory.py": [
(
"from . import changeSummaryMap, paging",
"from . import changeSummaryMap\nfrom .paging import Paging",
),
("from . import paging as paging_module", "from .paging import Paging"),
("Optional[paging.Paging]", "Optional[Paging]"),
("Optional[paging_module.Paging]", "Optional[Paging]"),
],
}
DIRECT_IMPORT_FIXES_EXPECTED = {
f"{ingestion_path}src/metadata/generated/schema/type/entityHistory.py": (
"from .paging import Paging"
),
}
for file_path, replacements in DIRECT_IMPORT_FIXES.items():
if not os.path.exists(file_path):
raise FileNotFoundError(
f"Expected generated file not found for DIRECT_IMPORT_FIXES: {file_path}"
)
with open(file_path, "r", encoding=UTF_8) as file_:
content = file_.read()
for old_value, new_value in replacements:
content = content.replace(old_value, new_value)
expected_marker = DIRECT_IMPORT_FIXES_EXPECTED.get(file_path)
if expected_marker is not None and expected_marker not in content:
raise RuntimeError(
f"DIRECT_IMPORT_FIXES produced no change in {file_path} "
f"(expected {expected_marker!r} to be present). "
"The generator output may have changed; update datamodel_generation.py."
)
with open(file_path, "w", encoding=UTF_8) as file_:
file_.write(content)
# unsupported rust regex pattern for pydantic v2
# https://docs.pydantic.dev/2.7/api/config/#pydantic.config.ConfigDict.regex_engine
# We'll remove validation from the client and let it fail on the server, rather than on the model generation
UNSUPPORTED_REGEX_PATTERN_FILE_PATHS = [
f"{ingestion_path}src/metadata/generated/schema/type/basic.py",
f"{ingestion_path}src/metadata/generated/schema/entity/data/searchIndex.py",
f"{ingestion_path}src/metadata/generated/schema/entity/data/table.py",
]
for file_path in UNSUPPORTED_REGEX_PATTERN_FILE_PATHS:
with open(file_path, "r", encoding=UTF_8) as file_:
content = file_.read()
content = content.replace("pattern='^((?!::).)*$',", "")
with open(file_path, "w", encoding=UTF_8) as file_:
file_.write(content)
# Until https://github.com/koxudaxi/datamodel-code-generator/issues/1996
# Supporting timezone aware datetime is too complex for the profiler
DATETIME_AWARE_FILE_PATHS = [
f"{ingestion_path}src/metadata/generated/schema/type/basic.py",
]
for file_path in DATETIME_AWARE_FILE_PATHS:
with open(file_path, "r", encoding=UTF_8) as file_:
content = file_.read()
content = content.replace(
"from pydantic import AnyUrl, AwareDatetime, ConfigDict, EmailStr, Field, RootModel",
"from pydantic import AnyUrl, ConfigDict, EmailStr, Field, RootModel"
)
content = content.replace("from datetime import date, time", "from datetime import date, time, datetime")
content = content.replace("AwareDatetime", "datetime")
with open(file_path, "w", encoding=UTF_8) as file_:
file_.write(content)