normalize

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package normalize rewrites a migration history so that the consolidation pipeline, which groups statements by object and emits them by category, never has to order statements around a rename or a dropped schema.

The history is read twice. The first pass simulates it to learn the name every object ends with; the second rewrites each statement so that it names objects by those final names:

  • Schemas, types (enums, composites, domains), views, materialized views, sequences and indexes are created under their final names, and the statements that renamed them (ALTER ... RENAME, SET SCHEMA, RENAME VALUE, RENAME ATTRIBUTE) are taken out.
  • Tables keep the statements that create and alter them as written, with their renames: PostgreSQL derives the names of a table's constraints, indexes and sequences from the table and column names at the moment it creates them, and a baseline that created the table under its final name would name them differently. Those statements are tracked under the table's final name, so the pipeline emits them together and in order; every other statement names the table and its columns as they end up, so it can run after that block. An index created without a name gets the name PostgreSQL gave it, spelled out.
  • Schemas leave the pipeline: CREATE SCHEMA, and the ALTER SCHEMA ... RENAME and DROP SCHEMA of schemas the history did not create, make the baseline's SCHEMAS section (SchemasSQL). A schema the history creates and drops, with DROP SCHEMA ... CASCADE or once empty, disappears with everything created in it.

What cannot be rewritten is reported as a warning (Warnings) rather than guessed; validation then shows the consequence.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Normalizer

type Normalizer struct {
	// contains filtered or unexported fields
}

Normalizer runs the two passes. Call Observe for every migration in history order, then Finish, then Rewrite for every migration again in the same order.

func New

func New() *Normalizer

New returns a normalizer ready for the first pass.

func (*Normalizer) Finish

func (n *Normalizer) Finish()

Finish ends the first pass.

func (*Normalizer) Observe

func (n *Normalizer) Observe(statements []types.Statement)

Observe feeds one migration's statements to the first pass.

func (*Normalizer) Rewrite

func (n *Normalizer) Rewrite(statements []types.Statement) ([]types.Statement, error)

Rewrite returns one migration's statements rewritten to final names, for the consolidation pipeline. Statements that only rename, and schema statements, are left out.

func (*Normalizer) SchemasSQL

func (n *Normalizer) SchemasSQL() string

SchemasSQL returns the SCHEMAS section of the baseline: the renames and drops of schemas the history did not create, in history order, then the schemas the history creates and keeps, under their final names.

func (*Normalizer) Warnings

func (n *Normalizer) Warnings() []string

Warnings lists what the rewrite could not carry, sorted.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL