Docs Version v0.2
Docs menu

Running in production

Upgrading and rolling back

Changed 9 min readv0.2

On this page

Introduction

A new release of Signari usually brings changes to the database's structure, as numbered "migrations". Upgrading means applying those migrations with the new release, then replacing the running servers with it. This page gives the order, and explains what happens when something goes wrong partway through.

Three commands do the work. The first says where the database is, the second applies the new release's migrations, and the third says whether a given release may serve the database:

Shellsignari migrate status        # where the database is, and the oldest binary that may serve itsignari migrate up            # the upgrade: as signari_engine, before the new binary servessignari verify                # whether this binary may serve this database

There are no down migrations. A migration that dropped a column cannot give the data back, and a down script that tried would be untested when it was needed. Two things take their place: a release before a migration can be allowed to keep running against it, and everything else is rolled back by restoring the backup taken before the upgrade.

What each schema version changed, and whether the release before may keep serving it, is in Schema versions. Read the versions between the one migrate status reports and the one the new binary expects.

The order

  1. Back up: Backup and restore. That backup is the rollback for any migration that is not compatible.
  2. Run signari migrate up with the new binary, once. Two at once take turns.
  3. Replace the engine nodes with the new binary.

Nodes of the previous release keep serving during step 3: the schema is checked when a node starts, not while it runs. A previous-release node that restarts during the roll, because it crashed or was rescheduled, checks again.

What the previous release does against the new schema

A binary serves a database ahead of it when every migration since its own version was declared compatible with the version before. It refuses every other database that is not at its own version:

Text$ signari-161 migrate statusdatabase version : 162binary expects   : 161ledger           : matches what migration 162 recordedoldest binary    : 161 -- the migrations since are compatible with it, so it may serve this database$ signari-161 verifyschema ok

Against a database whose newer migrations are not compatible, it refuses, and says what to do:

Text$ signari-161 verifysignari: the database is at 163, ahead of this binary (161), and migration         0163_drill_probe_dropped is not compatible with an earlier binary --         deploy a binary built for 163, or restore the backup taken before the         upgrade (docs/upgrading.md)

The declaration is the line -- signari:compatible-with-previous in a migration's leading comment. The ledger records it when the migration is applied, because the previous binary has never seen the file. migrate status prints the oldest binary that may serve the database.

Rolling back

  • migrate status names a binary as old as the one you are going back to: deploy it. Its migrate up finds nothing to apply and says the database is ahead, and it serves.
  • It does not: restore the backup from step 1, by the runbook, then deploy the previous binary.

A restored database is checked like any other. It matches its own ledger: a test dumps and restores a database migrated from empty and fails on any line of the fingerprint the round trip changes.

The drill

Run against a copy of a real database. Each step's result is what it printed.

StepResult
The previous release (159) restarts against 161refused: database is at 161, this binary expects 159. That release predates the declaration
161 migrates its database and servesdiscovery 200
162 applies a compatible migration; 161 restarts against itschema ok, oldest binary : 161, discovery 200
Roll back to 161: its migrate job, then servenothing to apply, the database is at 162, ahead of this binary (161), exit 0; discovery 200
Back up at 162; 163 applies a migration that is not compatible161 and 162 both refused, naming 0163_drill_probe_dropped
Restore the 162 backup by the runbook; 161 against it0 restore errors, schema ok, keys list opens the signing keys, discovery 200

Writing a compatible migration

Declare it only when the previous release keeps working against the result. The file is checked when it is loaded, and the binary refuses to start with a declaration on a migration that does anything but add:

AllowedRefused, because the previous release relies on it
CREATE TABLE, and on that table any ALTER, policy, trigger, unique index or rowa constraint, policy, trigger or unique index on a table it already has
ALTER TABLE … ADD COLUMN, nullable or with a default, one per statementDROP, RENAME, ALTER COLUMN, a NOT NULL column with no default
CREATE INDEX that is not uniqueDROP INDEX
CREATE FUNCTION, CREATE VIEWCREATE OR REPLACE either: the previous release calls or reads the old one
COMMENT ON, GRANT, SETREVOKE, INSERT, UPDATE or DELETE on a table it reads, DO blocks

The check reads the SQL and does not run it, so it is a floor. A new column the previous release would read wrongly, or a new row it would misread, is the author's to rule out.

Serving as signari_engine

From v0.2.0, serve refuses to start when SIGNARI_DSN, or SIGNARI_READ_DSN when the Admin API has a read replica, signs in as, acts as or may SET ROLE to a superuser or a role with BYPASSRLS. Row-level security is not the reason, since the engine's own role, signari_engine, reads every organisation by design. The reason is that a superuser can run programs and read files on the database server (COPY ... PROGRAM, pg_read_server_files), change that database server's configuration (ALTER SYSTEM) and reach every database on the cluster, the console's admin schema among them, and a BYPASSRLS role passes every row-level policy on every table it may read. The engine needs none of that, so serving as either would give a compromised engine, or a single SQL injection, powers the engine never uses. v0.1.0's README and its compose file both served as a superuser.

v0.1.0's signari doctor cannot see this, so check before you upgrade with the connection the engine serves with now:

Shellpsql "$SIGNARI_DSN" -Atc "SELECT rolname FROM pg_roles WHERE (rolsuper OR rolbypassrls)  AND (rolname IN (session_user, current_user) OR pg_has_role(session_user, oid, 'SET'))"

