danhuawang commented on code in PR #15001: URL: https://github.com/apache/gravitino/pull/15001#discussion_r4627171719
########## dev/ci/test_schema_migrate.py: ########## @@ -0,0 +1,473 @@ +#!/usr/bin/env python3 +# +# 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. +# + +"""Regression tests for chart schema-migrate.sh (upgrade chain + fresh install).""" + +import argparse +import os +from pathlib import Path +import shutil +import subprocess +import time +import unittest +import uuid + + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "charts/gravitino/resources/scripts/schema-migrate.sh" +SQL_ROOT = ROOT.parent / "scripts" +ENGINE = "mysql" +TARGET = "2.0.0" + + +def docker(*args, check=True): + result = subprocess.run( + ["docker", *args], text=True, capture_output=True, timeout=180 + ) + if check and result.returncode: + raise RuntimeError(f"docker {args[0]} failed: {result.stderr or result.stdout}") + return result + + +class TestSchemaMigrate(unittest.TestCase): + @classmethod + def setUpClass(cls): + if not SCRIPT.is_file(): + raise unittest.SkipTest(f"missing {SCRIPT}") + sql_dir = SQL_ROOT / ENGINE + if not sql_dir.is_dir(): + raise unittest.SkipTest(f"missing {sql_dir}") + + workspace = Path(os.environ.get("GITHUB_WORKSPACE") or ROOT.parent).resolve() + cls.work = workspace / f".schema-migrate-test-{uuid.uuid4().hex[:12]}" + cls.work.mkdir(parents=True) + shutil.copyfile(SCRIPT, cls.work / "schema-migrate.sh") + shutil.copytree(sql_dir, cls.work / ENGINE) + cls.container = "schema-migrate-" + uuid.uuid4().hex[:12] + cls.addClassCleanup(shutil.rmtree, cls.work, True) + cls.addClassCleanup(docker, "rm", "-f", "-v", cls.container, check=False) + + image = "mysql:8.0" if ENGINE == "mysql" else "postgres:13" + settings = ( + [ + "-e", + "MYSQL_ROOT_PASSWORD=runtime-test-password", + "-e", + "MYSQL_INITDB_SKIP_TZINFO=1", + ] + if ENGINE == "mysql" + else [ + "-e", + "POSTGRES_HOST_AUTH_METHOD=trust", + "-e", + "POSTGRES_INITDB_ARGS=--no-sync", + ] + ) + docker( + "run", + "-d", + "--network", + "none", + "--tmpfs", + ( + "/var/lib/mysql:rw,size=512m" + if ENGINE == "mysql" + else "/var/lib/postgresql/data:rw,size=256m" + ), + "--name", + cls.container, + "-v", + f"{cls.work}:/test:ro", + *settings, + image, + ) + deadline = time.monotonic() + 90 + while time.monotonic() < deadline: + if ( + cls.sql( + "mysql" if ENGINE == "mysql" else "postgres", + "SELECT 1", + check=False, + ).returncode + == 0 + ): + return + time.sleep(1) + raise RuntimeError(docker("logs", cls.container).stdout) + + @classmethod + def sql(cls, database, sql, check=True): + if ENGINE == "postgresql": + client = [ + "psql", + "-U", + "postgres", + "-d", + database, + "-v", + "ON_ERROR_STOP=1", + "-Atc", + sql, + ] + else: + client = ["mysql", "-uroot", "-D", database, "-sNe", sql] + return docker( + "exec", + "-e", + "MYSQL_PWD=runtime-test-password", + cls.container, + *client, + check=check, + ) + + def setUp(self): + self.database = "migrate_" + uuid.uuid4().hex[:12] + self.sql( + "mysql" if ENGINE == "mysql" else "postgres", + f"CREATE DATABASE {self.database}", + ) + self.scripts = self.work / self.database + self.scripts.mkdir(parents=True) + # Bind-mounted work is read-only in the container; copy scripts per DB. + # The migrate helper writes version.txt next to the engine SQL tree. + shutil.copytree(self.work / ENGINE, self.scripts / ENGINE) + + def write_version(self, target=TARGET): + (self.scripts / "version.txt").write_text(target + "\n") + + def migrate(self, target=TARGET, baseline="", check=True): + self.write_version(target) + env = [ + "-e", + f"DB_ENGINE={ENGINE}", + "-e", + "DB_HOST=127.0.0.1", + "-e", + f"GRAVITINO_DB={self.database}", + "-e", + f"SCRIPTS_DIR=/test/{self.database}", + "-e", + "MYSQL_USER=root", + "-e", + "MYSQL_PWD=runtime-test-password", + "-e", + "POSTGRES_USER=postgres", + "-e", + "POSTGRES_PASSWORD=runtime-test-password", + "-e", + f"BASELINE_VERSION={baseline}", + ] + # work dir is mounted read-only; copy scripts into a writable path first. + docker( + "exec", + self.container, + "bash", + "-c", + f"rm -rf /tmp/{self.database} && cp -a /test/{self.database} /tmp/{self.database}", + ) + result = docker( + "exec", + *env, + "-e", + f"SCRIPTS_DIR=/tmp/{self.database}", + self.container, + "bash", + "/test/schema-migrate.sh", + check=check, + ) + return result + + def seed_schema_file(self, version="1.2.0"): + schema = self.scripts / ENGINE / f"schema-{version}-{ENGINE}.sql" + self.assertTrue(schema.is_file(), f"missing {schema}") + docker( + "exec", + "-e", + "MYSQL_PWD=runtime-test-password", + self.container, + "bash", + "-c", + f"mysql -uroot -D {self.database} < /test/{self.database}/{ENGINE}/schema-{version}-{ENGINE}.sql", + ) + + def assert_version(self, version): + self.assertEqual( + self.sql( + self.database, + "SELECT schema_version FROM gravitino_schema_migration WHERE id=1", + ).stdout.strip(), + version, + ) + + def assert_view_meta_has_audit_info(self): + scope = "DATABASE()" if ENGINE == "mysql" else "current_schema()" + self.assertEqual( + self.sql( + self.database, + "SELECT COUNT(*) FROM information_schema.columns " + f"WHERE table_schema={scope} AND table_name='view_meta' " + "AND column_name='audit_info'", + ).stdout.strip(), + "1", + ) + + def assert_view_meta_missing_audit_info(self): + scope = "DATABASE()" if ENGINE == "mysql" else "current_schema()" + self.assertEqual( + self.sql( + self.database, + "SELECT COUNT(*) FROM information_schema.columns " + f"WHERE table_schema={scope} AND table_name='view_meta' " + "AND column_name='audit_info'", + ).stdout.strip(), + "0", + ) + + def test_fresh_install_records_target_version(self): + result = self.migrate() + self.assertIn("Fresh install detected", result.stdout) + self.assert_version(TARGET) + self.assert_view_meta_has_audit_info() + + def test_existing_1_2_0_applies_full_upgrade_chain(self): + self.seed_schema_file("1.2.0") + self.assert_view_meta_missing_audit_info() + result = self.migrate(baseline="1.2.0") + self.assertIn("upgrade-1.2.0-to-1.3.0", result.stdout) + self.assertIn("upgrade-1.3.0-to-2.0.0", result.stdout) + self.assertNotIn("tail -n 1", result.stdout) + self.assert_version(TARGET) + self.assert_view_meta_has_audit_info() + + def test_tail_n_1_would_skip_intermediate_is_rejected_by_chain(self): + """If only the last upgrade ran, view_meta.audit_info would stay missing.""" + self.seed_schema_file("1.2.0") + # Apply only the last upgrade (the old buggy chart behavior). + docker( + "exec", + "-e", + "MYSQL_PWD=runtime-test-password", + self.container, + "bash", + "-c", + f"mysql -uroot -D {self.database} < /test/{self.database}/{ENGINE}/upgrade-1.3.0-to-2.0.0-{ENGINE}.sql || true", + ) + self.assert_view_meta_missing_audit_info() + result = self.migrate(baseline="1.2.0") + self.assertIn("upgrade-1.2.0-to-1.3.0", result.stdout) + self.assert_view_meta_has_audit_info() + self.assert_version(TARGET) + + def test_infer_1_3_0_keeps_version_capture_clean(self): + """Ambiguous 1.3.0/2.0.0 tables: infer stdout must be only the version. + + The INFO diagnostic must go to stderr so inferred=$(infer_schema_version) + cannot exceed gravitino_schema_migration.schema_version VARCHAR(64). + """ + self.seed_schema_file("1.3.0") + result = self.migrate(baseline="") + self.assertIn("inferred 1.3.0 from existing tables", result.stdout) + self.assertIn("upgrade-1.3.0-to-2.0.0", result.stdout) + self.assertNotIn("INFO: Tables also match schema-2.0.0", result.stdout) + self.assertIn("INFO: Tables also match schema-2.0.0", result.stderr) Review Comment: Fixed in the latest commit. Kept this case focused on ordinary 1.3.0 inference without `baselineVersion`: a real `schema-1.3.0.sql` seed lacks newer snapshot tables, so `infer_schema_version` never reaches the ambiguous-column INFO branch. Removed the stderr INFO assertion; the test still checks inference + upgrade + a clean `X.Y.Z` recorded version, and that diagnostic text does not leak into stdout. -- 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]
