pgroll Documentation

repository·main·Indexed 26 days ago

https://github.com/xataio/pgroll

pgroll is a command-line tool for PostgreSQL that enables zero-downtime, reversible schema migrations. It uses virtual schemas (views) to serve multiple schema versions simultaneously, allowing for safe rollouts and instant rollbacks. The tool supports defining migrations in YAML or JSON, converting SQL statements to migrations, and managing database baselines.

Tokens
19.6K
Snippets
69
Records
147
Agent score
91%

What's inside pgroll

  1. Overview of pgroll

    main
    pgroll is a migration tool for PostgreSQL designed for zero-downtime, reversible schema migrations. It allows applications to access different versions of a database schema simultaneously, ensuring availability during schema changes and enabling instant rollbacks.
  2. Understand pgroll's lock-safe declarative migrations

    main

    pgroll uses a declarative approach to migrations instead of requiring direct SQL scripts. This provides several safety benefits:

    • Automated safety steps: pgroll implements the necessary sequence of steps to perform a schema change safely.
    • Minimized lock contention: The tool ensures that any locks required on affected database objects are held for the shortest possible duration, reducing the risk of blocking application traffic.
  3. Understand pgroll's multi-version migration approach

    main

    Unlike traditional migration tools, pgroll enables zero-downtime schema changes by maintaining two versions of the schema simultaneously: the version before the change and the version after the change. This allows for:

    • Side-by-side rollouts: Run new application versions that require the updated schema alongside old application versions that are incompatible with it.
    • Version selection: Applications can explicitly select which schema version they want to interact with during a rollout.
  4. How pgroll manages multiple schema versions

    main

    pgroll enables side-by-side schema versions by creating a separate Postgres schema for each migration. It uses Postgres views to expose specific tables or columns to client applications based on the schema version they are configured to use.

    Key Mechanisms:

    • Renaming Columns: pgroll creates a new schema with a view that uses the new column name. The old schema remains untouched until the complete phase, where the old schema is dropped and the actual column is renamed.
    • Complex Constraints (e.g., NOT NULL): For changes like adding a NOT NULL constraint, pgroll duplicates the affected column and backfills it. It uses triggers or backfills to keep the old and new columns in sync, allowing both versions to coexist until the migration is completed.
  5. Drop a column using pgroll

    main

    To drop a column from an existing table, define a drop_column operation. You must provide the table name, the column name to be removed, and a down SQL expression. The down field is mandatory because it is used to backfill the column with data if you need to revert to the previous schema version while a migration is active.

    For example, if you drop a price column, you can provide a down expression that sets the value to 0 for any new rows inserted against the new schema, ensuring compatibility with the old schema during the migration period.

    drop_column:
      table: name of table
      column: name of column to drop
      down: SQL expression
  6. Configure client applications to use a new schema version

    main

    Once a migration has started, client applications can access the new schema version by setting their PostgreSQL search_path to the new schema version name. The specific name is provided in the output of the pgroll start command.

    Example:

    SET search_path TO 'public_initial_migration';
    SET search_path TO 'public_initial_migration';
  7. Rename a table using pgroll

    main

    To rename a table, define a rename_table operation in your migration file. The table remains accessible via its old name in the old schema version and becomes accessible via its new name in the new schema version. The actual renaming of the table in the database occurs only upon successful migration completion.

    Structure of the operation:

    • from: The current name of the table.
    • to: The desired new name for the table.
  8. Prepare the database with init

    main

    Before using pgroll, you must initialize the database. This creates internal tables to track the current schema version and version history.

    Use the init command with the --postgres-url flag.

    pgroll --postgres-url postgres://user:password@host:port/dbname init
  9. Rename a constraint using `rename_constraint`

    main

    To rename a constraint, use the rename_constraint operation. The constraint will retain its old name during the active migration period and will only be renamed to the new name upon migration completion.

    Structure:

    • table: The name of the table containing the constraint.
    • from: The current name of the constraint.
    • to: The desired new name for the constraint.
    {
      "rename_constraint": {
        "table": "table name",
        "from": "old constraint name",
        "to": "new constraint name"
      }
    }