danhuawang commented on code in PR #15001:
URL: https://github.com/apache/gravitino/pull/15001#discussion_r4641659713
##########
dev/charts/gravitino/templates/_helpers.tpl:
##########
@@ -372,4 +493,256 @@ Database name for init-postgresql GRAVITINO_DB.
{{- else -}}
{{- .Values.postgresql.auth.database | default "gravitino" -}}
{{- end -}}
-{{- end -}}
\ No newline at end of file
+{{- end -}}
+{{/*
+True when the entity store uses an external MySQL JDBC URL (not in-chart
Bitnami).
+*/}}
+{{- define "gravitino.entityUsesExternalMysql" -}}
+{{- $url := .Values.entity.jdbcUrl | default "" -}}
+{{- if and (not .Values.mysql.enabled) (not .Values.postgresql.enabled)
(hasPrefix "jdbc:mysql://" $url) -}}
+true
+{{- end -}}
+{{- end -}}
+
+{{/*
+True when a relational JDBC backend is configured for schema migration.
+*/}}
+{{- define "gravitino.schemaHasTarget" -}}
+{{- if or .Values.mysql.enabled .Values.postgresql.enabled (eq (include
"gravitino.entityUsesExternalMysql" . | trim) "true") (eq (include
"gravitino.entityUsesExternalPostgresql" . | trim) "true") -}}
+true
+{{- end -}}
+{{- end -}}
+
+{{/*
+True when the schema-migrate Job should run (regular resource, not a Helm
hook).
+*/}}
+{{- define "gravitino.schemaMigrateEnabled" -}}
+{{- if .Values.schema.migrate.enabled -}}
+{{- if ne (include "gravitino.schemaHasTarget" . | trim) "true" -}}
+{{- fail "schema.migrate.enabled is true but no relational target: enable
mysql/postgresql or set entity.jdbcUrl to jdbc:mysql:// or jdbc:postgresql://"
-}}
+{{- end -}}
+true
+{{- end -}}
+{{- end -}}
+
+{{/*
+mysql or postgresql for schema migrate.
+*/}}
+{{- define "gravitino.schemaMigrateEngine" -}}
+{{- if .Values.mysql.enabled -}}
+mysql
+{{- else if .Values.postgresql.enabled -}}
+postgresql
+{{- else if eq (include "gravitino.entityUsesExternalMysql" . | trim) "true"
-}}
+mysql
+{{- else if eq (include "gravitino.entityUsesExternalPostgresql" . | trim)
"true" -}}
+postgresql
+{{- end -}}
+{{- end -}}
+
+{{/*
+Authority segment from jdbc:mysql:// URLs (single host:port only).
+*/}}
+{{- define "gravitino.entityJdbcMysqlAuthorityFromUrl" -}}
+{{- $url := .Values.entity.jdbcUrl | default "" -}}
+{{- if hasPrefix "jdbc:mysql://" $url -}}
+{{- $path := trimPrefix "jdbc:mysql://" $url -}}
+{{- $path = splitList "?" $path | first -}}
+{{- if contains "@" $path -}}
+{{- $path = last (splitList "@" $path) -}}
+{{- end -}}
+{{- $auth := splitList "/" $path | first -}}
+{{- if or (contains "[" $auth) (contains "," $auth) -}}
+{{- fail "entity.jdbcUrl authority is IPv6 or multi-host; set schema.dbHost
and schema.dbPort explicitly" -}}
+{{- end -}}
+{{- $auth -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+Database name from jdbc:mysql://host:port/db
+*/}}
+{{- define "gravitino.entityJdbcMysqlDatabaseFromUrl" -}}
+{{- $url := .Values.entity.jdbcUrl | default "" -}}
+{{- if hasPrefix "jdbc:mysql://" $url -}}
+{{- $path := trimPrefix "jdbc:mysql://" $url -}}
+{{- $parts := splitList "/" $path -}}
+{{- if ge (len $parts) 2 -}}
+{{- $dbPart := index $parts 1 -}}
+{{- $db := splitList "?" $dbPart | first -}}
+{{- if $db -}}
+{{- $db -}}
+{{- end -}}
+{{- end -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+Host for schema-migrate Job.
+*/}}
+{{- define "gravitino.schemaMigrateHost" -}}
+{{- $host := .Values.schema.dbHost | default "" | toString | trim -}}
+{{- if $host -}}
+{{- $host -}}
+{{- else if .Values.mysql.enabled -}}
+{{- include "gravitino.mysql.fullname" . -}}
+{{- else if .Values.postgresql.enabled -}}
+{{- include "gravitino.postgresql.fullname" . -}}
+{{- else if eq (include "gravitino.entityUsesExternalMysql" . | trim) "true"
-}}
+{{- $auth := include "gravitino.entityJdbcMysqlAuthorityFromUrl" . | trim -}}
+{{- if contains ":" $auth -}}
+{{- index (splitList ":" $auth) 0 -}}
+{{- else -}}
+{{- $auth -}}
+{{- end -}}
+{{- else -}}
+{{- $fromUrl := include "gravitino.entityJdbcHostFromUrl" . | trim -}}
+{{- if $fromUrl -}}
+{{- $fromUrl -}}
+{{- else -}}
+{{- fail "could not parse host from entity.jdbcUrl; set schema.dbHost" -}}
+{{- end -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+Port for schema-migrate Job.
+*/}}
+{{- define "gravitino.schemaMigratePort" -}}
+{{- $port := .Values.schema.dbPort | default "" | toString | trim -}}
+{{- if $port -}}
+{{- $port -}}
+{{- else if .Values.mysql.enabled -}}
+3306
+{{- else if .Values.postgresql.enabled -}}
+5432
+{{- else if eq (include "gravitino.entityUsesExternalMysql" . | trim) "true"
-}}
+{{- $auth := include "gravitino.entityJdbcMysqlAuthorityFromUrl" . | trim -}}
+{{- if and $auth (contains ":" $auth) -}}
+{{- index (splitList ":" $auth) 1 -}}
+{{- else -}}
+3306
+{{- end -}}
+{{- else -}}
+{{- include "gravitino.entityJdbcPortFromUrl" . | trim -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+Database name for schema-migrate Job.
+*/}}
+{{- define "gravitino.schemaMigrateDatabase" -}}
+{{- $db := .Values.schema.dbName | default "" | toString | trim -}}
+{{- if $db -}}
+{{- $db -}}
+{{- else if .Values.mysql.enabled -}}
+{{- .Values.mysql.auth.database -}}
+{{- else if .Values.postgresql.enabled -}}
+{{- .Values.postgresql.auth.database | default "gravitino" -}}
+{{- else if eq (include "gravitino.entityUsesExternalMysql" . | trim) "true"
-}}
+{{- $fromUrl := include "gravitino.entityJdbcMysqlDatabaseFromUrl" . | trim -}}
+{{- if $fromUrl -}}
+{{- $fromUrl -}}
+{{- else -}}
+{{- fail "could not parse database name from entity.jdbcUrl; use
jdbc:mysql://<host>:<port>/<database> or set schema.dbName" -}}
+{{- end -}}
+{{- else -}}
+{{- $fromUrl := include "gravitino.entityJdbcDatabaseFromUrl" . | trim -}}
+{{- if $fromUrl -}}
+{{- $fromUrl -}}
+{{- else -}}
+{{- fail "could not parse database name from entity.jdbcUrl; use
jdbc:postgresql://<host>:<port>/<database> or set schema.dbName" -}}
+{{- end -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+Password Secret for external relational stores (schema-migrate Job).
+*/}}
+{{- define "gravitino.schemaMigrateSecretName" -}}
+{{- if or .Values.mysql.enabled .Values.postgresql.enabled -}}
+{{- else if .Values.schema.migrate.existingSecret -}}
+{{- .Values.schema.migrate.existingSecret -}}
+{{- else -}}
+{{- fail "schema.migrate.existingSecret is required when
schema.migrate.enabled is true for external MySQL or PostgreSQL" -}}
+{{- end -}}
+{{- end -}}
+
+{{/*
+PostgreSQL currentSchema for schema-migrate Job (PG_CURRENT_SCHEMA /
PGOPTIONS).
+*/}}
+{{- define "gravitino.schemaMigrateCurrentSchema" -}}
+{{- $schema := "" -}}
+{{- if .Values.schema.currentSchema -}}
+{{- $schema = .Values.schema.currentSchema | toString | trim -}}
+{{- else if eq (include "gravitino.entityUsesExternalPostgresql" . | trim)
"true" -}}
+{{- $schema = include "gravitino.entityJdbcCurrentSchemaFromUrl" . | trim -}}
+{{- end -}}
+{{- if $schema -}}
+{{- if not (regexMatch "^[A-Za-z_][A-Za-z0-9_]*$" $schema) -}}
+{{- fail (printf "schema.currentSchema / entity.jdbcUrl currentSchema %q must
be a simple PostgreSQL identifier" $schema) -}}
+{{- end -}}
+{{- $schema -}}
Review Comment:
Fixed in the latest commit.
`gravitino.schemaMigrateCurrentSchema` now folds accepted identifiers to
lowercase (same as `gravitino.postgresqlInitCurrentSchema`), so
`currentSchema=App` yields `PG_CURRENT_SCHEMA=app` and `PGOPTIONS=-c
search_path=app`. Fresh-schema creation (`CREATE SCHEMA "…"`) and the
Deployment wait query therefore use the same name.
Added helm-unittest coverage for mixed-case `schema.currentSchema` and
jdbcUrl-derived `currentSchema`, asserting lowercase on the migrate Job and the
wait-for-schema initContainer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]