jerryshao commented on code in PR #15001:
URL: https://github.com/apache/gravitino/pull/15001#discussion_r4596277682


##########
dev/charts/gravitino/resources/scripts/schema-migrate.sh:
##########
@@ -0,0 +1,112 @@
+#!/usr/bin/env bash
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+#
+
+# Entity-store schema init for the Helm schema-migrate hook Job.
+set -euo pipefail
+
+: "${DB_ENGINE:?DB_ENGINE must be mysql or postgresql}"
+: "${DB_HOST:?DB_HOST is required}"
+: "${GRAVITINO_DB:?GRAVITINO_DB is required}"
+: "${SCRIPTS_DIR:=/scripts}"
+
+if [ "$DB_ENGINE" != mysql ] && [ "$DB_ENGINE" != postgresql ]; then
+  echo "ERROR: DB_ENGINE must be mysql or postgresql (got ${DB_ENGINE})" >&2
+  exit 1
+fi
+
+DB_PORT="${DB_PORT:-}"
+PG_CURRENT_SCHEMA="${PG_CURRENT_SCHEMA:-}"
+
+wait_for_db() {
+  echo "Waiting for ${DB_ENGINE} at ${DB_HOST}..."
+  if [ "$DB_ENGINE" = mysql ]; then
+    until mysql -h "$DB_HOST" "${mysql_extra[@]}" -u "$MYSQL_USER" -e "SELECT 
1" >/dev/null 2>&1; do
+      echo "Waiting for MySQL to be ready..."
+      sleep 2
+    done
+  else
+    until PGPASSWORD="$POSTGRES_PASSWORD" psql -h "$DB_HOST" -U 
"$POSTGRES_USER" -d "$GRAVITINO_DB" -c "SELECT 1" >/dev/null 2>&1; do
+      echo "Waiting for PostgreSQL to be ready..."
+      sleep 2
+    done
+  fi
+}
+
+mysql_extra=()
+if [ -n "$DB_PORT" ]; then
+  mysql_extra+=(-P "$DB_PORT")
+fi
+
+apply_migrations() {
+  local version dir schema_file upgrade_file
+  version=$(cat "${SCRIPTS_DIR}/version.txt")
+  if [ -z "$version" ]; then
+    echo "ERROR: version.txt is empty" >&2
+    exit 1
+  fi
+  dir="${SCRIPTS_DIR}/${DB_ENGINE}"
+  schema_file=$(ls "$dir"/schema-*-"${DB_ENGINE}".sql | sort -V | awk -F'-' -v 
ver="$version" '$2 <= ver {print $0}' | tail -n 1)
+  if [ ! -f "$schema_file" ]; then
+    echo "ERROR: Failed to find schema file under $dir for version $version" 
>&2
+    exit 1
+  fi
+  upgrade_file=$(ls "$dir"/upgrade-*-to-*-"${DB_ENGINE}".sql | sort -V | awk 
-F'-' -v ver="$version" '$4 <= ver {print $0}' | tail -n 1)

Review Comment:
   [P1] For an existing external MySQL database at schema 1.2.0 and a 2.0.0 
image, tail -n 1 runs only the 1.3.0-to-2.0.0 upgrade. The missing 
1.2.0-to-1.3.0 changes are not repaired by CREATE TABLE IF NOT EXISTS; for 
example, view_meta remains without audit_info. Please determine the starting 
schema and apply the required upgrade chain, or explicitly reject unsupported 
starting versions before reporting success. Add a regression test using an 
existing older schema and verify the resulting columns.



##########
dev/charts/gravitino/templates/schema-migrate-job.yaml:
##########
@@ -0,0 +1,189 @@
+{{- /*
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+  */}}
+
+{{- if eq (include "gravitino.schemaMigrateEnabled" . | trim) "true" }}
+{{- $_ := include "gravitino.schemaMigrateSslValidate" . }}
+{{- $engine := include "gravitino.schemaMigrateEngine" . | trim }}
+{{- $dbHost := include "gravitino.schemaMigrateHost" . | trim }}
+{{- $dbPort := include "gravitino.schemaMigratePort" . | trim }}
+{{- $dbName := include "gravitino.schemaMigrateDatabase" . | trim }}
+{{- $pgSchema := include "gravitino.schemaMigrateCurrentSchema" . | trim }}
+{{- $pgSslMode := include "gravitino.schemaMigrateSslMode" . | trim }}
+{{- $sslCaSecret := include "gravitino.schemaMigrateSslCaSecretName" . | trim 
}}
+{{- $hookWeight := .Values.schema.migrate.hookWeight | default "1" | toString 
}}
+apiVersion: batch/v1
+kind: Job
+metadata:
+  name: {{ printf "%s-schema-migrate-r%d" .Release.Name .Release.Revision | 
trunc 63 | trimSuffix "-" }}
+  namespace: {{ include "gravitino.namespace" . }}
+  labels:
+    {{- include "gravitino.labels" . | nindent 4 }}
+    app.kubernetes.io/component: schema-migrate
+  annotations:
+    helm.sh/hook: pre-install,pre-upgrade

Review Comment:
   [P1] With schema.migrate.enabled=true, this pre-install Job mounts a 
ConfigMap that Helm only creates after the hook completes. Upgrading from the 
previous chart has the same problem because its ConfigMap does not contain 
schema-migrate.sh yet. Bundled database installations also need their database 
and credentials created before migration can run. Please provide the script 
through an earlier dedicated hook resource or inline it, and arrange 
installation ordering so bundled databases are available before migration while 
application startup remains gated. Add fresh-install and first-upgrade 
lifecycle coverage; --wait cannot resolve this dependency cycle.



-- 
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]

Reply via email to