SchemaSmith Documentation

Rollback & Recovery - PostgreSQL (Enterprise)

Revert to any prior release state—schema changes apply automatically.

SchemaSmith rollback and recovery

Overview

With state-based deployments, rolling back to a prior release is straightforward: deploy the previous release's product definition. SchemaQuench compares the target state to the current database and applies the necessary changes to bring the database back to that prior state.

Unlike changelog-driven tools that require pre-planned rollback tags and manually-written rollback scripts, SchemaSmith's declarative approach means your schema metadata already describes what each release should look like. Rolling back is simply a matter of pointing SchemaQuench at an earlier version of that metadata.

What Rolls Back Automatically

When you deploy a prior release's metadata, SchemaQuench automatically reverts all schema objects to match that state.

Table Structure

  • Tables added in the newer release are dropped
  • Columns added are dropped; columns removed are re-added
  • Column modifications (data type, nullability, defaults) revert
  • Indexes, constraints, and foreign keys revert to prior definitions
  • Check constraints revert

SQL Objects

  • Functions restore to prior version
  • Views restore to prior version
  • Triggers restore to prior version
  • All scripted objects in your product definition revert

What Requires Migration Scripts

While schema structure reverts automatically, data preservation for destructive operations requires planning. SchemaSmith handles the schema; you handle the data when needed.

Data Preservation Scenarios

Scenario What You Need
Dropping columns with data If a column was added in the newer release and contains data you want to preserve during rollback, write a migration script to copy that data before SchemaQuench drops the column.
Restoring dropped columns If the newer release dropped a column and you want to restore the data that was in it, SchemaSmith recreates the column structure but the original data is gone. You'll need a backup or migration script that preserved the data before the original drop.
Table drop with data For table-level data preservation, Enterprise offers custom drop/restore hooks that let you implement a recycle bin pattern instead of immediate drops.
Data transformations Complex data migrations (splitting columns, merging tables, etc.) require migration scripts to transform data appropriately during rollback.
Note: Most rollbacks don't require migration scripts. Data preservation is needed when rolling back involves data loss—either from columns added in the newer release, or when restoring columns that were dropped. SchemaSmith recreates the column structure, but the original data is gone unless you preserved it before the drop.

How To Roll Back a Release

  1. Get the prior release's product definition

    Check out the tagged release from source control, or retrieve the product definition artifact from your prior deployment.

  2. Review what will change

    If needed, compare the prior metadata to the current database state to understand what SchemaQuench will modify.

  3. Run any data preservation scripts (if needed)

    If the rollback will drop columns containing data you need, or if you need to restore data in columns that were previously dropped, run your migration scripts first.

  4. Configure appsettings for the target environment

    Ensure your appsettings.json points to the correct servers and databases.

  5. Run SchemaQuench

    Execute SchemaQuench with the prior release's product definition:

    SchemaQuench
  6. Verify the deployment

    Review the SchemaQuench logs to confirm all changes applied successfully.

Custom Table Drop/Restore Hooks

Enterprise includes hooks that let you intercept table drops and implement custom handling such as a recycle bin pattern. Instead of immediately dropping tables removed from your product definition, SchemaQuench can call your custom functions.

schemasmith.custom_table_drop

When SchemaQuench detects a table should be dropped (it was in the product but has been removed), it checks for this function. If it exists, SchemaQuench calls it instead of executing DROP TABLE.

Parameters:

  • p_schema - The table's schema name
  • p_name - The table name

schemasmith.custom_table_restore

Near the beginning of the quench process, SchemaQuench attempts to restore tables that were previously custom-dropped. If this function exists, it's called before adding new tables.

Parameters:

  • p_schema - The table's schema name
  • p_name - The table name

Example: Recycle Bin Pattern

A common use case is moving dropped tables to a "recycle" schema for a retention period:

  • custom_table_drop: Move the table to a recycle schema instead of dropping it
  • custom_table_restore: Check if the table exists in recycle and move it back
  • Scheduled cleanup: A separate job drops tables from recycle after 30 days

This pattern gives you a safety net for accidental table removals while still allowing SchemaSmith to manage your schema declaratively.

For more details on custom hooks, see Forge Deeper with SchemaQuench and Customize and Configure.

Best Practices

Tag Releases in Source Control

Keep tagged releases of your product definition in Git. This makes it easy to retrieve any prior version for rollback without searching through deployment artifacts.

Document Data Dependencies

When adding columns or tables that will contain important data, document whether rollback requires data preservation scripts. Include this in your release notes.

Test Rollbacks Regularly

Practice rollback procedures in dev or staging on a regular cadence. When you need to roll back production, you'll already know the process works.

Consider Custom Hooks for Critical Tables

For tables where accidental data loss would be costly, implement the custom drop/restore hooks to add a safety buffer before permanent deletion.

Additional Resources

Also available for SQL Server and MySQL