In this example, nothing printed means the upgrade does not affect you. Any role it prints is one serve would refuse, and this section is for that deployment. From v0.2.0, signari doctor reports the same thing as critical.

Move it to signari_engine, which migration 0001 made, in either of two ways. Either way, keep the superuser for signari migrate bootstrap alone, and grant signari_engine no role it could SET ROLE to.

With signari setup, against the database you have, on the machine that runs it. It reaches PostgreSQL as you over the local socket, or, run as root, as the postgres system account; elsewhere, give -superuser-dsn-file a file holding a superuser's URL. It needs the root key you serve with now in a file, since a new key would open none of the signing keys already sealed. For the v0.1.0 quick start's database:

Shellmkdir -p -m 0700 ~/.config/signari( umask 077; printf '%s\n' "$SIGNARI_ROOT_KEY" > ~/.config/signari/root.key )signari setup -database signari_dev -issuer http://localhost:8080 -email you@example.com \  -root-key-file ~/.config/signari/root.key

As root, the files go under /etc/signari instead. Setup skips what is there: the database, migration 0001, the migrations after it, the instance, its organisation, and the link when people already sign in. It refuses a root key that does not open the signing keys. It gives signari_engine a password, checks that pg_hba.conf lets it sign in over TCP, and writes the env file, ~/.config/signari/signari.env here, with SIGNARI_DSN for it. If the organisation has no owner yet, it makes the person -email names its org-owner, once they have an account. Then serve from that file, with SIGNARI_ROOT_KEY no longer set, since the file names the key:

Shellunset SIGNARI_ROOT_KEYset -a; . ~/.config/signari/signari.env; set +a; signari serve

By hand, as a superuser, give the role a password, then point SIGNARI_DSN at it:

Shellpsql -d signari_dev -c '\password signari_engine'      # asks twice, and sends only its hashexport SIGNARI_DSN='postgres://signari_engine:<that password>@127.0.0.1:5432/signari_dev?sslmode=disable'

pg_hba.conf must let signari_engine sign in over TCP with a password, which Debian's and Ubuntu's own host all all 127.0.0.1/32 scram-sha-256 line does. signari doctor then reports no database finding.

For the compose stack, a data volume made before the engine served as signari_engine needs the role's password set once. The header of deploy/postgres-init/20-engine-role.sh gives the command.

With the Helm chart

A release whose SIGNARI_DSN is a superuser's, as the chart's migrations needed before migrations.superuserDsnSecretKey, moves in three steps. First, as that superuser, give signari_engine a password:

Shellpsql 'postgres://postgres@db:5432/signari' -c '\password signari_engine'

Then put the superuser's data source name (DSN) in the Secret under its own key, BOOTSTRAP_DSN, and SIGNARI_DSN as signari_engine, keeping the root key you serve with now:

Shellkubectl create secret generic signari-secrets --dry-run=client -o yaml \  --from-literal=SIGNARI_DSN='postgres://signari_engine:<that password>@db:5432/signari?sslmode=require' \  --from-literal=BOOTSTRAP_DSN='postgres://postgres:<its password>@db:5432/signari?sslmode=require' \  --from-literal=SIGNARI_ROOT_KEY='<the root key you serve with now>' \  --from-literal=SIGNARI_ROOT_KEY_REF=k8s | kubectl apply -f -

Last, upgrade with the migration Job reading the superuser's DSN from that key:

Shellhelm upgrade signari ./deploy/helm/signari --reuse-values \  --set secret.existingSecret=signari-secrets --set migrations.superuserDsnSecretKey=BOOTSTRAP_DSN

In this example, the Secret is one you manage. A release that kept its DSN inline moves to a Secret of its own the same way, since the Secret the chart makes from inline values has no key for the superuser's DSN. Until you can move it, the override starts the engine as before, with a warning:

Shellhelm upgrade signari ./deploy/helm/signari --reuse-values --set-string extraEnv.SIGNARI_ALLOW_RLS_BYPASS=1

Until you have moved it, SIGNARI_ALLOW_RLS_BYPASS=1, or serve -allow-rls-bypass, starts the engine as before, with a warning at startup, and signari doctor goes on reporting it.

Which PostgreSQL

Runs on16 and later. An older major is refused rather than assumed to work: signari verify, serve and migrate refuse it before applying anything
Tested on16 and 17, the whole engine suite against each. A later major runs, and signari doctor warns that it was not tested here
Not 15Every migration applies on 15, but 15 writes out a view's definition differently, with each column qualified by its table. So the schema fingerprints differently there, and a release pinned to the fingerprint would refuse every 15 database. 16 and 17 fingerprint the same schema alike: 17 lists MAINTAIN among a table owner's privileges and 16 has no such privilege, so the owner's own is left out
Bootstrapneeds a role that may create roles, one of them with BYPASSRLS (signari_maintenance), and grant schemas to them: a superuser on your own server. Whether a managed service's administrator role may do this is the service's to say, and has not been tried here

Upgrading PostgreSQL

The fingerprint a migration recorded is compared only under the PostgreSQL major version that computed it, because a catalogue may render the same schema differently in another. After a major upgrade, migrate status says the ledger is not compared, and the comparison resumes with the next migration. See Pinning a build to its schema.