Skip to main content

Module mod_rewriter

Module mod_rewriter 

Source
Expand description

AST-based rewriting of database and schema names in mod statements.

When a profile suffix is active, database names in mod files must be suffixed (e.g., app -> app_dev). When a staging suffix is active, schema names must be suffixed (e.g., public -> public_staging).

This module provides rewrite_database_names and rewrite_schema_names, which apply these transformations at the AST level using the auto-generated VisitMut traversal. This is safer than raw text substitution because it only touches actual identifier nodes, not string literals or comments.

ยงSupported Statement Types

The visitor handles all statement types permitted in mod files:

  • COMMENT ON DATABASE/SCHEMA
  • GRANT ... ON DATABASE/SCHEMA
  • ALTER DEFAULT PRIVILEGES IN DATABASE/SCHEMA

The auto-generated traversal uses two separate hooks for database/schema names depending on AST position:

  • visit_database_name_mut / visit_schema_name_mut โ€” associated type hooks used by COMMENT ON DATABASE/SCHEMA and ALTER DEFAULT PRIVILEGES
  • visit_unresolved_database_name_mut / visit_unresolved_schema_name_mut โ€” concrete struct hooks used by GRANT ON DATABASE/SCHEMA (via UnresolvedObjectName::Database/Schema)

Both hooks receive &mut UnresolvedDatabaseName / &mut UnresolvedSchemaName, so the rewriting logic is shared via a helper function. Both hooks must be overridden to cover all mod statement types.

Structsยง

DatabaseNameRewriter ๐Ÿ”’
Visitor that rewrites database name identifiers by appending a suffix.
SchemaNameRewriter ๐Ÿ”’
Visitor that rewrites schema name identifiers by appending a suffix.

Functionsยง

rewrite_database_name ๐Ÿ”’
Append suffix to the database name if it matches database_name.
rewrite_database_names ๐Ÿ”’
Rewrite database names in parsed mod statements by appending a suffix.
rewrite_schema_name ๐Ÿ”’
Append suffix to the schema part (last ident) if it matches schema_name.
rewrite_schema_names ๐Ÿ”’
Rewrite schema names in parsed mod statements by appending a suffix.