Skip to content

Introduce versioned SQL schema migrations - #1522

Open
enarha wants to merge 1 commit into
tektoncd:mainfrom
enarha:introduce-sql-migration-framework
Open

enarha wants to merge 1 commit into
tektoncd:mainfrom
enarha:introduce-sql-migration-framework

Conversation

@enarha

@enarha enarha commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Replace GORM AutoMigrate on API server startup with versioned SQL migrations managed by golang-migrate and applied by a dedicated results-admin command, run as a Kubernetes Job in each release.

Schema versioning:

  • Embed the migrations in pkg/api/server/db/migrations. 001_baseline creates the established results and records schema.
  • Record the schema version and dirty flag in schema_migrations.
  • Declare compatibility with a single RequiredSchemaVersion constant. A process starts when the effective schema version (the recorded version, or one less while it is dirty) is at or above it. Migrations are additive, so several releases can share a schema, rollback needs no downgrade, and a pod restarting during a long migration still starts.
  • The API server and retention policy agent run a read-only check at startup and exit with an actionable message when it fails. There is no structural validation at startup.
  • DB_SCHEMA_REQUIRED_VERSION_OVERRIDE deliberately replaces the required version, with a warning on every start.

results-admin:

  • New binary and image with status, check [--structural], migrate-up, migrate-to and force --repaired commands, using an advisory lock and bounded connect, lock and statement timeouts.
  • migrate-up adopts a database created by GORM AutoMigrate after verifying that its tables match the baseline exactly, and fails with the list of differences otherwise. Upgrading needs no manual step.
  • --wait-for-database retries while the database is not reachable yet and re-reads the configuration before each retry.

Deployment:

  • Add the tekton-results-db-migrate Job to the release. It waits up to 10 minutes for the database and is removed 6 hours after finishing.
  • Remove DB_ENABLE_AUTO_MIGRATION; it is ignored with a warning.
  • Publish the results-admin image and wait for the Job in the e2e install script.

Tests and CI:

  • Unit tests for the embedded migrations, compatibility rule, CLI and connection retries; PostgreSQL tests (build tag e2e) for migration, adoption, drift, dirty state, force and lock timeouts.
  • New db-migrations presubmit job runs the PostgreSQL tests and rejects changes to published migration files.

Docs: add docs/database-migrations.md and update the install, external database and API configuration docs.

Assisted-by: GitHub Copilot CLI (Claude Opus 5.5) noreply@anthropic.com

Changes

/kind feature

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you review them:

  • Has Docs included if any changes are user facing
  • Has Tests included if any functionality added or changed
  • Tested your changes locally (if this is a code change)
  • Follows the commit message standard
  • Meets the Tekton contributor standards (including functionality, content, code)
  • Has a kind label. You can add a comment on this PR that contains /kind <type>. Valid types are bug, cleanup, design, documentation, feature, flake, misc, question, tep
  • Release notes block below has been updated with any user-facing changes (API changes, bug fixes, changes requiring upgrade notices or deprecation warnings)
  • Release notes contain the string "action required" if the change requires additional action from users switching to the new release

Release Notes

TODO

@tekton-robot tekton-robot added kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Oct 26, 2026
@tekton-robot tekton-robot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Oct 26, 2026
@enarha
enarha force-pushed the introduce-sql-migration-framework branch from 2ea859e to ca873e4 Compare October 29, 2026 12:40
@enarha
enarha requested a max review from Copilot October 29, 2026 12:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical migration defects break custom-CA connectivity, rollback migrations, and legacy adoption, while incomplete validation can accept cross-schema foreign keys and disruptive triggers.

5 open issues
What changed in this PR

Replaces startup GORM AutoMigrate with embedded, versioned PostgreSQL migrations applied by the new results-admin CLI and a release-time Kubernetes Job. The API server and retention agent instead perform read-only schema compatibility checks.

Changes:

  • Adds baseline migrations, legacy-schema adoption, structural validation, locking, and recovery commands.
  • Integrates migration execution into deployment, image publishing, configuration, and installation.
  • Adds PostgreSQL migration coverage, migration-immutability checks in CI, and operational documentation.

This pull request requires changes. Unresolved findings affect custom-CA upgrades, rollback migration Jobs, legacy-schema adoption, and structural validation. Test safety, operational warnings, and documentation also need attention.

Vendored files are listed for completeness; third-party implementations were excluded from code review.

File Description
.github/​workflows/​presubmit-ci.yaml Adds PostgreSQL migration tests and migration-immutability checks.
AGENTS.md Documents schema administration and migration guidance.
cmd/​api/​README.md Documents the schema-version override.
cmd/​api/​main.go Checks schema compatibility before starting the API.
cmd/​results-admin/​main.go Implements database administration commands.
cmd/​results-admin/​main_test.go Tests CLI validation and connection-string escaping.
cmd/​retention-policy-agent/​main.go Checks schema compatibility before starting retention.
config/​base/​db-migrate.yaml Defines the release migration Job.
config/​base/​env/​config Replaces AutoMigrate configuration with the version override.
config/​base/​kustomization.yaml Includes the migration Job.
docs/​README.md Links database migration documentation.
docs/​database-migrations.md Documents installation, upgrades, administration, and recovery.
docs/​external-database.md Adds migration Job configuration and database-switch procedures.
docs/​install.md Explains migration behavior during installation.
go.mod Adds migration dependencies and refreshes indirect dependencies.
pkg/​api/​server/​config/​config.go Adds the version override and obsolete-setting warning.
pkg/​api/​server/​db/​migrations/​001_baseline.down.sql Defines guarded baseline removal.
pkg/​api/​server/​db/​migrations/​001_baseline.up.sql Defines the established PostgreSQL schema.
pkg/​api/​server/​db/​migrations/​README.md Documents migration development rules and testing.
pkg/​api/​server/​db/​migrations/​admin.go Implements retries, locking, adoption, migration, and repair operations.
pkg/​api/​server/​db/​migrations/​migrations.go Embeds migrations and implements schema compatibility checks.
pkg/​api/​server/​db/​migrations/​postgres_test.go Tests migration, adoption, drift, dirty-state, and locking behavior.
pkg/​api/​server/​db/​migrations/​structure.go Validates baseline structure for adoption and diagnostics.
pkg/​api/​server/​test/​db.go Centralizes SQLite test-schema creation.
pkg/​api/​server/​v1alpha2/​logs_test.go Removes obsolete AutoMigrate configuration from log tests.
pkg/​api/​server/​v1alpha2/​plugin/​plugin_logs_test.go Removes obsolete AutoMigrate configuration from plugin tests.
pkg/​api/​server/​v1alpha2/​records_test.go Uses explicitly initialized test databases.
pkg/​api/​server/​v1alpha2/​results_test.go Uses explicitly initialized test databases.
pkg/​api/​server/​v1alpha2/​server.go Removes application-startup AutoMigrate.
pkg/​internal/​test/​clients.go Removes obsolete AutoMigrate configuration from test clients.
tekton/​publish.yaml Publishes the results-admin image.
test/​e2e/​01-install.sh Waits for migration completion before application readiness.
test/​e2e/​blob-logs/​vector-minio-config.yaml Removes the obsolete AutoMigrate setting.
test/​e2e/​db/​README.md Marks deployed-schema verification as active.
test/​e2e/​db/​db_test.go Updates comments describing database test coverage.
test/​e2e/​db/​migrations_test.go Verifies deployed schema version and baseline structure.
test/​e2e/​db/​schema_test.go Attributes schema creation to release migrations.
test/​e2e/​gcs-emulator.yaml Removes the obsolete AutoMigrate setting.
test/​e2e/​loki_vector/​loki-vector-api-config.yaml Removes the obsolete AutoMigrate setting.
vendor/​modules.txt Updates the vendored dependency inventory.
vendor/​go.opentelemetry.io/​otel/​semconv/​v1.41.0/​rpcconv/​metric.go Refreshes vendored RPC metric conventions.
vendor/​go.opentelemetry.io/​otel/​semconv/​v1.40.0/​schema.go Removes the superseded schema declaration.
vendor/​go.opentelemetry.io/​otel/​semconv/​v1.40.0/​README.md Removes superseded convention documentation.
vendor/​go.opentelemetry.io/​otel/​semconv/​v1.40.0/​MIGRATION.md Removes superseded convention migration guidance.
vendor/​go.opentelemetry.io/​otel/​semconv/​v1.40.0/​exception.go Removes superseded exception conventions.
vendor/​go.opentelemetry.io/​otel/​semconv/​v1.40.0/​error_type.go Removes superseded error-type helpers.
vendor/​go.opentelemetry.io/​otel/​semconv/​v1.40.0/​doc.go Removes superseded package documentation.
vendor/​go.opentelemetry.io/​contrib/​instrumentation/​google.golang.org/​grpc/​otelgrpc/​version.go Updates the vendored instrumentation version.
vendor/​go.opentelemetry.io/​contrib/​instrumentation/​google.golang.org/​grpc/​otelgrpc/​stats_handler.go Refreshes vendored gRPC instrumentation.
vendor/​go.opentelemetry.io/​contrib/​instrumentation/​google.golang.org/​grpc/​otelgrpc/​internal/​parse.go Updates semantic-convention references.
vendor/​go.opentelemetry.io/​contrib/​instrumentation/​google.golang.org/​grpc/​otelgrpc/​interceptor.go Updates semantic-convention imports.
vendor/​go.opentelemetry.io/​contrib/​instrumentation/​google.golang.org/​grpc/​otelgrpc/​config.go Refreshes vendored instrumentation configuration.
vendor/​github.com/​jackc/​pgerrcode/​README.md Adds PostgreSQL error-code package documentation.
vendor/​github.com/​jackc/​pgerrcode/​LICENSE Adds dependency licensing.
vendor/​github.com/​jackc/​pgerrcode/​gen.rb Includes PostgreSQL error-code generation tooling.
vendor/​github.com/​golang-migrate/​migrate/​v4/​util.go Adds migration-library utilities.
vendor/​github.com/​golang-migrate/​migrate/​v4/​source/​parse.go Adds migration-filename parsing.
vendor/​github.com/​golang-migrate/​migrate/​v4/​source/​migration.go Adds migration-source indexing.
vendor/​github.com/​golang-migrate/​migrate/​v4/​source/​iofs/​README.md Adds embedded-filesystem driver documentation.
vendor/​github.com/​golang-migrate/​migrate/​v4/​source/​iofs/​iofs.go Adds embedded-filesystem migration loading.
vendor/​github.com/​golang-migrate/​migrate/​v4/​source/​iofs/​doc.go Adds filesystem-driver package documentation.
vendor/​github.com/​golang-migrate/​migrate/​v4/​source/​errors.go Adds migration-source errors.
vendor/​github.com/​golang-migrate/​migrate/​v4/​source/​driver.go Adds migration-source interfaces.
vendor/​github.com/​golang-migrate/​migrate/​v4/​SECURITY.md Includes upstream security guidance.
vendor/​github.com/​golang-migrate/​migrate/​v4/​README.md Includes migration-library documentation.
vendor/​github.com/​golang-migrate/​migrate/​v4/​MIGRATIONS.md Includes migration-format guidance.
vendor/​github.com/​golang-migrate/​migrate/​v4/​migration.go Adds migration representation and buffering.
vendor/​github.com/​golang-migrate/​migrate/​v4/​Makefile Includes upstream build tooling.
vendor/​github.com/​golang-migrate/​migrate/​v4/​log.go Adds the migration logging interface.
vendor/​github.com/​golang-migrate/​migrate/​v4/​LICENSE Adds migration-library licensing.
vendor/​github.com/​golang-migrate/​migrate/​v4/​internal/​url/​url.go Adds migration URL utilities.
vendor/​github.com/​golang-migrate/​migrate/​v4/​GETTING_STARTED.md Includes dependency setup guidance.
vendor/​github.com/​golang-migrate/​migrate/​v4/​FAQ.md Includes migration reference documentation.
vendor/​github.com/​golang-migrate/​migrate/​v4/​Dockerfile.github-actions Includes upstream Actions container packaging.
vendor/​github.com/​golang-migrate/​migrate/​v4/​Dockerfile.circleci Includes upstream CI container packaging.
vendor/​github.com/​golang-migrate/​migrate/​v4/​Dockerfile Includes upstream container packaging.
vendor/​github.com/​golang-migrate/​migrate/​v4/​docker-deploy.sh Includes upstream image-publishing tooling.
vendor/​github.com/​golang-migrate/​migrate/​v4/​database/​util.go Adds database-driver locking utilities.
vendor/​github.com/​golang-migrate/​migrate/​v4/​database/​pgx/​v5/​README.md Documents the PostgreSQL migration driver.
vendor/​github.com/​golang-migrate/​migrate/​v4/​database/​multistmt/​parse.go Adds SQL statement parsing.
vendor/​github.com/​golang-migrate/​migrate/​v4/​database/​error.go Adds migration database errors.
vendor/​github.com/​golang-migrate/​migrate/​v4/​database/​driver.go Adds migration database-driver interfaces.
vendor/​github.com/​golang-migrate/​migrate/​v4/​CONTRIBUTING.md Includes upstream contributor documentation.
vendor/​github.com/​golang-migrate/​migrate/​v4/​.goreleaser.yml Includes upstream release configuration.
vendor/​github.com/​golang-migrate/​migrate/​v4/​.golangci.yml Includes upstream lint configuration.
vendor/​github.com/​golang-migrate/​migrate/​v4/​.gitignore Includes upstream repository ignore rules.
vendor/​github.com/​golang-migrate/​migrate/​v4/​.dockerignore Includes upstream container ignore rules.
vendor/​github.com/​felixge/​httpsnoop/​wrap_generated_lt_1.8.go Removes an obsolete generated HTTP wrapper.
vendor/​github.com/​felixge/​httpsnoop/​wrap_generated_gteq_1.8.go Removes a superseded generated HTTP wrapper.
vendor/​github.com/​felixge/​httpsnoop/​capture_metrics.go Refreshes vendored HTTP metric capture.

Review effort: Max

Comment on lines +62 to +65
volumeMounts:
- name: config
mountPath: /etc/tekton/results
readOnly: true
Comment on lines +189 to +194
if s.Dirty {
return fmt.Errorf("schema version %d is dirty: a migration failed or is still running; repair it before migrating", s.Version)
}
if target != 0 && target < uint(s.Version) { //nolint:gosec // migration versions are small
return fmt.Errorf("target version %d is below current version %d; downgrades are not supported", target, s.Version)
}
CASE k.confdeltype WHEN 'c' THEN 'CASCADE' ELSE k.confdeltype::text END)
FROM pg_constraint k
JOIN pg_class c ON c.oid = k.conrelid
LEFT JOIN pg_class rc ON rc.oid = k.confrelid
Comment on lines +223 to +227
if kind == "f" && definition == baselineForeignKey && validated && !foreignKey {
foreignKey = true
return nil
}
diff("constraint %s on %s: unexpected (type %s, validated %t)", name, table, kind, validated)
diff("missing foreign key %s", baselineForeignKey)
}

if len(diffs) == 0 {
@tekton-robot tekton-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 12, 2026

@olegbet olegbet left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve
/lgtm

@tekton-robot

Copy link
Copy Markdown
Contributor

@olegbet: changing LGTM is restricted to collaborators

Details

In response to this:

/approve
/lgtm

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@tekton-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: olegbet
To complete the pull request process, please ask for approval from enarha after the PR has been reviewed.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Replace GORM AutoMigrate on API server startup with versioned SQL
migrations managed by golang-migrate and applied by a dedicated
results-admin command, run as a Kubernetes Job in each release.

Schema versioning:
- Embed the migrations in pkg/api/server/db/migrations. 001_baseline
  creates the established results and records schema.
- Record the schema version and dirty flag in schema_migrations.
- Declare compatibility with a single RequiredSchemaVersion constant.
  A process starts when the effective schema version (the recorded
  version, or one less while it is dirty) is at or above it. Migrations
  are additive, so several releases can share a schema, rollback needs
  no downgrade, and a pod restarting during a long migration still
  starts.
- The API server and retention policy agent run a read-only check at
  startup and exit with an actionable message when it fails. There is
  no structural validation at startup.
- DB_SCHEMA_REQUIRED_VERSION_OVERRIDE deliberately replaces the
  required version, with a warning on every start.

results-admin:
- New binary and image with status, check [--structural], migrate-up,
  migrate-to and force --repaired commands, using an advisory lock and
  bounded connect, lock and statement timeouts.
- migrate-up adopts a database created by GORM AutoMigrate after
  verifying that its tables match the baseline exactly, and fails with
  the list of differences otherwise. Upgrading needs no manual step.
- --wait-for-database retries while the database is not reachable yet
  and re-reads the configuration before each retry.

Deployment:
- Add the tekton-results-db-migrate Job to the release. It waits up to
  10 minutes for the database and is removed 6 hours after finishing.
- Remove DB_ENABLE_AUTO_MIGRATION; it is ignored with a warning.
- Publish the results-admin image and wait for the Job in the e2e
  install script.

Tests and CI:
- Unit tests for the embedded migrations, compatibility rule, CLI and
  connection retries; PostgreSQL tests (build tag e2e) for migration,
  adoption, drift, dirty state, force and lock timeouts.
- New db-migrations presubmit job runs the PostgreSQL tests and rejects
  changes to published migration files.

Docs: add docs/database-migrations.md and update the install, external
database and API configuration docs.

Signed-off-by: Emil Natan <ena@redhat.com>
Assisted-by: GitHub Copilot CLI (Claude Opus 5.5) <noreply@anthropic.com>
@enarha
enarha force-pushed the introduce-sql-migration-framework branch from ca873e4 to d943aad Compare November 29, 2026 11:14
@tekton-robot tekton-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants