sqlschema

package
v0.0.0-...-3776344 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultPostgresCredentials

func DefaultPostgresCredentials() (user, password, database string)

DefaultPostgresCredentials returns the default local dev credentials for PostgreSQL containers.

func FormatConnectionString

func FormatConnectionString(host, port, user, password, database string) string

FormatConnectionString builds a PostgreSQL connection string from components.

func HasSqlDatabaseResources

func HasSqlDatabaseResources(bp *schema.Blueprint) bool

HasSqlDatabaseResources returns true if the blueprint contains any celerity/sqlDatabase resources.

func ResourceType

func ResourceType() string

ResourceTypeSqlDatabase is the blueprint resource type for SQL databases. Exported for use by the compose generator.

Types

type Applier

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

Applier applies SQL schemas and migration scripts to a database. It tracks applied migrations in a celerity_schema_migrations table. Engine-specific SQL is delegated to a dialect implementation.

func NewApplier

func NewApplier(engine string, connStr string, logger *zap.Logger) (*Applier, error)

NewApplier creates an Applier for the given engine and connection string.

func (*Applier) ApplyAll

func (a *Applier) ApplyAll(ctx context.Context, schemaPath, migrationsPath, engine string) error

ApplyAll applies schema DDL (up) followed by pending migration up scripts.

func (*Applier) ApplyMigrations

func (a *Applier) ApplyMigrations(ctx context.Context, migrationsPath string) error

ApplyMigrations discovers and executes pending up migration scripts in version order.

func (*Applier) ApplySchema

func (a *Applier) ApplySchema(ctx context.Context, schemaPath string, engine string) error

ApplySchema parses a schema YAML file and executes the up DDL statements.

func (*Applier) Close

func (a *Applier) Close() error

Close releases the database connection.

func (*Applier) EnsureDatabase

func (a *Applier) EnsureDatabase(ctx context.Context, dbName string) error

EnsureDatabase creates a database if it does not already exist. Must be called on an Applier connected to the admin database.

func (*Applier) ExecSQL

func (a *Applier) ExecSQL(ctx context.Context, sqlContent string) error

ExecSQL executes a raw SQL string against the database.

func (*Applier) ForDatabase

func (a *Applier) ForDatabase(dbName string) (*Applier, error)

ForDatabase returns a new Applier connected to a specific database, derived from the original connection string by replacing the database path.

func (*Applier) RollbackAll

func (a *Applier) RollbackAll(ctx context.Context, schemaPath, migrationsPath, engine string) error

RollbackAll rolls back applied migrations (down, reverse order) then schema DDL (down).

func (*Applier) RollbackMigrations

func (a *Applier) RollbackMigrations(ctx context.Context, migrationsPath string) error

RollbackMigrations discovers and executes down migration scripts in reverse version order.

func (*Applier) RollbackSchema

func (a *Applier) RollbackSchema(ctx context.Context, schemaPath string, engine string) error

RollbackSchema parses a schema YAML file and executes the down DDL statements.

type Column

type Column struct {
	Type           string      `yaml:"type"`
	PrimaryKey     bool        `yaml:"primaryKey"`
	Nullable       bool        `yaml:"nullable"`
	Unique         bool        `yaml:"unique"`
	Default        string      `yaml:"default"`
	Description    string      `yaml:"description"`
	Classification string      `yaml:"classification"`
	References     *ForeignKey `yaml:"references"`
}

Column describes a single table column with engine-native type.

type Constraint

type Constraint struct {
	Name       string   `yaml:"name"`
	Type       string   `yaml:"type"`
	Expression string   `yaml:"expression"`
	Columns    []string `yaml:"columns"`
}

Constraint describes a table constraint (check, unique, composite foreign key).

type DDLResult

type DDLResult struct {
	Up   []string
	Down []string
}

DDLResult holds both up (create) and down (drop) DDL statements.

func GenerateDDL

func GenerateDDL(s *Schema, engine string) (*DDLResult, error)

GenerateDDL produces ordered up and down DDL statements from a parsed schema. The engine parameter is validated (v0: "postgres" only).

Up statements are in dependency order: 1. Extensions 2. Tables (CREATE TABLE) 3. Foreign keys (ALTER TABLE ADD CONSTRAINT) 4. Indexes (CREATE INDEX) 5. Check constraints

Down statements are in reverse dependency order: 1. Check constraints (DROP) 2. Indexes (DROP) 3. Foreign keys (DROP) 4. Tables (DROP) — reverse alphabetical 5. Extensions (DROP)

type DatabaseResource

type DatabaseResource struct {
	ResourceName   string
	Name           string
	Engine         string
	SchemaPath     string
	MigrationsPath string
	AuthMode       string
}

DatabaseResource holds the resolved configuration for a celerity/sqlDatabase blueprint resource. Paths are resolved to absolute paths.

func CollectDatabaseResources

func CollectDatabaseResources(bp *schema.Blueprint, projectRoot string) []DatabaseResource

CollectDatabaseResources extracts celerity/sqlDatabase resources from a blueprint and resolves schema/migration paths relative to the project root.

type ForeignKey

type ForeignKey struct {
	Table    string `yaml:"table"`
	Column   string `yaml:"column"`
	OnDelete string `yaml:"onDelete"`
}

ForeignKey describes a foreign key reference from a column.

type Index

type Index struct {
	Name    string   `yaml:"name"`
	Columns []string `yaml:"columns"`
	Unique  bool     `yaml:"unique"`
}

Index describes a table index.

type MigrationScript

type MigrationScript struct {
	Version     int
	Description string
	UpPath      string
	DownPath    string
}

MigrationScript represents a versioned SQL migration with up/down files (V<number>__<description>.up.sql / V<number>__<description>.down.sql).

func DiscoverMigrationScripts

func DiscoverMigrationScripts(migrationsPath string) ([]MigrationScript, error)

DiscoverMigrationScripts finds versioned SQL migration pairs (V<number>__<description>.up.sql / V<number>__<description>.down.sql) in the given directory, sorted by version number.

type Schema

type Schema struct {
	Tables     map[string]Table `yaml:"tables"`
	Extensions []string         `yaml:"extensions"`
}

Schema represents a parsed SQL database schema YAML file. Column types are engine-native (e.g. "serial", "varchar(50)", "jsonb").

func ParseSchema

func ParseSchema(r io.Reader) (*Schema, error)

ParseSchema parses a SQL schema YAML from a reader.

func ParseSchemaFile

func ParseSchemaFile(path string) (*Schema, error)

ParseSchemaFile reads and parses a SQL schema YAML file from disk.

type Table

type Table struct {
	Description string            `yaml:"description"`
	Columns     map[string]Column `yaml:"columns"`
	Indexes     []Index           `yaml:"indexes"`
	Constraints []Constraint      `yaml:"constraints"`
}

Table describes a single database table.

Jump to

Keyboard shortcuts

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