Documentation
¶
Overview ¶
Package dbservice implements database setup, connection, and migration
Index ¶
Examples ¶
Constants ¶
View Source
const ( DBTypeMySQL = "mysql" DBTypeSQLite3 = "sqlite3" )
Variables ¶
This section is empty.
Functions ¶
func RunMigrations ¶
RunMigrations runs schema migrations on the provided service broker database to get it up to date
func SetDatabaseCredentials ¶
func SetDatabaseCredentials(vcapService VcapService) error
func SetupDB ¶
SetupDB pulls db credentials from the environment, connects to the db, and returns the db connection
func UseVcapServices ¶
func UseVcapServices() error
Example ¶
_ = os.Setenv("VCAP_SERVICES", `{
"p.mysql": [
{
"label": "p.mysql",
"name": "my-instance",
"plan": "db-medium",
"provider": null,
"syslog_drain_url": null,
"tags": [
"mysql"
],
"credentials": {
"hostname": "10.0.0.20",
"jdbcUrl": "jdbc:mysql://10.0.0.20:3306/service_instance_db?user=fefcbe8360854a18a7994b870e7b0bf5\u0026password=z9z6eskdbs1rhtxt",
"name": "service_instance_db",
"password": "z9z6eskdbs1rhtxt",
"port": 3306,
"uri": "mysql://fefcbe8360854a18a7994b870e7b0bf5:z9z6eskdbs1rhtxt@10.0.0.20:3306/service_instance_db?reconnect=true",
"username": "fefcbe8360854a18a7994b870e7b0bf5"
},
"volume_mounts": []
}
]
}`)
_ = UseVcapServices()
fmt.Println(viper.Get(dbHostProp))
fmt.Println(viper.Get(dbUserProp))
fmt.Println(viper.Get(dbPassProp))
fmt.Println(viper.Get(dbNameProp))
Output: 10.0.0.20 fefcbe8360854a18a7994b870e7b0bf5 z9z6eskdbs1rhtxt service_instance_db
func ValidateLastMigration ¶
ValidateLastMigration returns an error if the database version is newer than this tool supports or is too old to be updated.
Types ¶
type VcapService ¶
type VcapService struct {
BindingName string `json:"binding_name"` // The name assigned to the service binding by the user.
InstanceName string `json:"instance_name"` // The name assigned to the service instance by the user.
Name string `json:"name"` // The binding_name if it exists; otherwise the instance_name.
Label string `json:"label"` // The name of the service offering.
Tags []string `json:"tags"` // An array of strings an app can use to identify a service instance.
Plan string `json:"plan"` // The service plan selected when the service instance was created.
Credentials map[string]any `json:"credentials"` // The service-specific credentials needed to access the service instance.
}
func ParseVcapServices ¶
func ParseVcapServices(vcapServicesData string) (VcapService, error)
Source Files
¶
Click to show internal directories.
Click to hide internal directories.