cmdext

package
v0.0.0-...-a0fccc6 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 39 Imported by: 0

Documentation

Overview

Package cmdext provides extensions to the Atlas configuration file such as schema loaders, data sources and cloud connectors.

Index

Constants

View Source
const (
	SchemaTypeFile  = "file"
	SchemaTypeAtlas = "atlas"
)

Schema reader types (URL schemes).

View Source
const (
	FileTypeHCL  = ".hcl"
	FileTypeSQL  = ".sql"
	FileTypeTest = ".test.hcl"
)

File extensions supported by the file driver.

Variables

View Source
var SpecOptions = append(
	[]schemahcl.Option{
		schemahcl.WithDataSource("sql", Query),
		schemahcl.WithDataSource("external", External),
		schemahcl.WithDataSource("runtimevar", RuntimeVar),
		schemahcl.WithDataSource("template_dir", TemplateDir),
		schemahcl.WithDataSource("remote_dir", RemoteDir),
		schemahcl.WithDataSource("remote_schema", RemoteSchema),
		schemahcl.WithDataSource("hcl_schema", SchemaHCL),
		schemahcl.WithDataSource("external_schema", SchemaExternal),
		schemahcl.WithDataSource("aws_rds_token", AWSRDSToken),
		schemahcl.WithDataSource("gcp_cloudsql_token", GCPCloudSQLToken),
	},
	specOptions...,
)

SpecOptions exposes the schema spec options like data-sources provided by this package.

View Source
var (
	// States is a global registry for external state loaders.
	States = registry{
		"ent": EntLoader{},
		"mem": memLoader,
	}
)

Functions

func AWSRDSToken

func AWSRDSToken(ctx context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error)

AWSRDSToken exposes an AWS RDS token as a schemahcl datasource.

data "aws_rds_token" "token" {
	endpoint = "db.hostname.io:3306"
	region   = "us-east-1"
	username = "admin"
	profile  = "prod-ext"
}

func External

func External(_ context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error)

External allows loading data using external program execution.

data "external" "env1" {
  program = [
    "node",
    loadenv.js",
  ]
}

data "external" "env2" {
  program = [
    "bash",
    "-c",
    "env_to_json --file=${var.envfile} | jq '...' ",
  ]
}

func FilesAsDir

func FilesAsDir(files ...migrate.File) (migrate.Dir, error)

FilesAsDir wraps the given files as MemDir.

func FilesExt

func FilesExt(urls []*url.URL) (string, error)

FilesExt returns the file extension of the given URLs. Note, all URL must have the same extension.

func GCPCloudSQLToken

func GCPCloudSQLToken(ctx context.Context, _ *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error)

GCPCloudSQLToken exposes a CloudSQL token as a schemahcl datasource.

data "gcp_cloudsql_token" "hello" {}

func Query

func Query(ctx context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error)

Query exposes the database/sql.Query as a schemahcl datasource.

data "sql" "tenants" {
  url = var.url
  query = <query>
  args = [<arg1>, <arg2>, ...]
}

env "prod" {
  for_each = toset(data.sql.tenants.values)
  url      = urlsetpath(var.url, each.value)
}

func RemoteDir

RemoteDir is a data source that reads a remote migration directory.

func RemoteSchema

RemoteSchema is a data source that for reading remote schemas.

func RuntimeVar

func RuntimeVar(ctx context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error)

RuntimeVar exposes the gocloud.dev/runtimevar as a schemahcl datasource.

data "runtimevar" "pass" {
  url = "driver://path?query=param"
}

locals {
  url = "mysql://root:${data.runtimevar.pass}@:3306/"
}

func SchemaExternal

func SchemaExternal(context.Context, *hcl.EvalContext, *hclsyntax.Block) (cty.Value, error)

SchemaExternal is a data source that for reading external schemas.

func SchemaHCL

func SchemaHCL(_ context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error)

SchemaHCL is a data source that reads an Atlas HCL schema file(s), evaluates it with the given variables and exposes its resulting schema as in-memory HCL file.

func TemplateDir

func TemplateDir(_ context.Context, ectx *hcl.EvalContext, block *hclsyntax.Block) (cty.Value, error)

TemplateDir implements migrate.Dir interface for template directories.

data "template_dir" "name" {
  path = "path/to/directory"
  vars = {
    Env  = atlas.env
    Seed = var.seed
  }
}

env "dev" {
  url = "driver://path?query=param"
  migration {
    dir = data.template_dir.name.url
  }
}

func UnsupportedErr

func UnsupportedErr(feature string) error

Types

type AtlasConfig

type AtlasConfig struct {
	Client  *cloudapi.Client // Client attached to Atlas Cloud.
	Token   string           // User token.
	Org     string           // Organization to connect to.
	Project string           // Optional project.
}

AtlasConfig exposes non-sensitive information returned by the "atlas" init-block. By invoking AtlasInitBlock() a new config is returned that is set by the init block defined and executed on schemahcl Eval functions.

func (*AtlasConfig) InitBlock

func (c *AtlasConfig) InitBlock() schemahcl.Option

InitBlock returns the handler for the "atlas" init block.

type EntLoader

type EntLoader struct{}

EntLoader is a StateLoader for loading ent.Schema's as StateReader's.

func (EntLoader) LoadState

LoadState returns a migrate.StateReader that reads the schema from an ent.Schema.

func (EntLoader) MigrateDiff

MigrateDiff returns the diff between ent.Schema and a directory.

type MemLoader

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

MemLoader is a StateLoader for loading data-sources that were loaded into program memory.

func (MemLoader) LoadState

func (l MemLoader) LoadState(ctx context.Context, config *StateReaderConfig) (*StateReadCloser, error)

LoadState loads the state loaded from data-sources into memory.

type MigrateDiffOptions

type MigrateDiffOptions struct {
	Name    string
	Indent  string
	To      []string
	Dir     migrate.Dir
	Dev     *sqlclient.Client
	Options []schema.DiffOption
}

MigrateDiffOptions for external migration differ.

type MigrateDiffer

type MigrateDiffer interface {
	MigrateDiff(context.Context, *MigrateDiffOptions) error
	// contains filtered or unexported methods
}

MigrateDiffer allows external sources to implement custom migration differs.

type StateLoader

type StateLoader interface {
	LoadState(context.Context, *StateReaderConfig) (*StateReadCloser, error)
}

StateLoader allows loading StateReader's from external sources.

type StateLoaderFunc

type StateLoaderFunc func(context.Context, *StateReaderConfig) (*StateReadCloser, error)

The StateLoaderFunc type is an adapter to allow the use of ordinary function as StateLoader.

func (StateLoaderFunc) LoadState

LoadState calls f(ctx, opts).

type StateReadCloser

type StateReadCloser struct {
	migrate.StateReader
	io.Closer        // optional close function
	Schema    string // in case we work on a single schema
	HCL       bool   // true if state was read from HCL files since in that case we always compare realms
}

StateReadCloser is a migrate.StateReader with an optional io.Closer.

func StateReaderAtlas

func StateReaderAtlas(context.Context, *StateReaderConfig) (*StateReadCloser, error)

StateReaderAtlas returns a migrate.StateReader from an Atlas Cloud schema.

func StateReaderHCL

func StateReaderHCL(ctx context.Context, c *StateReaderConfig) (*StateReadCloser, error)

StateReaderHCL returns a StateReader that reads the state from the given HCL paths urls.

func StateReaderSQL

func StateReaderSQL(ctx context.Context, config *StateReaderConfig) (*StateReadCloser, error)

StateReaderSQL returns a migrate.StateReader from an SQL file or a directory of migrations.

func (*StateReadCloser) Close

func (r *StateReadCloser) Close() error

Close redirects calls to Close to the enclosed io.Closer.

type StateReaderConfig

type StateReaderConfig struct {
	URLs        []*url.URL        // urls to create a migrate.StateReader from
	Client, Dev *sqlclient.Client // database connections, while dev is considered a dev database, client is not
	Schemas     []string          // schemas to work on
	Exclude     []string          // exclude flag values
	Include     []string          // include flag values
	WithPos     bool              // Indicate if schema.Pos should be loaded.
	Vars        map[string]cty.Value
}

StateReaderConfig is given to stateReader.

type UnsupportedError

type UnsupportedError struct {
	Err error
}

UnsupportedError wraps the standard message used to present an unsupported feature error.

func (*UnsupportedError) IsAbort

func (*UnsupportedError) IsAbort()

IsAbort implements the cmdapi.Aborter interface.

Jump to

Keyboard shortcuts

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