Documentation
¶
Overview ¶
Package compiler provides public APIs for compiling OpenFGA schemas to SQL.
It is a thin wrapper around lib/sqlgen that exposes only the types and functions needed by external consumers. The package covers two concerns:
- Check and list SQL generation: AnalyzeRelations, GenerateSQL, GenerateListSQL, CollectFunctionNames, CollectNamedFunctions.
- Migration file generation: GenerateMigrationSQL and MigrationOptions, which assemble versioned UP/DOWN SQL files from already-compiled output.
For applying generated SQL directly to a database, use pkg/migrator instead.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AnalyzeRelations = sqlgen.AnalyzeRelations
AnalyzeRelations classifies all relations and gathers data needed for SQL generation.
var BuildInlineSQLData = sqlgen.BuildInlineSQLData
BuildInlineSQLData builds inline SQL data from closure and analyses.
var CollectFunctionNames = sqlgen.CollectFunctionNames
CollectFunctionNames returns all generated function names for tracking.
var CollectNamedFunctions = sqlgen.CollectNamedFunctions
CollectNamedFunctions returns specialized functions paired with their SQL.
var ComputeCanGenerate = sqlgen.ComputeCanGenerate
ComputeCanGenerate computes which relations can have functions generated.
var GenerateListSQL = sqlgen.GenerateListSQL
GenerateListSQL generates specialized list functions from relation analyses.
var GenerateSQL = sqlgen.GenerateSQL
GenerateSQL generates specialized check_permission functions from relation analyses.
Functions ¶
This section is empty.
Types ¶
type GeneratedSQL ¶
type GeneratedSQL = sqlgen.GeneratedSQL
GeneratedSQL contains all SQL generated from a schema for check functions.
type InlineSQLData ¶
type InlineSQLData = sqlgen.InlineSQLData
InlineSQLData contains inline SQL data for generated functions.
type ListGeneratedSQL ¶
type ListGeneratedSQL = sqlgen.ListGeneratedSQL
ListGeneratedSQL contains all SQL generated for list functions.
type MigrationOptions ¶ added in v0.7.4
type MigrationOptions struct {
// Version is the melange CLI/library version (e.g., "v0.7.3").
Version string
// SchemaChecksum is the SHA256 of the current schema content.
SchemaChecksum string
// CodegenVersion is the codegen version string.
CodegenVersion string
// PreviousFunctionNames enables orphan-aware output when non-nil.
PreviousFunctionNames []string
// PreviousSource describes where previous state came from (for header comment).
// e.g., "database", "git:abc1234", "file:old.fga"
PreviousSource string
// PreviousChecksums maps function_name → SHA256(sql_body) from the previous state.
// When set alongside NamedFunctions, only functions whose SQL body has changed (or
// are new) are included in the UP migration. Dispatchers are always included
// regardless. If nil, all specialized functions are emitted unconditionally.
PreviousChecksums map[string]string
// NamedFunctions pairs each specialized function name with its SQL body for
// checksum comparison. Required alongside PreviousChecksums for change detection;
// if either is absent, change detection is skipped and all functions are emitted.
NamedFunctions []NamedFunction
}
MigrationOptions controls migration SQL generation.
type MigrationSQL ¶ added in v0.7.4
MigrationSQL holds the UP and DOWN SQL for a single versioned migration. UP contains CREATE OR REPLACE statements for all current functions, plus DROP statements for any orphaned functions detected via comparison mode. DOWN drops all functions installed by UP; restoring a prior state requires re-applying that version's UP migration.
func GenerateMigrationSQL ¶ added in v0.7.4
func GenerateMigrationSQL( generatedSQL GeneratedSQL, listSQL ListGeneratedSQL, expectedFunctions []string, opts MigrationOptions, ) MigrationSQL
GenerateMigrationSQL is the terminal step of the generate migration pipeline. It assembles UP and DOWN SQL from functions already compiled by GenerateSQL and GenerateListSQL. Call CollectFunctionNames and CollectNamedFunctions first to populate expectedFunctions and opts.NamedFunctions respectively.
When opts.PreviousFunctionNames is nil the output includes every function (full mode). When set, orphaned functions are dropped in UP and checksum-based filtering is applied if opts.PreviousChecksums is also provided.
type NamedFunction ¶ added in v0.7.4
type NamedFunction = sqlgen.NamedFunction
NamedFunction pairs a function name with its generated SQL body.
type RelationAnalysis ¶
type RelationAnalysis = sqlgen.RelationAnalysis
RelationAnalysis contains the analyzed features of a relation.