Use pgmi as a Go library#
pgmi is a CLI and a Go library. A devops tool written in Go can run a deploy
itself, with the same file discovery, session and execution contract as
pgmi deploy:
go get github.com/vvka-141/pgmiimport (
"github.com/vvka-141/pgmi/pkg/pgmi"
"github.com/vvka-141/pgmi/pkg/pgmi/deploy"
)
result, err := deploy.New().Deploy(ctx, pgmi.DeploymentConfig{
SourcePath: "./db",
ConnectionString: os.Getenv("DATABASE_URL"),
MaintenanceDatabase: "postgres",
DatabaseName: "app",
Parameters: map[string]string{"env": "dev"},
})
if err != nil {
log.Printf("%s (exit code %d)", pgmi.FormatError(err), pgmi.ExitCodeForError(err))
}pgmi.DeploymentConfigtakes what the CLI flags resolve to: a connection string, the target and maintenance databases, parameters,Overwrite,Force. It has no timeout: set a deadline on the context passed toDeploy(context.WithTimeout); a deploy that runs past it fails with an errorpgmi.ExitCodeForErrormaps to 16, as the CLI’s--timeoutdoes. Nothing readspgmi.yamlorPGMI_PARAM_*for you. The standardPG*variables do apply, through pgx, to any field the connection string leaves out:PGUSER,PGPASSWORD,PGPASSFILE,PGCONNECT_TIMEOUT,PGSSLMODE,PGSSLKEY,PGSSLCERT,PGSSLCERTMODE,PGSSLSNI,PGSSLROOTCERT,PGSSLPASSWORD,PGSSLNEGOTIATION,PGTARGETSESSIONATTRS,PGSERVICE,PGSERVICEFILE,PGTZ,PGOPTIONS,PGMINPROTOCOLVERSION,PGMAXPROTOCOLVERSION,PGCHANNELBINDINGandPGREQUIREAUTH.PGHOST,PGPORTandPGDATABASEdo not (unlike the CLI): an omitted host, port or database becomeslocalhost,5432andpostgresbefore pgx sees the string, andPGAPPNAMEdoes not either (pgmi names the sessionpgmi). pgmi itself readsPGSSLMODEfor Azure and AWS token auth. A program that must not inherit them, say a CI job that setsPG*for another tool, puts the fields in the connection string or unsets the variables.pgmi.DeployResultsays how far the deploy got, also on failure: files loaded, execution units and how many committed, the unit that failed, whether the database was created.pgmi.ExitCodeForErrormaps the error to the CLI’s exit codes .- An overwrite of an existing database is refused unless
Forceis set ordeploy.WithApproverdecides. The library never prompts. deploy.WithLoggerreceives pgmi’s progress messages, connection retries and warnings; the default discards them.deploy.WithNoticeHandlerreceives the PostgreSQL noticesdeploy.sqlraises, as(severity, message, detail, hint)with the unlocalized severity; without it they are discarded. Each Deployer has its own, so concurrent deploys never mix their notices; one Deployer’s handler is called concurrently when its deploys overlap.- A nil logger or notice handler, or an empty
deploy.WithVersion, keeps the default.
The API is pre-1.0 and may change between minor versions.