Skip to content

Dispatch schema dump/restore

The isolated PostgreSQL 16 / current Rails experiment in tests/dispatch_schema_roundtrip_test.sh verifies the reserved schema can be dumped and loaded into a pre-provisioned database without dropping that schema. It uses disposable containers and a temporary copy of the Rails app; it does not modify the development database or the repository’s structure.sql.

Rails’ SQL dump includes the dispatch schema, tables, sequences and RLS policies, but normally emits CREATE SCHEMA even when provisioning already created the schema. It also deliberately omits object ACLs. The PostgreSQL structure loader is therefore adapted in apps/web/lib/notifi/dispatch_schema_restore.rb when a dump includes notifi_dispatch:

  • Replace the schema creation statement only in a temporary copy, and verify the existing or newly created schema is owned by notifi_migrator and that the loader uses that role. Never drop an existing schema.
  • Load the temporary SQL in one PostgreSQL transaction, including a final revoke of PUBLIC and app execution for all routines in the reserved schema. Verify no role that the app can assume retains EXECUTE. An error rolls back the load.
  • Fail closed if SQL references the reserved schema but its expected schema definition is missing. Dumps that do not reference it use ordinary Rails load.

Because Rails’ dump omits ACLs, this is intentionally fail-closed, not a restoration of previous function grants. No dispatch routine is currently allowed to execute as the app after restore. A future dispatch function must not be enabled until an explicit reviewed grant/reconciliation strategy exists for fresh creation, schema restore and provisioning reruns. Do not rely on a one-off migration GRANT surviving SQL schema restore.

Provisioning also revokes PostgreSQL’s global default PUBLIC EXECUTE for new functions created by notifi_migrator in every schema. Existing functions are not changed by that default; any future function that should be callable must receive an explicit reviewed grant. Provisioning verifies app/assumable roles do not have EXECUTE on reserved routines and tests the new-function default. This is separate from application authorization and tenant identity.

The isolated test covers a provisioned target, schema owner, RLS policy, FORCE RLS, table/sequence permissions, PUBLIC function execution, wrong schema owner and rollback of a deliberately failing load. It deliberately enables PUBLIC function defaults before restoration to verify the loader closes execution, and reruns provisioning afterwards. The separate privilege test covers broad public table grants, provisioning reruns and unsafe default/inherited role grants.

These tests do not establish a production migration, worker-role policy, function allowlist, operational recovery procedure or safety of arbitrary SQL under the app role. Rails version changes may affect the PostgreSQL task hook and SQL dump format; the round-trip test must run before relying on such changes. Do not treat this as authorization to create a sendable dispatch or expose a privileged database function.