sqlddl

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package sqlddl reads SQL migrations without running them: a lexer splits statements, and a replay applies the DDL that shapes tables and columns to an in-memory schema. PostgreSQL first; MySQL quoting is tolerated. Statements it does not know are skipped, never guessed. See https://cernly.dev/how/plugins/.

Index

Constants

View Source
const (
	MySQL      = "mysql"
	PostgreSQL = "postgresql"
	SQLServer  = "sqlserver" // a Set only: the EF Core reader's migrationBuilder.Sql
)

Variables

This section is empty.

Functions

func Replay

func Replay(files []File, hint Dialect) (tables []*Table, d Dialect, problems []Problem)

Replay applies a migration set's files in order and returns the tables they leave, sorted by name. hint is the dialect when the SQL itself has no signal (a Prisma migration_lock.toml); the SQL's own signal wins. Only a PostgreSQL set puts unqualified tables in public and says whether row-level security is on.

func Shapes

func Shapes(stmts []Stmt) bool

Shapes reports whether a file's statements shape tables: CREATE TABLE (not temporary), ALTER, DROP or RENAME TABLE, or CREATE TYPE … AS ENUM. A .sql file without any is a query, a seed or a function, not a migration.

func SplitSet

func SplitSet(srcs [][]byte) ([][]Stmt, []error)

SplitSet lexes a migration set's files. A set that is MySQL-shaped (backticks, an ENGINE= option, a line starting with #, or a file only MySQL's lexing reads) is lexed again as MySQL: backslash escapes in quotes and # line comments.

Types

type Column

type Column struct {
	Name     string
	Type     string // as written, lower case, spacing normalised; a quoted type name kept as written
	NotNull  bool
	Primary  bool
	Unique   bool
	Enum     bool
	Evidence []Ev
}

type Dialect

type Dialect struct {
	Name   string // "mysql", "postgresql" or ""
	Signal string // what was seen, e.g. "CREATE EXTENSION"
	File   string
	Line   int
}

Dialect is what a migration set was read as, and the first statement that says so: MySQL, PostgreSQL, or neither ("": table names stay as written, no row-level security).

func Detect

func Detect(files []File) Dialect

Detect reads a set's dialect from the SQL itself. MySQL: backticks, a CREATE TABLE … ENGINE= option, or lexed as MySQL by SplitSet. PostgreSQL: a schema-qualified table, CREATE EXTENSION, row-level security, a $$ body, a SERIAL type or a :: cast. The first statement with a signal decides within each kind; MySQL wins over PostgreSQL.

type Ev

type Ev struct {
	File      string
	Line, End int
}

Ev is a place in a migration file that shaped a table or column.

func AddEv

func AddEv(evs []Ev, ev Ev) []Ev

AddEv appends ev unless evs already holds one at the same file and line.

type File

type File struct {
	Path  string
	Stmts []Stmt
}

File is one migration file's statements, in the order the set applies them.

type Problem

type Problem struct {
	File string
	Line int
	Msg  string
}

Problem is a statement the replay could not read.

type Set

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

Set is a replay that a caller drives statement by statement and whose tables it changes in between: the EF Core reader replays migrationBuilder.Sql literals into the tables its own operations built.

func NewSet

func NewSet(dialect, defaultSchema string) *Set

NewSet starts an empty set read as dialect (MySQL, PostgreSQL, SQLServer or ""). defaultSchema qualifies unqualified names outside PostgreSQL (dbo for SQL Server); a MySQL set has no schemas. A SQLServer set finds tables and columns without regard to case, as SQL Server's default collation does, when exactly one matches so.

func (*Set) Exec

func (s *Set) Exec(file string, line int, stmts []Stmt) (problems []Problem, unread bool)

Exec applies stmts from file with every statement placed at line: SQL held in a string of another language has no lines of its own. unread is true when a statement that shapes tables could not be read, or names a table or column the set does not hold, so it changed nothing.

func (*Set) Split

func (s *Set) Split(src []byte) ([]Stmt, error)

Split lexes SQL for this set: T-SQL for a SQLServer set ([bracketed] names, unquoted names as written), otherwise as SplitSet lexes one file.

func (*Set) Tables

func (s *Set) Tables() map[string]*Table

Tables are the set's tables by Qualified name. The caller may add, change and remove them, keeping each under its Qualified name.

type Stmt

type Stmt struct {
	Line, End int
	// contains filtered or unexported fields
}

Stmt is one statement: its tokens, without comments, and its first and last line.

func Split

func Split(src []byte) ([]Stmt, error)

Split lexes a file into statements. It stops at a down marker. Strings follow PostgreSQL: a backslash is literal except in E'…'.

type SyntaxError

type SyntaxError struct {
	Line int
	Msg  string
}

SyntaxError is a lexing failure at a line.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type Table

type Table struct {
	Schema   string // "" when the set is MySQL-shaped
	Name     string
	RLS      bool
	Columns  []*Column
	Evidence []Ev
}

func (*Table) Qualified

func (t *Table) Qualified() string

Qualified is schema.table, or the table alone without a schema.

Jump to

Keyboard shortcuts

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