Skip to content

[Server] Check schema migration steps on every pull request - #2046

Closed
shba24 wants to merge 182 commits into
unitycatalog:mainfrom
shba24:stack/uc-flyway-merge-checks
Closed

shba24 wants to merge 182 commits into
unitycatalog:mainfrom
shba24:stack/uc-flyway-merge-checks

Conversation

@shba24

@shba24 shba24 commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

🥞 Stacked PR

Use this link to review incremental changes.


PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • If you've added or modified a feature, documentation in docs is updated

Description of changes

Reviewing: this PR's own changes are shba24/unitycatalog@stack/uc-flyway-schema...stack/uc-flyway-merge-checks. GitHub's Files changed tab also shows #2009, #2044, #2045, which this PR builds on, until they merge.

Pull requests that change the schema the wrong way now fail CI with a message saying what to do.

#2045 makes Flyway steps the only way the schema changes. This PR adds the checks that keep those steps trustworthy:

  • Merged steps never change. A new CI job, migration-steps, runs dev/check-migration-steps.sh against the pull request's base and fails on an edited, deleted or renamed step under server/src/main/resources/db/migration/.
  • Patch releases don't change the schema. The same job fails a pull request into a release branch (branch-*) that adds a step. Together with BaselineMatchesEntitiesTest (an entity change without a step fails), a patch release keeps exactly the schema of its .0 release. This applies from branch-0.8 on; branch-0.7 has no steps.
  • One allowed change per SQL step. MigrationStepRulesTest fails on a step with two statements, or whose statement isn't on an allow-list: create a table, create an index that is not unique, add a column (NOT NULL only with a DEFAULT, without a key or CHECK), widen a column's type, or drop its NOT NULL. Everything else is refused until a release needs it: data changes, unique indexes and rules that existing rows can break, and drops and renames that servers of the previous release still use. The test reads the statement, not the column, so review checks that a type change widens the column. Outside '...' strings and -- comments a step may use only letters, digits, _, spaces and ( ) , . = < > -; a backslash in a string, -- without a space after it, a carriage return without a line feed, and CREATE INDEX CONCURRENTLY are refused too. Other quoting and comments (", backticks, MySQL #, H2 //, block comments, $$ strings) could hide a second statement from the test. One statement keeps a step all-or-nothing on MySQL. Only V1__baseline_0_7.sql, the 0.7.0 schema, is exempt.
  • CHECK constraints are compared. BaselineMatchesEntitiesTest compares the schema the steps build with the one Hibernate builds from the entities; it now also compares CHECK constraints. Adding an enum value (say a 23rd column type) changes the entities' check (type_name between 0 and 22); without this, every database would keep 0 and 21 and writing the new value would fail at runtime.
  • The sample database stays current. SampleDatabaseIsCurrentTest checks that etc/db/h2db.mv.db is on the build's latest schema version. When it is behind, the test migrates the file in place and fails asking you to commit it, so there's nothing to do by hand. It also compares the file's whole schema with what the steps build on an empty H2 database. That found two old differences in the shipped file, now fixed: its function type rules allowed only codes 0 to 20, so a function returning a table (code 21) couldn't be stored, and name was nullable on catalogs, schemas, tables and volumes. The file's data and schema history are unchanged.
  • AGENTS.md lists how to add a schema step.

TODO for maintainers: make the Merged migration steps unchanged job (and the unit tests) a required status check on main and branch-0.8 onward. Until then a failing run doesn't block a merge. Not on branch-0.7: it doesn't have the job.

Testing

  • Each check fails when its rule is broken (scratch changes, since removed):
    • a two-statement step: h2/V3__scratch.sql has 2 statements;
    • a second statement hidden behind quoting (DEFAULT 'it\'s' on MySQL, a "it's" column on PostgreSQL, each followed by DROP TABLE uc_tables; -- '): passed before, now refused; the same for a MySQL # comment, an H2 // comment, a nested block comment, a $$ string, a lone carriage return, MySQL --1 and CREATE INDEX CONCURRENTLY ON t (a);
    • a CHECK constraint in V1 edited from 0 and 21 to 0 and 20: BaselineMatchesEntitiesTest fails, listing check uc_function_params "TYPE_NAME" BETWEEN 0 AND 20 against the entities' 21;
    • a new step with the sample database left behind: etc/db/h2db.mv.db is PENDING at version 2, this build at 3; the test migrated it, commit etc/db/h2db.mv.db. The next run passes and leaves the file unchanged.
  • dev/check-migration-steps.sh on throwaway commits:
    == edit V2:
    These merged migration steps changed since HEAD~1:
    M	server/src/main/resources/db/migration/h2/V2__widen_function_param_type_json.sql
    Merged steps can't change; add a new step under server/src/main/resources/db/migration/<db>/ instead.
    exit=1
    == new step, base main:
    No merged migration step changed since HEAD~1.
    exit=0
    == new step, base branch-0.8:
    These migration steps are new on the release branch branch-0.8:
    server/src/main/resources/db/migration/h2/V3__scratch.sql
    Release branches take no new steps; add the step on main.
    exit=1
    
    It gives the same result when run from a subdirectory.
  • migrate.* tests pass on H2, PostgreSQL 18 and MySQL 8.4. Locally, build/sbt test passes on H2 except SdkModelCRUDTest.testModelCRUD, an existing flaky test (two updates in the same millisecond) that passes on its own; server/test passes on PostgreSQL 18 (903 tests) and MySQL 8.4 (902 tests); Javadoc builds.
📐 Design notes & details

Why "merged" and not "released". A step that reached main may already have run on a database built from main. Freezing steps at merge is stricter than freezing them at release, and needs no release bookkeeping. Flyway itself only notices an edited step when a database that ran it starts, which is after the release.

Why our own statement splitter. The steps are written in three dialects (H2, PostgreSQL, MySQL). The check only needs to count statements and match each one against the allow-list: it strips comments and splits on ; outside '...' strings, and refuses every character it doesn't need, so other quoting and comments can't hide a ;. A file with only comments passes. Flyway's own parser would do it, but it is internal API.

Later baselines. If the list of steps gets long, a later release can add a Flyway baseline migration (B<version>__…sql, the whole schema at that version) next to the V steps; Flyway runs it only on a new database. The test reads only V files, so it needs no change for that.

How CHECK constraints are compared. By clause, without their generated names (a schema the steps built and one Hibernate built name them differently), and keeping their case, because the enum literals in them are case-sensitive.

Moved out of this PR. The recorded schema dumps (schema/<db>.txt, SchemaDumpTest) move to #2050, the PR whose server code reads them. A check that new steps are numbered above the base's was dropped: Flyway already refuses two steps with the same version.

This pull request and its description were written by Isaac.

@shba24 shba24 changed the title [Server] Check the migrated schema against a recorded dump per database [Server] Check schema migration steps on every pull request Oct 24, 2026
@shba24
shba24 force-pushed the stack/uc-flyway-merge-checks branch 5 times, most recently from dd7a8ed to e45f944 Compare October 24, 2026 23:42
shba24 and others added 20 commits October 25, 2026 01:53
Adds MySQL 8.4 to the test database harness (testMySQL8) and a CI job. Test
databases use the case-sensitive utf8mb4_0900_bin collation. The isolation
test now reads the database's default level instead of assuming
READ_COMMITTED.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Without an ORDER BY, MySQL returned columns (and function parameters) in
its own order.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Storage paths and names are case-sensitive, but MySQL's default collation is
not. Log a warning at startup and document the utf8mb4_0900_bin collation.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
MySQL and PostgreSQL now share one harness: Hibernate builds the schema once
per JVM into a template and each test clones it. Both containers keep their
data in memory with durability off.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Fix harness comments and docs, assign the cached database only after it
started, drop duplicate test dependencies, request a different isolation
level in the restore test, check the MySQL template has every mapped
table, and cover parameter order in the function CRUD test.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
The MYSQL_8 CI row runs SdkDeltaCommitsCRUDTest on MySQL, so the class that started its own MySQL container is no longer needed. Its lock-reading test moves into SdkDeltaCommitsCRUDTest and runs on every database.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
A @TempDatabase field gets a fresh TestDatabase, per test for an instance field and per class for a static one. The extension stores it in the JUnit store, which closes it after every @AfterEach, so a test's server always closes before its database is dropped, and a failing setUp still drops it.

TestDatabase no longer takes a ServerProperties (it overwrote everything the properties set) and offers hibernateProperties(), hibernateConfigurator() and sessionFactory(). All tests that made their own database now use the annotation.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…eSQL containers

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…lt_on_error

The isolation test asks for a level other than the default, because asking for
the default would pass even if nothing were restored. The MySQL template keeps
halt_on_error off only for the one index InnoDB rejects; a TODO removes it once
the schema migration steps build the template.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Adds Flyway 13.10.3, the V1 baseline of the 0.7 schema and V2 (type_json
widening) for H2, PostgreSQL and MySQL; empty test databases,
@TempDatabase and TestDatabase.dataSource()/kind() in the harness; and a
schema snapshot check that the steps build the schema the entities
expect.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Adds Migrator, which classifies a database (empty, current, pending,
newer than this release, an unversioned 0.7 database, not a Unity Catalog
database, failed step) and, under a run lock, sets up an empty database,
adopts a 0.7 one (version 1 recorded as the baseline) and applies the
pending steps. The run lock is pg_advisory_lock on PostgreSQL, GET_LOCK on
MySQL and an in-process lock per database file on H2. bin/uc-migrate runs
`migrate` and `info` against the database in etc/conf/hibernate.properties.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…ases with Flyway

The server sets up an empty database itself (a second server starting at the
same time waits on the run lock) and refuses any other schema version with a
message that names the fix. Hibernate never alters the schema: hbm2ddl.auto is
forced to none, even if an old hibernate.properties says update.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…ases

Adds V3. On MySQL it converts the database and every Unity Catalog table
(and casbin_rule, when it exists) to utf8mb4_0900_bin, so a database
created with MySQL's case-insensitive default compares names and storage
paths like PostgreSQL and H2. Converting is safe to repeat, so the step
holds several statements. On PostgreSQL and H2, V3 does nothing.

The startup collation warning skips Flyway's history table, which keeps
the database's original collation. Test-only steps move to version 1000
and tests read the latest version instead of hard-coding it.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Every test database now gets its schema from Migrator, never from
Hibernate DDL: the PostgreSQL and MySQL templates are migrated once, and
each H2 test database is migrated when it is created. Hibernate runs
with hbm2ddl.auto=none in every test.

MySQL clones now also copy the template's rows (Flyway's history), as
PostgreSQL's TEMPLATE does, so a clone is at the current version. The
MySQL template no longer needs halt_on_error=false or the check that
every mapped table exists: Flyway stops on any failed statement.
PopulateTestDatabase migrates its database before opening it.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Describe how Flyway manages the schema, how to upgrade without Helm with
bin/uc-migrate, the first start on an empty database, the MySQL
case-sensitivity change, going back to 0.7, and each error message with what
to do. Replace the outdated Hibernate auto-update paragraph in the deployment
guide with a link to the guide.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
- The server and bin/uc-migrate print the same refusal for a foreign,
  newer or failed schema (Migrator.refusal); "Stop the servers and run
  bin/uc-migrate migrate" is kept for a 0.7 or pending database.
- bin/uc-migrate runs from the repository or tarball root, so it reads
  etc/conf/hibernate.properties from any directory; it exits 1 when it
  finds neither, and builds a missing classpath file.
- The MySQL run lock is per database, not per MySQL server.
- Flyway fails on missing step locations; a jar without steps says so.
- uc-migrate reuses the server's Hikari settings.
- The shaded server jar keeps META-INF/services (merged), so Flyway
  finds its database support in connector tests.
- The Helm chart no longer sets hibernate.hbm2ddl.auto=update.
- Tests: release-to-release upgrade, casbin_rule conversion on MySQL,
  server properties always turn Hibernate schema updates off.
- Docs: messages say who prints them; going back to 0.7 needs no
  restore; waiting applies to PostgreSQL and MySQL.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…6 via 0.7

RealUpgradeTest runs real server images on each UpgradePath and test
database: the first image writes a data set through the API with
authorization on, later images just start, then the current server
refuses the database until Migrator.migrate() upgrades it to the fresh
install schema, and the data reads back.

The test kit gains AcrossUpgradePaths, UpgradePath (BASE built from
UC_TEST_BASE_REF by dev/build-image.sh, LATEST, V0_6_VIA_0_7), OldImage,
OldServer and DataSet. The tests are tagged "upgrades": the default test
and coverage runs leave them out, and testUpgrades,
testUpgradesPostgres18 and testUpgradesMySQL8 run them, also in a new CI
job.

Real 0.7 databases showed that Hibernate's auto-update names the unique
constraints of uc_columns, uc_delta_commits and uc_properties, so the H2
and PostgreSQL baselines now name them the same way, and the baseline
check builds its reference schema with "update", as 0.7 installs did.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
shba24 and others added 7 commits November 4, 2026 00:18
…ySQL

Hibernate already defaults halt_on_error to false, so the old MySQL-only
setting did nothing. Set it always: true on H2 and PostgreSQL, false on MySQL,
where one entity index is too long.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
The H2 run lock holds only within one JVM, so with AUTO_SERVER=TRUE or a
jdbc:h2:tcp:/jdbc:h2:ssl: URL two servers could run steps at once. The
migrator now refuses those URLs before opening its pool. It reads the
configured URL, since H2's connection URL leaves AUTO_SERVER out.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…atabase

- The H2 server-mode refusal read only hibernate.connection.url and only
  AUTO_SERVER=TRUE. It now also reads the data-source URL of a pool set
  up by data source class and an AUTO_SERVER driver property, accepts
  every value H2 reads as true (1, t, y, yes, true), and finds
  :h2:tcp:/:h2:ssl: behind a wrapping driver's prefix.
- The test rows open their databases under a temp directory.
- Remove uc.mv.db, which an earlier run of that test wrote into the
  checkout.
- upgrades.md: name only Group Replication for the unshared named lock.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>

# Conflicts:
#	server/src/test/java/io/unitycatalog/server/migrate/SchemaSnapshot.java
@shba24
shba24 requested a review from timothyw553 as a code owner November 4, 2026 06:33
@shba24

shba24 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

/review

shba24 and others added 3 commits November 4, 2026 07:26
Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
KeyMapperTest (added on main in unitycatalog#1939) opened its own in-memory H2 database and
relied on Hibernate creating the tables. The server no longer lets Hibernate
change the schema, so the test found no tables. It now uses the shared test
database, which the schema steps build, like the other tests that write
entities directly.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…apperTest fix into stack/uc-flyway-merge-checks

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
shba24 added a commit to shba24/unitycatalog that referenced this pull request Nov 4, 2026
…d KeyMapperTest fix into stack/uc-hibernate-mode

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
@shba24

shba24 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

/review

shba24 and others added 3 commits November 4, 2026 10:54
…e database

DatabaseMetaData.getTables and getColumns take the schema (and getColumns the
table) as a search pattern, where _ and % match any character. With PostgreSQL
currentSchema=uc_a, the tables of a schema named ucxa were listed too, so an
empty uc_a looked like another application's database and the server refused
to start. The migrator and the tests' schema snapshot now escape the names.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
The server ignores the setting now that the schema steps own the schema, and
warned about it on every start of a Helm install. Also treat NONE like none,
as the other schema-action checks do, so it logs no warning.

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
…and the Helm setting into stack/uc-flyway-merge-checks

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
shba24 added a commit to shba24/unitycatalog that referenced this pull request Nov 4, 2026
…names and the Helm setting into stack/uc-hibernate-mode

Co-authored-by: Isaac <[email protected]>
Signed-off-by: Shubham Bansal <[email protected]>
@shba24

shba24 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

/review

1 similar comment
@shba24

shba24 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

/review

@shba24

shba24 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

Replaced by #2113. The schema upgrade work is now a stack of 8 PRs, each adding one feature: #2045, #2112, #2113, #2114, #2115, #2116, #2117, #2118. The merge checks from this PR are in #2113.

@shba24 shba24 closed this Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants