Skip to content

feat(engine): create and upgrade temporal's sql schema on start - #6

Closed
hasithaa wants to merge 35 commits into
wso2:mainfrom
hasithaa:schema-on-start
Closed

hasithaa wants to merge 35 commits into
wso2:mainfrom
hasithaa:schema-on-start

Conversation

@hasithaa

@hasithaa hasithaa commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

Resolves wso2/product-integrator#2911

Stacked on #5 (which is stacked on #4). Review the commits after f735a1d.

On PostgreSQL and MySQL the engine started only after someone created Temporal's two databases and installed their schemas with workflow-engine-sql, six commands per installation, and again after each upgrade.

Goals

  • workflow-engine start --runtime-config creates the workflow and visibility databases when missing, sets up schema versioning in an empty database, and applies the schema versions shipped in the binary, before Temporal's services start.
  • Nodes started together take turns.
  • [database] manageSchema = false leaves schema changes to a DBA.

Approach

  • engine/internal/sqlschema uses Temporal's own schema tool packages (tools/sql, tools/common/schema) in process, with the schema files embedded in the binary.
  • One connection to the server's default database holds a server-wide lock (pg_advisory_lock, MySQL GET_LOCK) and creates the databases; without the lock, three nodes starting together failed with duplicate keys every time (tested).
  • A user without the right to create databases still works with databases a DBA created.
  • With schema management on, database names are restricted to what MySQL's unquoted CREATE DATABASE accepts.
  • The suite runner no longer runs workflow-engine-sql; the PostgreSQL and MySQL suites start from empty databases.

Release note

The Workflow Runtime creates and upgrades its PostgreSQL or MySQL databases on start.

Documentation

docs/user-guide/database.md (new: supported databases, rights, Amazon RDS and Aurora, DBA-applied schemas), docs/features/database-schema.md (new), docs/features/runtime-config.md, docs/guides/ARCHITECTURE.md, docs/guides/TESTING.md, engine/README.md.

Automation tests

  • Unit tests: sqlschema 94% with a fake connection (empty, current and older databases, databases the user cannot create, every failure, the lock statements).
  • Integration: three nodes starting together on new PostgreSQL 16 and MySQL 8.4 databases, then a second start (TestPrepareOnRealDatabases, also in the PostgreSQL smoke job); Ballerina smoke on empty PostgreSQL and MySQL, and on a second start.

Security checks

  • Followed secure coding standards: yes
  • Confirmed that this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets: yes

Related PRs

#4, #5 (base).

Test environment

Go 1.26.10, macOS arm64; PostgreSQL 16 and MySQL 8.4 in Docker.

`workflow-engine start --runtime-config conf/runtime.toml` builds
Temporal's server configuration from the [engine] and [database]
tables, so nobody writes Temporal's YAML. PostgreSQL maps to
postgres12, MySQL to mysql8 and SQLite to sqlite. The configuration
is passed in memory through temporal.WithConfig, so the database
password never reaches disk; it can come from the environment with
$env{NAME}, and $secret{} is refused until secure-vault support.

Unknown keys in the two tables, duplicate or invalid ports, and
database settings that cannot work fail at start, and the result is
checked with Temporal's own validation. `render-config` prints the
generated configuration with the password hidden. --config-file
still accepts a Temporal YAML used as is; exactly one is required.
The suite runner starts the engine from tests/engine/runtime-<db>.toml
instead of a hand-written Temporal YAML, and for PostgreSQL and MySQL
creates the databases and schemas with workflow-engine-sql first,
through a new Options.Prepare hook that runs before the engine starts.
CI runs smoke and full against each database, with PostgreSQL 16 and
MySQL 8.4 as service containers.
make test-docker needs nothing but Docker: it runs make check and
make sbom, then the smoke and full suites on SQLite, PostgreSQL 16 and
MySQL 8.4, the same jobs GitHub Actions runs. The databases share a
network namespace with the runner on 15432 and 13306, so the
tests/engine runtime files work unchanged. Each run copies the source
tree, uncommitted changes included, and starts with empty databases;
Go, golangci-lint and Ballerina caches persist in volumes, and a
damaged module cache is reported with the command that resets it.

The image uses the platform-independent Ballerina zip on a Java 25
JRE, since there is no arm64 package, so it runs natively on Apple
Silicon.
With an authorizer on, start refuses a configuration without
services.internal-frontend, and a runtime.toml produced none. The
generated configuration now always has it (port 7236, membership
6936), so a start from runtime.toml runs it with the default services.
koanf (with its TOML parser, the confmap provider and mapstructure)
is what OpenChoreo uses and what WSO2's dependency registry approves
for every scope; BurntSushi/toml is not in the registry. Unknown keys
in [engine] and [database] are still rejected, now through
mapstructure's metadata, and every existing test passes unchanged.
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Too many files!

This PR contains 115 files, which is 15 over the limit of 100.

To get a review, reduce the PR to 100 files or fewer by splitting it into smaller PRs or changing its base branch.

Upgrade to a paid plan to raise the limit.

This review couldn't start because sufficient usage credits or metered capacity aren't available. Add credits or update usage-based reviews in the billing tab, then retry.

Run configuration
Commits

Reviewing files that changed from the base of the PR and between ad78698 and aaa6160.

Files ignored due to path filters: 2
  • engine/go.sum is excluded by !**/*.sum, !engine/go.sum
  • third-party/go-licenses.csv is excluded by !**/*.csv, !third-party/go-licenses.csv
Files selected for processing: 115
  • .github/workflows/ballerina-tests.yml
  • .golangci.yml
  • AGENTS.md
  • Makefile
  • README.md
  • distribution/conf/runtime.toml
  • docs/features/README.md
  • docs/features/database-schema.md
  • docs/features/engine-security.md
  • docs/features/engine.md
  • docs/features/runtime-config.md
  • docs/guides/ARCHITECTURE.md
  • docs/guides/DEVELOPING.md
  • docs/guides/TESTING.md
  • docs/user-guide/README.md
  • docs/user-guide/configure.md
  • docs/user-guide/connect-an-integration.md
  • docs/user-guide/database.md
  • docs/user-guide/monitor.md
  • docs/user-guide/reference/runtime-toml.md
  • docs/user-guide/scale-across-nodes.md
  • docs/user-guide/secure-the-engine.md
  • docs/user-guide/troubleshoot.md
  • docs/user-guide/workflow-runtime.md
  • engine/README.md
  • engine/go.mod
  • engine/internal/cli/cli.go
  • engine/internal/cli/cli_test.go
  • engine/internal/cli/testdata/certs/engine.crt
  • engine/internal/cli/testdata/certs/engine.key
  • engine/internal/cli/testdata/runtime-auth.toml
  • engine/internal/cli/testdata/runtime-postgresql.toml
  • engine/internal/cli/testdata/runtime-sqlite.toml
  • engine/internal/runtimeconfig/fixtures_test.go
  • engine/internal/runtimeconfig/mysqlwords.go
  • engine/internal/runtimeconfig/postgresqltls_test.go
  • engine/internal/runtimeconfig/profiles.go
  • engine/internal/runtimeconfig/runtimeconfig.go
  • engine/internal/runtimeconfig/runtimeconfig_test.go
  • engine/internal/runtimeconfig/security.go
  • engine/internal/runtimeconfig/security_test.go
  • engine/internal/runtimeconfig/testdata/config/database-tls-client-certificate.txtar
  • engine/internal/runtimeconfig/testdata/config/database-tls-mysql-server-name.txtar
  • engine/internal/runtimeconfig/testdata/config/database-tls-verification-off.txtar
  • engine/internal/runtimeconfig/testdata/config/database-tls-verified-by-default.txtar
  • engine/internal/runtimeconfig/testdata/config/engine-overrides.txtar
  • engine/internal/runtimeconfig/testdata/config/error-bad-database-port.txtar
  • engine/internal/runtimeconfig/testdata/config/error-bad-log-level.txtar
  • engine/internal/runtimeconfig/testdata/config/error-bad-max-conns.txtar
  • engine/internal/runtimeconfig/testdata/config/error-bind-all-without-broadcast.txtar
  • engine/internal/runtimeconfig/testdata/config/error-bind-not-an-ip.txtar
  • engine/internal/runtimeconfig/testdata/config/error-duplicate-ports.txtar
  • engine/internal/runtimeconfig/testdata/config/error-membership-collides.txtar
  • engine/internal/runtimeconfig/testdata/config/error-missing-env-password.txtar
  • engine/internal/runtimeconfig/testdata/config/error-missing-host.txtar
  • engine/internal/runtimeconfig/testdata/config/error-missing-type.txtar
  • engine/internal/runtimeconfig/testdata/config/error-mysql-reserved-database-name.txtar
  • engine/internal/runtimeconfig/testdata/config/error-negative-shards.txtar
  • engine/internal/runtimeconfig/testdata/config/error-not-toml.txtar
  • engine/internal/runtimeconfig/testdata/config/error-port-without-room-for-membership.txtar
  • engine/internal/runtimeconfig/testdata/config/error-postgresql-tls-server-name.txtar
  • engine/internal/runtimeconfig/testdata/config/error-same-database-names.txtar
  • engine/internal/runtimeconfig/testdata/config/error-secret-reference.txtar
  • engine/internal/runtimeconfig/testdata/config/error-sqlite-memory-with-two-connections.txtar
  • engine/internal/runtimeconfig/testdata/config/error-sqlite-with-both-locations.txtar
  • engine/internal/runtimeconfig/testdata/config/error-sqlite-with-tls.txtar
  • engine/internal/runtimeconfig/testdata/config/error-sqlite-without-location.txtar
  • engine/internal/runtimeconfig/testdata/config/error-tls-files-without-tls.txtar
  • engine/internal/runtimeconfig/testdata/config/error-tls-half-client-certificate.txtar
  • engine/internal/runtimeconfig/testdata/config/error-tls-server-name-without-tls.txtar
  • engine/internal/runtimeconfig/testdata/config/error-typo-in-database.txtar
  • engine/internal/runtimeconfig/testdata/config/error-typo-in-engine.txtar
  • engine/internal/runtimeconfig/testdata/config/error-unknown-database-type.txtar
  • engine/internal/runtimeconfig/testdata/config/error-unspecified-broadcast-address.txtar
  • engine/internal/runtimeconfig/testdata/config/error-unspecified-broadcast-ipv6.txtar
  • engine/internal/runtimeconfig/testdata/config/mysql-overrides.txtar
  • engine/internal/runtimeconfig/testdata/config/mysql-reserved-database-name-unmanaged.txtar
  • engine/internal/runtimeconfig/testdata/config/other-tables-ignored.txtar
  • engine/internal/runtimeconfig/testdata/config/password-from-environment.txtar
  • engine/internal/runtimeconfig/testdata/config/postgresql.txtar
  • engine/internal/runtimeconfig/testdata/config/sqlite-file.txtar
  • engine/internal/runtimeconfig/testdata/config/sqlite-memory.txtar
  • engine/internal/sqlschema/sqlschema.go
  • engine/internal/sqlschema/sqlschema_db_test.go
  • engine/internal/sqlschema/sqlschema_test.go
  • engine/plugins/jwtclaims/discovery.go
  • engine/plugins/jwtclaims/discovery_test.go
  • engine/plugins/jwtclaims/jwtclaims.go
  • engine/plugins/jwtclaims/jwtclaims_test.go
  • launcher/README.md
  • tests/README.md
  • tests/ballerina/upgrade/Ballerina.toml
  • tests/ballerina/upgrade/tests/Config.toml
  • tests/ballerina/upgrade/tests/upgrade_test.bal
  • tests/ballerina/upgrade/workflows.bal
  • tests/docker/Dockerfile
  • tests/docker/compose.yml
  • tests/docker/run.sh
  • tests/engine/runtime-mysql.toml
  • tests/engine/runtime-postgresql.toml
  • tests/engine/runtime-sqlite.toml
  • tests/engine/temporal-test.yaml
  • tools/cmd/balsuite/main.go
  • tools/internal/balsuite/balsuite.go
  • tools/internal/balsuite/balsuite_test.go
  • tools/internal/balsuite/upgrade.go
  • tools/internal/balsuite/upgrade_test.go
  • tools/internal/enginetest/doc.go
  • tools/internal/enginetest/engine_test.go
  • tools/internal/enginetest/portlock_unix_test.go
  • tools/internal/enginetest/portlock_windows_test.go
  • tools/internal/enginetest/security_helpers_test.go
  • tools/internal/enginetest/security_test.go
  • tools/internal/enginetest/sqlite_test.go
  • tools/internal/scripts/docker_runner_test.go

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

With [database.tls] enabled, the server's certificate and host name are now verified unless
enableHostVerification = false: before, an omitted setting skipped verification (InsecureSkipVerify on MySQL,
sslmode=require on PostgreSQL). Half a client certificate pair, and an in-memory SQLite database with more than
one connection (each private connection gets its own empty database), are refused at load. The SQLite location
rule reports both and neither separately, and database.type is a typed value.

The configuration tests move to one txtar fixture per case under testdata/config: the input and the whole
rendered Temporal configuration, or the exact error, compared by a parallel harness (-update rewrites them).
The suite runner passes WORKFLOW_TEST_DB_PASSWORD to workflow-engine-sql as SQL_PASSWORD instead of --pw, and treats only "already exists" as success: "database does not exist" and every other failure stop the run.
In a linked worktree .git points to the repository's Git directory outside the checkout. make test-docker now mounts that directory at its host path, and the runner builds self-contained metadata in /work at the checked-out commit instead of copying the pointer. The runner's paths are configurable, so tools/internal/scripts runs it for real (linked worktree, targets, refusals) with fake make and go; tar options are portable.
tools/internal/enginetest starts the real engine binary on free ports and drives it with the Temporal CLI: a new SQLite file gets its schema on the first start, and a namespace and a waiting workflow (same run, still running) survive a restart; the restarted engine then closes the workflow. Runs in the SQLite smoke job; skips without WORKFLOW_ENGINE and the temporal CLI. The CLI, not Temporal's Go SDK, drives the engine because the SDK is not in the WSO2 registry.
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

Temporal retires a pooled connection after MaxConnLifetime, and an in-memory SQLite database dies with its only
connection, so the lifetime is now 0 there. [database.tls] on SQLite is refused because the plugin never reads it.
The fixture harness no longer records an unexpected parse error under -update. The schema helper tolerates
"already exists" only for create-database and setup-schema; its tests check both databases' arguments and that no
password reaches them, and the Docker runner tests check a local edit and an unrequested database.
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

serverName and enableHostVerification were ignored with database.tls.enabled = false; they now fail like the
files. An error fixture whose configuration parses now fails instead of comparing two empty errors.
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

@heshanpadmasiri heshanpadmasiri 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.

Reviewed the schema-on-start changes. Three correctness findings and two separate testing/maintainability comments below.

Comment thread engine/internal/sqlschema/sqlschema.go Outdated
}
return fmt.Errorf("unable to read the tables of database %s: %w", name, err)
}
if !slices.Contains(tables, versionTable) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Recover incomplete schema-version bootstrap

Temporal creates schema_version and schema_update_history before inserting the initial version row. An interruption or failure between these operations leaves the version table present but initialization incomplete. The next start skips setup because it checks only table existence; the update task then fails reading the missing version row. Repeated starts cannot recover without DBA intervention.

Please detect incomplete versioning state and resume initialization without resetting a valid schema version. Add regression coverage for a version table without its version row, including a missing update-history table.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in e3949c4. Setup now runs unless both version tables exist and schema_version holds this database's row, so a setup that stopped after creating the tables (with or without schema_update_history) is finished on the next start. Only sql.ErrNoRows counts as "not versioned"; any other read error stops the start, so a valid version is never reset to 0.0. Covered by TestPrepareResumesAnInterruptedSetup (both cases) and TestPrepareResumesAnInterruptedSetupOnRealDatabases on PostgreSQL and MySQL.

Comment thread engine/internal/sqlschema/sqlschema.go Outdated
dir := schemaDirs[stores[0].sql.PluginName]
// One connection to the server's default database: it holds the lock and creates the databases.
server := *stores[0].sql
server.DatabaseName = ""

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Avoid an undocumented PostgreSQL maintenance-database requirement

Clearing DatabaseName makes the pinned Temporal PostgreSQL plugin try postgres and defaultdb. A runtime role with full rights on the two DBA-created workflow databases, but no connection permission on either maintenance database, fails before those workflow databases are used. The guide does not document this additional permission requirement.

Please provide a supported maintenance/lock database selection or a safe path using an accessible existing database. Document any required connection privileges and test a restricted role with access limited to the configured databases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in e3949c4. The schema lock now lives in the workflow database, which every node opens. The maintenance database is used only to create missing databases, and the engine skips that when it cannot connect. A role limited to its two DBA-created databases therefore needs nothing else. database.md lists the privileges for both paths (runtime creates them, or the DBA creates them with the role as owner). TestPrepareWithARoleLimitedToItsDatabases runs on PostgreSQL with CONNECT on postgres revoked from PUBLIC, and on MySQL with a user granted only the two databases.

problems = append(problems, "database.name and database.visibilityName must differ")
}
for _, n := range []struct{ key, value string }{{"name", d.Name}, {"visibilityName", d.VisibilityName}} {
if *d.ManageSchema && !databaseName.MatchString(n.value) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P2] Handle reserved MySQL database identifiers

A name such as order passes the character and length validation, but Temporal's MySQL database-creation implementation interpolates identifiers without quoting. The resulting CREATE DATABASE IF NOT EXISTS order CHARACTER SET utf8mb4 is invalid, so an accepted configuration cannot bootstrap its database.

Please correctly quote identifiers through a public SQL execution API, or explicitly reject unsupported reserved names during configuration validation. Add a MySQL reserved-word database-name test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in e3949c4 by refusing them: Temporal's MySQL tool also interpolates the name unquoted in SHOW TABLES FROM, so quoting our own CREATE DATABASE would not be enough. With type = "mysql" and manageSchema = true, a reserved word (MySQL 8.4's 262, taken from INFORMATION_SCHEMA.KEYWORDS) is refused, and the message suggests manageSchema = false. Fixtures: error-mysql-reserved-database-name (order refused) and mysql-reserved-database-name-unmanaged (accepted).

t.Fatalf("node %d: Prepare = %v", i, err)
}
}
if err := Prepare(p, log.NewNoopLogger()); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Human-review flag][P2] Add real existing-data upgrade coverage

The real-database test covers empty databases and another start at the current version, not upgrading an older database containing workflow data. The unit upgrade test accepts migration SQL through a fake connection without executing it.

Please add an older-version schema fixture with representative persisted data for PostgreSQL and MySQL. Verify the upgraded schema and preserved data; ideally demonstrate that a workflow can continue afterward.

This is a missing-test concern, not an observed migration failure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added in e3949c4: TestPrepareUpgradesExistingDataOnRealDatabases builds both databases at schema 1.0 (the oldest Temporal ships) on PostgreSQL and MySQL. It inserts a namespace, a workflow execution and its visibility row, runs Prepare, and checks the current versions and that every row is intact. Continuing a workflow across an upgrade needs the previous engine release to write the history, so that end-to-end case belongs in the release tests (old release → new release), not in this PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now covered end to end in 0af14fb, so this no longer waits for the release tests. balsuite upgrade installs the schemas of Temporal 1.30.9 itself (temporal 1.18, visibility 1.13). 1.30 is the newest release older than the engine's schema, since 1.31 already ships 1.19/1.14. It runs the 1.30.9 server in Docker, where a Ballerina order workflow runs its first activity and waits for a decision. Then it stops that server and starts the engine on the same databases; the engine upgrades them to 1.19/1.14. The decision is sent to the engine, and the workflow runs its second activity there and completes with the expected result. It runs on PostgreSQL and MySQL as a step in the smoke jobs of every pull request; locally both pass in about 30 s.

// under the License.
//

// Package sqlschema creates Temporal's SQL databases and brings their schemas to the version this binary ships.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Human-review flag] Enforce the SQL ownership boundary

Keeping first-party SQL queries in one package is a good idea, but I am not convinced coding agents will reliably preserve that pattern through convention alone. Please document the intended boundary and add a linter or equivalent CI check to enforce it. Scope the rule to our own queries and automatic Temporal schema-management code, allowing intentional delegation to Temporal, the DBA CLI, and test fixtures.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 83637c1. AGENTS.md states the boundary: our own SQL lives in engine/internal/sqlschema, and everything else delegates to Temporal's plugins, the DBA tool or test fixtures. A depguard rule in .golangci.yml refuses database/sql, sqlx, the drivers, and Temporal's SQL and schema tools elsewhere, while still allowing the plugin registration imports. It already caught one case on #8, where dev mode wrote namespaces from the cli package; that code moved to sqlschema there (f56fe04).

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

1 similar comment
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

Temporal's default claim mapper reads one top-level claim of "<namespace>:<role>" values, which identity
providers rarely issue. jwtclaims verifies the token against the provider's key set (Temporal's token key
provider), checks expiry, issuer and audience, reads a nested or space-separated roles claim, and maps its
values to namespace roles through rules, Temporal's own format, or a regular expression with named groups.
A role in namespace "*" becomes Temporal's system role, which applies to every namespace.

Uses github.com/golang-jwt/jwt/v5 (MIT, in the WSO2 registry for all scopes); Temporal links v4.
[engine.tls] puts TLS on the frontend, optionally requiring client certificates, and on internode traffic.
[engine.auth] turns on Temporal's default authorizer with the jwtclaims mapper built by the start command.
[engine.metrics] serves Prometheus metrics. [runtime] profiles choose the services a node runs (all, engine,
frontend with the internal frontend, history, matching, worker; api starts nothing), and --service still
overrides them. Every setting is validated, certificate files included.

Checked against a real engine: TLS required, an admin token creates namespaces and starts workflows, a reader
token lists but cannot start, tokens without a matching group or no token are refused, and a ballerina/workflow
integration connects with its token and the CA.
docs/user-guide is the source of the Workflow Runtime section of the WSO2 Integrator documentation, shaped like
its ICP section: configure the runtime, secure the engine (TLS, the identity-provider permission model, rules per
provider), connect an integration, run across nodes, monitor, troubleshoot, and a runtime.toml reference.
AGENTS.md now requires every user-visible change to update it with working instructions and to check the whole
guide for consistency.
With issuer and no jwksUrls, the start command reads <issuer>/.well-known/openid-configuration and uses its
jwks_uri, so any OpenID Connect provider needs only its issuer. The document must name the same issuer, and the
engine does not start when it cannot read it. The user guide adds WSO2 Identity Server, Asgardeo and other
OpenID Connect providers.
When ballerina/workflow registers WorkflowKind itself and starts a workflow at once, the frontend refuses it
until its namespace cache refreshes; creating the attribute with the namespace avoids that (seen with Compose).
With engine.auth.type = "jwt", the engine now refuses to start without [engine.tls], so tokens never cross the
network in plain text, and without [engine.tls.internode] when bindAddress is not a loopback address: Temporal
gives the internal frontend system access without a token (its internal claim mapper), so it and the other
service ports must accept only internode certificates. A rule granting "worker" is refused with "grant write":
Temporal's default authorizer needs write for polling, so a worker-only grant refused every call it was for.
A start with neither issuer nor audience logs a warning. Runtime.Temporal is held by value, and the server
factory receives the services, so a test sees what the profiles and --service choose.

The guide drops the worker role, states the TLS and internode requirements with firewall advice and the
issuer/audience risk, and the architecture's profile table uses api, the API server's profile.

tools/internal/enginetest/security_test.go runs a real engine with TLS, internode mutual TLS and an OpenID
Connect provider: an integration token works in its namespace; another namespace, a reader writing, no token,
expired, forged, other-issuer and unmatched tokens, and plaintext are refused; the internal frontend refuses
callers without an internode certificate and answers one with it. Mapped grants are also checked through
Temporal's authorizer (polling).
CLI 1.9 (the CI pin) names the reason, "requires TLS" or "requires client certificate", where 1.6 printed a
connection error, so TestSecuredEngine failed on CI only.
jwksUrls, a discovery issuer and a discovered jwks_uri must use https, or plain http to a loopback host: a key set
replaced on the way would let anyone sign tokens the engine trusts. Discovery compares the issuer exactly, since
tokens are checked against it as written. prometheusAddress needs a numeric port up to 65535. The guide says that
issuer alone does not limit tokens to this engine and gives the SQLite historyShards default.
workflow-engine start --runtime-config now prepares Temporal's workflow and visibility databases on PostgreSQL
and MySQL before the services start: it creates missing databases, sets up schema versioning in an empty
database and applies the schema versions embedded in the binary, through Temporal's own schema tool packages.

Nodes started together serialize on a server-wide lock (pg_advisory_lock, GET_LOCK) taken on a single
connection to the server's default database; without it, concurrent setups fail with duplicate keys. A user who
cannot create databases still works with databases a DBA created. [database] manageSchema = false leaves the
schema to the DBA and Temporal's start-up check. With schema management on, database names are restricted to
what MySQL's unquoted CREATE DATABASE accepts.

Resolves wso2/product-integrator#2911
The suite runner no longer creates the databases and schemas with workflow-engine-sql; the engine does it on
start, so the PostgreSQL and MySQL suites now start from empty databases. The smoke job on PostgreSQL also runs
the schema test against both service databases, three nodes starting together and then a second start.
Covers the supported databases, the user's rights, Amazon RDS and Aurora with TLS, and installations where a
DBA applies schema changes; configure, troubleshoot and the reference follow schema creation on start.
A setup that stopped after creating schema_version but before recording 0.0 is finished on the next start; a
version that cannot be read for another reason stops the start instead of being reset to 0.0. The schema lock now
lives in the workflow database, and databases are created through the maintenance database only when it can be
reached, so a role limited to its two DBA-created databases works. MySQL reserved words are refused as database
names when the engine manages the schema: Temporal's schema tool writes them unquoted.

The real-database tests now also cover an interrupted setup, databases at schema 1.0 holding a namespace, a
workflow and its visibility row (upgraded with the rows kept), and a role limited to its databases.
A depguard rule refuses database drivers, database/sql, sqlx and Temporal's SQL and schema tools outside
engine/internal/sqlschema, the DBA tool and tests. Registering Temporal's persistence plugins stays allowed.
balsuite upgrade installs Temporal 1.30.9's schemas (temporal 1.18, visibility 1.13) with its admin-tools image,
runs its server in Docker and starts tests/ballerina/upgrade's order workflow there: it runs an activity and waits
for a decision. The runner checks the history, stops the old server, starts the engine on the same databases
(which upgrades them to 1.19 and 1.14), and the after group sends the decision; the workflow runs its second
activity on the engine and completes. 1.30 is the newest release with an older schema: 1.31 already ships the
engine's versions. The PostgreSQL and MySQL smoke jobs run it on every pull request.
Name the databases instead of excluding sqlite, so a new matrix entry such as dev mode does not run it.
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Dependency validation

Status Module Version Note
APPROVED github.com/go-viper/mapstructure/v2 v2.5.0 Approved (constraint: >=v2.4.0)
APPROVED github.com/golang-jwt/jwt/v5 v5.3.1 Approved (constraint: >=v5.3.1)
APPROVED github.com/jackc/pgx/v5 v5.10.0 Approved (constraint: >=v5.8.0)
APPROVED github.com/jmoiron/sqlx v1.4.0 Approved (constraint: >=v1.4.0)
APPROVED github.com/knadh/koanf/parsers/toml/v2 v2.2.2 Approved (constraint: >=v2.2.0)
APPROVED github.com/knadh/koanf/providers/confmap v1.0.1 Approved (constraint: >=v1.0.0)
APPROVED github.com/knadh/koanf/v2 v2.3.8 Approved (constraint: >=v2.3.2)
APPROVED gopkg.in/yaml.v3 v3.0.1 Approved (constraint: >=v3.0.1)

@hasithaa

hasithaa commented Nov 4, 2026

Copy link
Copy Markdown
Contributor Author

Moved to #10, opened from a wso2 branch so the remaining PRs form a GitHub stack (stack #13). It has the same commits. The review threads here stay as the history, and new review happens on #10.

@hasithaa hasithaa 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.

Create and upgrade the Workflow Runtime's database schema on start

2 participants