Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 93 additions & 112 deletions context/goals/v1.data.extensions.md

Large diffs are not rendered by default.

42 changes: 36 additions & 6 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ role's, every check is on, and no source is registered.
```toml
# The engine whose export names the native forms, and the pattern sources.
engine = "github.com/standards-lab/sqlate/postgres"
# Modules whose exported native forms join the engine's; listing any requires an engine.
# extensions = ["github.com/example/sqlate-pgvector"]

[sources]
sql = "github.com/standards-lab/sqlate"
Expand Down Expand Up @@ -602,15 +604,18 @@ single_statement = true
[export]
patterns = "patterns"
# overlay = "overlay"
# An extension module that adds no syntax declares the table empty and says why:
# reason = "the extension adds functions, not syntax"
# native_forms = {}
# [export.native_forms]
# returning = '(?i)\bRETURNING\b'
```

A glob is slash-separated; each segment is a `path.Match` pattern and `**` matches any run of
segments. A source or the engine is a path: a directory of the tree, or a module path (its
first segment contains a dot). A producer, a module or a directory that contains its own
`sqlint.toml`, declares in `[export]` what a consumer reads: the directory its patterns
publish, the overlay directory an engine supplies, and the native forms an engine names. A
segments. A source, the engine, or an extension is a path: a directory of the tree, or a module
path (its first segment contains a dot). A producer, a module or a directory that contains its own
`sqlint.toml`, declares in `[export]` what a consumer reads: the directory its patterns publish,
the overlay directory an engine supplies, and the native forms an engine or an extension names. A
bare directory is the pattern files themselves. An engine is always a producer. The `sql`
namespace always resolves to the patterns the library embeds: the linter does not read the path
of a `sql` entry, and applies the entry's overlay, when one is declared, to those patterns.
Expand All @@ -622,14 +627,39 @@ and comments stripped first. The PostgreSQL module exports `returning`, `on_conf
`concurrently`, `limit`, `serial`, `jsonb`, `timestamptz`, `cast` (the `::` operator),
`pg_catalog` (any `pg_` function), `now`, and `uuidv7`.

`extensions` lists the modules whose native forms join the engine's. An extension module
exports its own forms in `[export.native_forms]`, or declares the table empty, `native_forms = {}`,
beside a `reason`, a string saying why it names none; a `reason` may also accompany forms.
`Load` refuses an `extensions` that is not a list of strings, an empty entry, an entry listed
twice, and any `extensions` while `engine` is unset, one line each.

`Lint` finds each entry the way it finds the engine: a module path through the `Resolver`, and
anything else as a directory of the tree. It accepts a module whose export names at least one
native form, or declares the table empty beside a non-empty `reason`. `Lint` reports each of these
failures against `sqlint.toml` and lints the rest of the tree with what resolved:

- an entry that does not resolve, or has no `sqlint.toml`: `extensions: <entry>: ` and the error;
- a refused module, one with no `native_forms` table or an empty table and no reason:
`extensions: <entry> exports neither native forms nor a reason`; it adds no forms;
- a form whose expression does not compile: `extensions: <entry>: native_forms.<name>: ` and the
error; the module's other forms still apply.

An accepted module's forms are matched exactly where the engine's are, and a finding from one names
the entry beside the form: `"<=>" (cosine_distance, from github.com/example/sqlate-pgvector) in a
standard-tier file; declare the tier native and name the port`. A name the engine and an extension
both export is matched and reported for each. A native file that uses an extension's syntax names
the extension in its free-text `native:` header, `native: postgres, pgvector — <=> cosine distance`;
the linter does not read that header, so review checks it. The linter cannot read Go values either,
so review keeps the `extensions` list in step with `migrate.Options.Extensions`.

### The checks

| Role | Check | Switch |
|---|---|---|
| statements | The directory compiles against the configured sources, the way a program compiles it: the header grammar, the parameter syntax, the field contract, includes resolving. | always |
| statements | A file is named for its operation, not its SQL verb (`insert_`, `select_`, `update_`, `upsert_`, `merge_`). | `verb_named` |
| statements, patterns | `{{` appears in no comment and no string literal. | `delimiter` |
| statements, patterns | A standard-tier file matches none of the engine's native forms. | `native_forms` |
| statements, patterns | A standard-tier file matches none of the native forms of the engine and the accepted extensions. | `native_forms` |
| statements | A statement that includes one of the guard protocol's patterns includes both: a statement with a `SET` list that includes `guard_where` also includes `guard_set`, and a statement that includes `guard_set` also includes `guard_where`. The guard's check and a guarded delete include `guard_where` alone. The include is matched by pattern name under any namespace. | `guard` |
| patterns | The directory validates as a catalog source: a tier on every file, a port on every native file, parameters only. | always |
| migrations | A file headed `transaction: none` contains exactly one statement. | `single_statement` |
Expand All @@ -641,7 +671,7 @@ and comments stripped first. The PostgreSQL module exports `returning`, `on_conf
renders `path:line: message`. A nil configuration is the defaults. The `Resolver` turns a
module path into the filesystem of the module's root; `GoList(root)` resolves through
`go list -m` in the module at `root`, to the version its `go.mod` pins, a workspace or replace
directive included. A source or engine that does not resolve is a finding against
directive included. A source, engine, or extension that does not resolve is a finding against
`sqlint.toml`, and the rest of the tree is linted with what did. Errors in the file's own
syntax are `Load`'s, one line each.

Expand Down
11 changes: 10 additions & 1 deletion postgres/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ changelog covers this sub-module only; the base module keeps its own.

## [Unreleased]

## [v0.9.0] - 2026-11-24

### Added

- `Dialect.ExtensionState`, `Dialect.ActivateExtension`, and `Dialect.RemoveExtension`, the
Expand All @@ -22,6 +24,12 @@ changelog covers this sub-module only; the base module keeps its own.
removal waits for a set migration to drop the column depending on it, `Reset` leaves an
extension installed, and two concurrent `Up` runs activate an extension once.

### Changed

- The `sqlate` requirement is v0.8.0, whose `migrate.ExtensionCatalog` the dialect implements.

Requires `github.com/standards-lab/sqlate v0.8.0`.

## [v0.8.0] - 2026-11-17

### Added
Expand Down Expand Up @@ -157,7 +165,8 @@ The first release of the PostgreSQL dialect, against `github.com/standards-lab/s
- The integration tier: the migrate and query proofs against a live engine, behind the
`integration` build tag.

[Unreleased]: https://github.com/standards-lab/sqlate/compare/postgres/v0.8.0...HEAD
[Unreleased]: https://github.com/standards-lab/sqlate/compare/postgres/v0.9.0...HEAD
[v0.9.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.8.0...postgres/v0.9.0
[v0.8.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.7.0...postgres/v0.8.0
[v0.7.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.6.0...postgres/v0.7.0
[v0.6.0]: https://github.com/standards-lab/sqlate/compare/postgres/v0.5.0...postgres/v0.6.0
Expand Down
2 changes: 1 addition & 1 deletion postgres/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.27

require (
github.com/jackc/pgx/v5 v5.13.0
github.com/standards-lab/sqlate v0.7.0
github.com/standards-lab/sqlate v0.8.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions postgres/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao
github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/standards-lab/sqlate v0.7.0 h1:GVb3OCDXwSS9RCxQ2BRIfV73zwBeJBhkvDLBJnTYd/U=
github.com/standards-lab/sqlate v0.7.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
github.com/standards-lab/sqlate v0.8.0 h1:G1uUIWmc2DxJz5YhtBvIlftfURnb3b2RKsYB59Wa03Y=
github.com/standards-lab/sqlate v0.8.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
22 changes: 22 additions & 0 deletions sqlint/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@ changelog covers this sub-module only; the base module keeps its own.

## [Unreleased]

### Added

- An `extensions` key in `sqlint.toml` lists the modules whose exported native forms join the
engine's, read into `Config.Extensions`. Each entry is found as the engine is, a module path
through the `Resolver` or a directory of the tree. `Load` refuses an `extensions` that is not a
list of strings, an empty entry, an entry listed twice, and any `extensions` without an `engine`.
- `[export] reason`, read into `Export.Reason`: an extension module that names no forms declares
`native_forms = {}` beside a reason saying why.
- `Lint` reports against `sqlint.toml` an entry that does not resolve or has no `sqlint.toml`
(`extensions: <entry>: …`), a module that exports neither native forms nor a reason
(`extensions: <entry> exports neither native forms nor a reason`), and a form whose
expression does not compile (`extensions: <entry>: native_forms.<name>: …`).
- A finding from an extension's form names the entry beside the form:
`"<=>" (cosine_distance, from <entry>) in a standard-tier file; declare the tier native and
name the port`. Findings from the engine's forms are unchanged.

### Changed

- The `sqlate` requirement is v0.8.0.

Requires `github.com/standards-lab/sqlate v0.8.0`.

## [v0.4.0] - 2026-11-11

### Changed
Expand Down
9 changes: 8 additions & 1 deletion sqlint/body.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (l *linter) lintBody(p, text string, end int, delimiter, forms, guard bool)
if forms {
for _, f := range l.forms {
if m := f.re.FindString(code); m != "" {
l.report(p, line, fmt.Sprintf("%q (%s) in a standard-tier file; declare the tier native and name the port", m, f.name))
l.report(p, line, fmt.Sprintf("%q (%s) in a standard-tier file; declare the tier native and name the port", m, f.label()))
}
}
}
Expand Down Expand Up @@ -96,3 +96,10 @@ func codeOnly(line string, inBlock bool) (string, bool) {
}
return b.String(), inBlock
}

func (f form) label() string {
if f.from == "" {
return f.name
}
return f.name + ", from " + f.from
}
40 changes: 37 additions & 3 deletions sqlint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Config struct {
// Engine is the path of the engine whose export declares the native
// forms; empty means the native-forms check has no list.
Engine string
// Extensions are the module paths whose exported native forms join
// the engine's; listing any requires an engine.
Extensions []string
// Sources maps a namespace to the source registered under it.
Sources map[string]Source
// Roles maps each role's name to the role.
Expand All @@ -39,13 +42,17 @@ type Source struct {

// Export is a producer's declaration: the directory its namespace
// publishes, the overlay directory an engine supplies, and the native
// forms an engine names, each a regular expression under the name the
// finding reports, so the engine states in what position a spelling
// counts, word boundaries and case, and not only which spelling.
// forms an engine or an extension names, each a regular expression under
// the name the finding reports, so the producer states in what position a
// spelling counts, word boundaries and case, and not only which spelling.
// NativeForms is nil when the export declares no native_forms table, and
// empty, not nil, when it declares the table empty; Reason is the
// producer's account of why it names the forms it does, or none.
type Export struct {
Patterns string `toml:"patterns"`
Overlay string `toml:"overlay"`
NativeForms map[string]string `toml:"native_forms"`
Reason string `toml:"reason"`
}

// Role is one role's directory globs and switches, with any per-glob
Expand Down Expand Up @@ -76,6 +83,7 @@ func defaults() *Config {

type raw struct {
Engine string `toml:"engine"`
Extensions toml.Primitive `toml:"extensions"`
Sources map[string]toml.Primitive `toml:"sources"`
Statements map[string]toml.Primitive `toml:"statements"`
Patterns map[string]toml.Primitive `toml:"patterns"`
Expand Down Expand Up @@ -124,6 +132,12 @@ func parseConfig(text string) (*Config, error) {
}
c.Sources[ns] = s
}
if md.IsDefined("extensions") {
c.Extensions = parseExtensions(md, r.Extensions, fail)
if r.Engine == "" {
fail("extensions: listed without an engine")
}
}
for name, table := range map[string]map[string]toml.Primitive{"statements": r.Statements, "patterns": r.Patterns, "migrations": r.Migrations} {
if table == nil {
continue
Expand All @@ -138,6 +152,26 @@ func parseConfig(text string) (*Config, error) {
return c, errors.Join(errs...)
}

func parseExtensions(md toml.MetaData, prim toml.Primitive, fail func(string, ...any)) []string {
var list []string
if err := md.PrimitiveDecode(prim, &list); err != nil {
fail("extensions: a list of module paths")
return nil
}
var out []string
for i, ext := range list {
switch {
case ext == "":
fail("extensions[%d]: empty", i)
case slices.Contains(out, ext):
fail("extensions[%d]: %q is listed twice", i, ext)
default:
out = append(out, ext)
}
}
return out
}

func parseRole(md toml.MetaData, name string, table map[string]toml.Primitive, role *Role) error {
var errs []error
fail := func(format string, args ...any) { errs = append(errs, fmt.Errorf(File+": "+name+": "+format, args...)) }
Expand Down
4 changes: 2 additions & 2 deletions sqlint/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// - a file is named for its operation and not its SQL verb
// - the parameter delimiter does not appear inside a comment or a string
// literal
// - a standard-tier file uses no native form the configured engine
// declares
// - a standard-tier file uses no native form the configured engine or
// an accepted extension declares
// - a statement that includes one of the guard protocol's patterns
// includes both: guard_set beside guard_where in a SET statement, and
// guard_where beside guard_set
Expand Down
2 changes: 1 addition & 1 deletion sqlint/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.27

require (
github.com/BurntSushi/toml v1.6.0
github.com/standards-lab/sqlate v0.7.0
github.com/standards-lab/sqlate v0.8.0
)
4 changes: 2 additions & 2 deletions sqlint/go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/standards-lab/sqlate v0.7.0 h1:GVb3OCDXwSS9RCxQ2BRIfV73zwBeJBhkvDLBJnTYd/U=
github.com/standards-lab/sqlate v0.7.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
github.com/standards-lab/sqlate v0.8.0 h1:G1uUIWmc2DxJz5YhtBvIlftfURnb3b2RKsYB59Wa03Y=
github.com/standards-lab/sqlate v0.8.0/go.mod h1:pikNmGf/z3x46SBWFXgt/xKIDZuquSgCoXy5OWBFFR4=
52 changes: 40 additions & 12 deletions sqlint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ type linter struct {

type form struct {
name string
from string
re *regexp.Regexp
}

// Lint walks fsys under cfg and returns every finding, in walk order. A
// nil cfg is the defaults. resolve turns a module path into its root
// filesystem; nil refuses every module path. A source or engine that does
// not resolve is a finding against File; the rest of the tree is linted
// with what did.
// not resolve is a finding against File, as is an extension that does
// not resolve or exports neither native forms nor a reason; the rest of
// the tree is linted with what did.
func Lint(fsys fs.FS, cfg *Config, resolve Resolver) []Finding {
if cfg == nil {
cfg = defaults()
Expand All @@ -57,6 +59,7 @@ func Lint(fsys fs.FS, cfg *Config, resolve Resolver) []Finding {
l := &linter{fsys: fsys, cfg: cfg, resolve: resolve}
l.resolveSources()
l.resolveEngine()
l.resolveExtensions()
_ = fs.WalkDir(fsys, ".", func(p string, d fs.DirEntry, err error) error {
if err != nil || !d.IsDir() {
return nil
Expand Down Expand Up @@ -166,27 +169,52 @@ func (l *linter) resolveEngine() {
if l.cfg.Engine == "" {
return
}
fsys, base, err := l.locate(l.cfg.Engine)
if export, ok := l.producer("engine: ", l.cfg.Engine); ok {
l.addForms("engine: ", "", export.NativeForms)
}
}

func (l *linter) resolveExtensions() {
for _, ext := range l.cfg.Extensions {
prefix := "extensions: " + ext + ": "
export, ok := l.producer(prefix, ext)
if !ok {
continue
}
if export.NativeForms == nil || (len(export.NativeForms) == 0 && export.Reason == "") {
l.report(File, 0, fmt.Sprintf("extensions: %s exports neither native forms nor a reason", ext))
continue
}
l.addForms(prefix, ext, export.NativeForms)
}
}

func (l *linter) producer(prefix, value string) (Export, bool) {
fsys, base, err := l.locate(value)
if err != nil {
l.report(File, 0, fmt.Sprintf("engine: %v", err))
return
l.report(File, 0, prefix+err.Error())
return Export{}, false
}
export, err := readExport(fsys, base)
if err != nil {
l.report(File, 0, fmt.Sprintf("engine: %v", err))
return
l.report(File, 0, prefix+err.Error())
return Export{}, false
}
names := make([]string, 0, len(export.NativeForms))
for name := range export.NativeForms {
return export, true
}

func (l *linter) addForms(prefix, from string, forms map[string]string) {
names := make([]string, 0, len(forms))
for name := range forms {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
re, err := regexp.Compile(export.NativeForms[name])
re, err := regexp.Compile(forms[name])
if err != nil {
l.report(File, 0, fmt.Sprintf("engine: native_forms.%s: %v", name, err))
l.report(File, 0, fmt.Sprintf("%snative_forms.%s: %v", prefix, name, err))
continue
}
l.forms = append(l.forms, form{name: name, re: re})
l.forms = append(l.forms, form{name: name, from: from, re: re})
}
}
Loading