diff --git a/.gitignore b/.gitignore index 19443e389dcfb..e63d02fa5b4cb 100644 --- a/.gitignore +++ b/.gitignore @@ -63,5 +63,3 @@ plugin-transpiler/dist *-esbuild-meta.json *-esbuild-bundle-visualization.html .dlt -# Crap generated by macOS sed – remove when `schema:build:python:fix-up-enum` is gone from package.json -*.py-e diff --git a/bin/build-schema.mjs b/bin/build-schema-json.mjs similarity index 100% rename from bin/build-schema.mjs rename to bin/build-schema-json.mjs diff --git a/bin/build-schema-python.sh b/bin/build-schema-python.sh new file mode 100755 index 0000000000000..d3415a7118dec --- /dev/null +++ b/bin/build-schema-python.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +set -e + +# Generate schema.py from schema.json +datamodel-codegen \ + --class-name='SchemaRoot' --collapse-root-models --target-python-version 3.10 --disable-timestamp \ + --use-one-literal-as-default --use-default --use-default-kwarg --use-subclass-enum \ + --input frontend/src/queries/schema.json --input-file-type jsonschema \ + --output posthog/schema.py --output-model-type pydantic_v2.BaseModel + +# Format schema.py +ruff format posthog/schema.py + +# HACK: Datamodel-codegen output for enum-type fields with a default is invalid – the default value is a plain string, +# and not the expected enum member. We fix this using sed, which is pretty hacky, but does the job. +# Specifically, we need to replace `Optional[PropertyOperator] = "exact"` +# with `Optional[PropertyOperator] = PropertyOperator("exact")` to make the default value valid. +# Remove this when https://github.com/koxudaxi/datamodel-code-generator/issues/1929 is resolved. +if [[ "$OSTYPE" == "darwin"* ]]; then + # sed needs `-i` to be followed by `''` on macOS + sed -i '' -e 's/Optional\[PropertyOperator\] = \("[A-Za-z_]*"\)/Optional[PropertyOperator] = PropertyOperator(\1)/g' posthog/schema.py +else + sed -i -e 's/Optional\[PropertyOperator\] = \("[A-Za-z_]*"\)/Optional[PropertyOperator] = PropertyOperator(\1)/g' posthog/schema.py +fi diff --git a/package.json b/package.json index 0f7a26d84a4f2..27b43a897392e 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,8 @@ "build": "pnpm copy-scripts && pnpm build:esbuild", "build:esbuild": "node frontend/build.mjs", "schema:build": "pnpm run schema:build:json && pnpm run schema:build:python", - "schema:build:json": "ts-node bin/build-schema.mjs && prettier --write frontend/src/queries/schema.json", - "schema:build:python": "datamodel-codegen --class-name='SchemaRoot' --collapse-root-models --target-python-version 3.10 --disable-timestamp --use-one-literal-as-default --use-default --use-default-kwarg --use-subclass-enum --input frontend/src/queries/schema.json --input-file-type jsonschema --output posthog/schema.py --output-model-type pydantic_v2.BaseModel && ruff format posthog/schema.py && npm run schema:build:python:fix-up-enum", - "schema:build:python:fix-up-enum": "sed -i -e 's/Optional\\[PropertyOperator\\] = \\(\"[A-Za-z_]*\"\\)/Optional[PropertyOperator] = PropertyOperator(\\1)/g' posthog/schema.py # THIS SED INVOCATION IS A MASSIVE HACK - remove when datamodel-codegen properly supports enums", + "schema:build:json": "ts-node bin/build-schema-json.mjs && prettier --write frontend/src/queries/schema.json", + "schema:build:python": "bash bin/build-schema-python.sh", "grammar:build": "npm run grammar:build:python && npm run grammar:build:cpp", "grammar:build:python": "cd posthog/hogql/grammar && antlr -Dlanguage=Python3 HogQLLexer.g4 && antlr -visitor -no-listener -Dlanguage=Python3 HogQLParser.g4", "grammar:build:cpp": "cd posthog/hogql/grammar && antlr -o ../../../hogql_parser -Dlanguage=Cpp HogQLLexer.g4 && antlr -o ../../../hogql_parser -visitor -no-listener -Dlanguage=Cpp HogQLParser.g4",