Documentation
¶
Overview ¶
Package postgres provides PostgreSQL functionality for desired state management. This file defines the interface for desired state providers (embedded or external databases).
Package postgres provides embedded PostgreSQL functionality for production use. This package is used by the plan command to create temporary PostgreSQL instances for validating desired state schemas.
Package postgres provides external PostgreSQL database functionality for desired state management.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateTempSchemaName ¶
func GenerateTempSchemaName() string
GenerateTempSchemaName creates a unique temporary schema name for plan operations. The format is: pgschema_tmp_YYYYMMDD_HHMMSS_RRRRRRRR where RRRRRRRR is a random 8-character hex string for uniqueness. The "_tmp_" marker makes it distinctive and prevents accidental matching with user schemas.
Example: pgschema_tmp_20251030_154501_a3f9d2e1
Panics if random number generation fails (indicates serious system issue).
Types ¶
type DesiredStateProvider ¶
type DesiredStateProvider interface {
// GetConnectionDetails returns connection details for IR inspection
// Returns: host, port, database, username, password
GetConnectionDetails() (string, int, string, string, string)
// GetSchemaName returns the actual schema name to inspect.
// For embedded postgres: returns the temporary schema name (pgschema_tmp_*)
// For external database: returns the temporary schema name (pgschema_tmp_*)
GetSchemaName() string
// ApplySchema applies the desired state SQL to a schema.
// For embedded postgres: resets the schema (drop/recreate)
// For external database: creates temporary schema with timestamp suffix
ApplySchema(ctx context.Context, schema string, sql string) error
// Stop performs cleanup.
// For embedded postgres: stops instance and removes temp directory
// For external database: drops temporary schema (best effort) and closes connection
Stop() error
}
DesiredStateProvider is an interface that abstracts the desired state database provider. It can be implemented by either embedded PostgreSQL or an external database connection.
type EmbeddedPostgres ¶
type EmbeddedPostgres struct {
// contains filtered or unexported fields
}
EmbeddedPostgres manages a temporary embedded PostgreSQL instance. This is used by the plan command to validate desired state schemas.
func StartEmbeddedPostgres ¶
func StartEmbeddedPostgres(config *EmbeddedPostgresConfig) (*EmbeddedPostgres, error)
StartEmbeddedPostgres starts a temporary embedded PostgreSQL instance
func (*EmbeddedPostgres) ApplySchema ¶
ApplySchema resets a schema (drops and recreates it) and applies SQL to it. This ensures a clean state before applying the desired schema definition. Note: The schema parameter is ignored - we always use the temporary schema name.
func (*EmbeddedPostgres) GetConnectionDetails ¶
func (ep *EmbeddedPostgres) GetConnectionDetails() (host string, port int, database, username, password string)
GetConnectionDetails returns all connection details needed to connect to the embedded PostgreSQL instance
func (*EmbeddedPostgres) GetSchemaName ¶
func (ep *EmbeddedPostgres) GetSchemaName() string
GetSchemaName returns the temporary schema name used for desired state validation. This returns the timestamped schema name that was created by ApplySchema.
func (*EmbeddedPostgres) Stop ¶
func (ep *EmbeddedPostgres) Stop() error
Stop stops and cleans up the embedded PostgreSQL instance
type EmbeddedPostgresConfig ¶
type EmbeddedPostgresConfig struct {
Version PostgresVersion
Database string
Username string
Password string
}
EmbeddedPostgresConfig holds configuration for starting embedded PostgreSQL
type ExternalDatabase ¶
type ExternalDatabase struct {
// contains filtered or unexported fields
}
ExternalDatabase manages an external PostgreSQL database for desired state validation. It creates temporary schemas with timestamp suffixes to avoid conflicts.
func NewExternalDatabase ¶
func NewExternalDatabase(config *ExternalDatabaseConfig) (*ExternalDatabase, error)
NewExternalDatabase creates a new external database connection for desired state validation. It validates the connection, checks version compatibility, and generates a temporary schema name.
func (*ExternalDatabase) ApplySchema ¶
ApplySchema creates a temporary schema and applies SQL to it. The temporary schema name includes a timestamp to avoid conflicts.
func (*ExternalDatabase) GetConnectionDetails ¶
func (ed *ExternalDatabase) GetConnectionDetails() (host string, port int, database, username, password string)
GetConnectionDetails returns all connection details needed to connect to the external database
func (*ExternalDatabase) GetSchemaName ¶
func (ed *ExternalDatabase) GetSchemaName() string
GetSchemaName returns the temporary schema name used for desired state validation
func (*ExternalDatabase) Stop ¶
func (ed *ExternalDatabase) Stop() error
Stop closes the connection and drops the temporary schema (best effort). Errors during cleanup are logged but don't cause failures.
type ExternalDatabaseConfig ¶
type ExternalDatabaseConfig struct {
Host string
Port int
Database string
Username string
Password string
TargetMajorVersion int // Expected major version to match
}
ExternalDatabaseConfig holds configuration for connecting to an external database
type PostgresVersion ¶
type PostgresVersion = embeddedpostgres.PostgresVersion
PostgresVersion is an alias for the embedded-postgres version type.
func DetectPostgresVersionFromDB ¶
func DetectPostgresVersionFromDB(host string, port int, database, user, password string) (PostgresVersion, error)
DetectPostgresVersionFromDB connects to a database and detects its version This is a convenience function that opens a connection, detects the version, and closes it