extract

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collected

type Collected struct {
	Rows map[string][]Row
}

Collected groups extracted rows by table name. Ordering for emission is the caller's responsibility (see internal/plan.Build).

func Walk

func Walk(
	ctx context.Context,
	db *sql.DB,
	d dialect.Dialect,
	schema introspect.Schema,
	seedTable, whereClause string,
	dir Direction,
) (*Collected, error)

Walk extracts rows from a seed (seedTable + whereClause) and recursively collects the FK-connected closure in the given direction: parents for Forward (clone), referencing children for Backward (delete).

An empty whereClause selects every row in seedTable (no WHERE filter is appended). The CLI rejects empty filters; this is for callers that want to operate on the full contents of a small lookup table.

Composite primary keys, composite foreign keys, and self-referencing FKs are all supported. Within a single table, callers order the row slice with SortByPK: emit it as-is for Forward (referenced rows first), reversed for Backward (referencing rows first).

type Direction

type Direction int

Direction selects which way Walk traverses the FK graph from the seed.

const (
	// Forward collects the FK parents a row references (clone). Emit the
	// result parents-first so every INSERT lands after its referenced rows.
	Forward Direction = iota
	// Backward collects the rows that reference a row (delete). Emit the
	// result most-dependent-first so every DELETE runs before the row it
	// depends on.
	Backward
)

type Row

type Row = map[string]any

Row maps column names to scanned values for one extracted row.

func SortByPK

func SortByPK(rows []Row, table introspect.Table) ([]Row, error)

SortByPK reorders rows so that any self-referencing FK is satisfied at apply time: a row whose FK column points to another row in the same slice will appear after its parent. Tables without a self-ref FK are returned unchanged.

v0.1 supports single-column self-ref FKs against single-column PKs. Composite self-ref FKs or composite PKs return an error. Cyclic data (e.g., A -> B -> A) also returns an error: the apply step would need a DEFERRABLE constraint to succeed, which is out of scope.

Jump to

Keyboard shortcuts

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