dbservice

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

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 New

func New(logger lager.Logger) *gorm.DB

New instantiates the db connection and runs migrations

func RunMigrations

func RunMigrations(db *gorm.DB) error

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

func SetupDB(logger lager.Logger) *gorm.DB

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

func ValidateLastMigration(lastMigration int) error

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)

Directories

Path Synopsis
Package models implements database object models for use with GORM
Package models implements database object models for use with GORM

Jump to

Keyboard shortcuts

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