本文整理匯總了Golang中github.com/starkandwayne/rdpgd/pg.NewPG函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewPG函數的具體用法?Golang NewPG怎麽用?Golang NewPG使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewPG函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: bdrGroupJoin
// Join BDR Group
func (r *RDPG) bdrGroupJoin() (err error) {
log.Trace(fmt.Sprintf(`rdpg.RDPG<%s>#bdrGroupJoin() Joining BDR Group rdpg for cluster...`, ClusterID))
p := pg.NewPG(globals.MyIP, pgPort, `rdpg`, `rdpg`, pgPass)
joinPG := pg.NewPG(bdrJoinIP, pgPort, `rdpg`, `rdpg`, pgPass)
re := regexp.MustCompile(`[^0-9]+`)
ip := strings.ToLower(string(re.ReplaceAll([]byte(globals.MyIP), []byte("_"))))
localNodeName := fmt.Sprintf(`rdpg_%s`, ip)
err = p.BDRGroupJoin(localNodeName, `rdpg`, *joinPG)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bdrGroupJoin(%s,rdpg) {HINT: Check pgbdr logs and pg_hba.conf} ! %s`, ClusterID, localNodeName, err))
return
}
return
}
示例2: Find
func (c *Credentials) Find() (err error) {
log.Trace(fmt.Sprintf(`cfsb.Credentials#Find(%s) ... `, c.BindingID))
if c.BindingID == "" {
return errors.New("Credentials ID is empty, can not Credentials#Find()")
}
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("cfsb.Credentials#Find(%s) PG#Connect() ! %s", c.BindingID, err))
return
}
defer db.Close()
sq := fmt.Sprintf(`SELECT id,instance_id,binding_id FROM cfsb.credentials WHERE binding_id=lower('%s') LIMIT 1`, c.BindingID)
log.Trace(fmt.Sprintf(`cfsb.Credentials#Find(%s) SQL > %s`, c.BindingID, sq))
err = db.Get(c, sq)
if err != nil {
if err == sql.ErrNoRows {
log.Error(fmt.Sprintf("cfsb.Credentials#Find(%s) ! Could not find binding with given Credentials ID", c.BindingID))
} else {
log.Error(fmt.Sprintf("cfsb.Credentials#Find(%s) ! %s", c.BindingID, err))
}
}
return
}
示例3: Create
// Create Credentials in the data store
func (c *Credentials) Create() (err error) {
log.Trace(fmt.Sprintf(`cfsb.Credentials#Create(%s,%s) ... `, c.InstanceID, c.BindingID))
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("cfsb.Credentials#Create(%s) ! %s", c.BindingID, err))
return
}
defer db.Close()
err = c.Find()
if err != nil { // Does not yet exist, insert the credentials.
if err == sql.ErrNoRows { // Does not yet exist, insert the credentials.
sq := fmt.Sprintf(`INSERT INTO cfsb.credentials (instance_id,binding_id,host,port,dbuser,dbpass,dbname) VALUES (lower('%s'),lower('%s'),'%s','%s','%s','%s','%s');`, c.InstanceID, c.BindingID, c.Host, c.Port, c.UserName, c.Password, c.Database)
log.Trace(fmt.Sprintf(`cfsb.Credentials#Create() > %s`, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf(`cfsb.Credentials#Create() %s ! %s`, sq, err))
}
} else {
log.Error(fmt.Sprintf(`cfsb.Credentials#Create() c.Find() binding %s ! %s`, c.BindingID, err))
}
return
} else { // Credentials already exists, return.
log.Trace(fmt.Sprintf(`cfsb.Credentials#Create() Credentials already exist for binding %s, returning`, c.BindingID))
return
}
}
示例4: getLockCountByDatabase
func getLockCountByDatabase(dbname string) (locks []databaseLocks, err error) {
locks = []databaseLocks{}
sql := `SELECT mode::text as mode, count(mode) as mode_count, datname::text as dbname FROM pg_locks, pg_database WHERE database=oid`
if dbname != "" {
sql += fmt.Sprintf(` AND datname = '%s'`, dbname)
}
sql += ` GROUP BY mode, datname;`
pgPort := `7432`
address := `127.0.0.1`
pgPass = os.Getenv(`RDPGD_PG_PASS`)
p := pg.NewPG(address, pgPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
return nil, err
}
defer db.Close()
err = db.Select(&locks, sql)
if err != nil {
log.Error(fmt.Sprintf("admin.getLockCountByDatabase ! db.Select(&locks, %s) erred : %s", sql, err.Error()))
return nil, err
}
return locks, nil
}
示例5: Reconcile
// This is called on the management cluster when it is running the scheduled task
// which reconciles the databases comparing against the service clusters lists.
func (i *Instance) Reconcile() (err error) {
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("instances.Register() p.Connect(%s) ! %s", p.URI, err))
return err
}
defer db.Close()
err = i.Lock()
if err != nil {
log.Error(fmt.Sprintf("instances.Instance#Reconcile(%s) Failed Locking instance %s ! %s", i.Database, i.Database, err))
return
}
ei, err := FindByDatabase(i.Database)
if err != nil {
log.Error(fmt.Sprintf("instances.Instance#Reconcile() ! %s", err))
} else if ei == nil {
log.Trace(fmt.Sprintf(`instances.Instance#Reconcile() Reconciling database %s for cluster %s`, i.Database, i.ClusterID))
sq := fmt.Sprintf(`INSERT INTO cfsb.instances (cluster_id,service_id ,plan_id ,instance_id ,organization_id ,space_id,dbname, dbuser, dbpass,effective_at) VALUES ('%s', '%s', '%s', '%s', '%s','%s','%s','%s','%s',CURRENT_TIMESTAMP)`, i.ClusterID, i.ServiceID, i.PlanID, i.InstanceID, i.OrganizationID, i.SpaceID, i.Database, i.User, i.Pass)
log.Trace(fmt.Sprintf(`instances.Instance#Reconcile(%s) > %s`, i.Database, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf("instances.Instance#Reconcile(%s) ! %s", i.Database, err))
}
}
err = i.Unlock()
if err != nil {
log.Error(fmt.Sprintf(`instances.Instance#Reconcile(%s) Unlocking ! %s`, i.InstanceID, err))
}
return
}
示例6: initialBootstrap
// General Boostrapping that should occur on every node irrespective of role/leader.
func (r *RDPG) initialBootstrap() (err error) {
log.Trace(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrap() Beginning general Bootstrapping...`, ClusterID))
// TODO: Record somehow that general bootstrap was completed and do not re-run.
p := pg.NewPG(`127.0.0.1`, pgPort, `postgres`, `postgres`, ``)
err = p.CreateUser(`rdpg`, pgPass)
if err != nil {
log.Error(fmt.Sprintf(`r.RDPG<%s>#initialBootstrap() CreateUser(rdpg) ! %s`, ClusterID, err))
return
}
err = p.CreateUser(`health`, `check`)
if err != nil {
log.Error(fmt.Sprintf(`r.RDPG<%s>#initialBootstrap() CreateUser(health) ! %s`, ClusterID, err))
return
}
// TODO: ALTER USER health SET default_transaction_read_only=on;
priviliges := []string{`SUPERUSER`, `CREATEDB`, `CREATEROLE`, `INHERIT`}
err = p.GrantUserPrivileges(`rdpg`, priviliges)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrap() p.GrantUserPrivileges(rdpg,...) ! %s`, ClusterID, err))
return
}
err = p.CreateDatabase(`rdpg`, `rdpg`)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrapping() CreateDatabase() ! %s`, ClusterID, err))
return
}
err = p.CreateDatabase(`health`, `health`)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrapping() CreateDatabase() ! %s`, ClusterID, err))
return
}
exts := []string{`btree_gist`, `bdr`, `pgcrypto`}
err = p.CreateExtensions(`rdpg`, exts)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrap() CreateExtensions() ! %s`, ClusterID, err))
return
}
err = r.Register()
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrap() Register() ! %s`, ClusterID, err))
return
}
err = r.waitForClusterNodes()
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrap() r.waitForClusterNodes() ! %s`, ClusterID, err))
return
}
// TODO: Record somehow that general bootstrap was completed and do not re-run.
return
}
示例7: Fetch
func (c *Catalog) Fetch() (err error) {
log.Trace(`cfsb.Catalog#Fetch()...`)
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("cfsb.Catalog#Fetch() ! %s", err))
return
}
defer db.Close()
sq := `SELECT service_id,name,description,bindable FROM cfsb.services;`
log.Trace(fmt.Sprintf(`cfsb.Catalog#Fetch() > %s`, sq))
err = db.Select(&c.Services, sq)
if err != nil {
log.Error(fmt.Sprintf("cfsb.Catalog#Fetch() db.Select() ! %s", err.Error()))
return
}
// TODO: Account for plans being associated with a service.
for i, _ := range c.Services {
service := &c.Services[i]
sq := `SELECT plan_id,name,description FROM cfsb.plans;`
log.Trace(fmt.Sprintf(`cfsb.Catalog#Fetch() > %s`, sq))
err = db.Select(&service.Plans, sq)
if err != nil {
log.Error(fmt.Sprintf("cfsb.Catalog#Fetch() db.Select() ! %s", err.Error()))
return
}
c.Services[i].Tags = []string{"rdpg", "postgresql"}
// c.Services[i].Dashboard = DashboardClient{}
}
return
}
示例8: columnMigrations
/*
columnMigrations migrates columns testing for conditions,
eg. handle the migration of pre-existing environments.
*/
func columnMigrations() (err error) {
p := pg.NewPG(`127.0.0.1`, pgPort, `rdpg`, `rdpg`, pgPass)
p.Set(`database`, `rdpg`)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf(`rdpg.columnMigration() Could not open connection ! %s`, err))
return
}
defer db.Close()
sq := fmt.Sprintf(`SELECT constraint_name FROM information_schema.table_constraints WHERE table_name='instances' AND constraint_type='UNIQUE';`)
log.Trace(fmt.Sprintf("rdpg.columnMigrations() %s", sq))
var constraintName string
if err = db.QueryRow(sq).Scan(&constraintName); err != nil {
if err == sql.ErrNoRows {
log.Trace(fmt.Sprintf("The instance table db name is not set UNIQUE constraints"))
_, err = db.Exec(`ALTER TABLE cfsb.instances ADD CONSTRAINT instances_dbname_key UNIQUE (dbname)`)
if err != nil {
log.Error(fmt.Sprintf("rdpg.columnMigrations()%s", err))
return
}
} else {
log.Error(fmt.Sprintf("rdpg.columnMigrations() ! %s", err))
return
}
}
return
}
示例9: Register
// This is called on the management cluster when the service cluster has created
// a new database and is registering it's avialabilityh with the management cluster.
func (i *Instance) Register() (err error) {
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("instances.Register() p.Connect(%s) ! %s", p.URI, err))
return err
}
defer db.Close()
err = i.Lock()
if err != nil {
log.Error(fmt.Sprintf("instances.Instance#Register(%s) Failed Locking instance %s ! %s", i.Database, i.Database, err))
return
}
sq := fmt.Sprintf(`INSERT INTO cfsb.instances (cluster_id,dbname, dbuser, dbpass,effective_at) VALUES ('%s','%s','%s','%s',CURRENT_TIMESTAMP)`, i.ClusterID, i.Database, i.User, i.Pass)
log.Trace(fmt.Sprintf(`instances.Instance#Register(%s) > %s`, i.Database, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf("instances.Instance#Register(%s) ! %s", i.Database, err))
}
err = i.Unlock()
if err != nil {
log.Error(fmt.Sprintf(`instances.Instance#Register(%s) Unlocking ! %s`, i.InstanceID, err))
}
return
}
示例10: Remove
func (b *Binding) Remove() (err error) {
log.Trace(fmt.Sprintf(`cfsb.Binding#Remove(%s) ... `, b.BindingID))
err = b.Find()
if err != nil {
log.Error(fmt.Sprintf(`cfsb.Binding#Remove(%s) ! %s`, b.BindingID, err))
return
}
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("cfsb.Binding#Remove(%s) ! %s", b.BindingID, err))
return
}
defer db.Close()
// TODO: Scheduled background task that does any cleanup necessary for an
// unbinding (remove credentials?)
sq := fmt.Sprintf(`UPDATE cfsb.bindings SET ineffective_at=CURRENT_TIMESTAMP WHERE binding_id=lower('%s')`, b.BindingID)
log.Trace(fmt.Sprintf(`cfsb.Binding#Remove(%s) SQL > %s`, b.BindingID, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf(`cfsb.Binding#Remove(%s) ! %s`, b.BindingID, err))
}
b.Creds = &Credentials{
InstanceID: b.InstanceID,
BindingID: b.BindingID,
}
err = b.Creds.Remove()
if err != nil {
log.Error(fmt.Sprintf(`cfsb.Binding#Remove(%s) b.Creds.Remove() ! %s`, b.BindingID, err))
}
return
}
示例11: Decommission
func (i *Instance) Decommission() (err error) {
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("instances.Decommission() p.Connect(%s) ! %s", p.URI, err))
return
}
defer db.Close()
// TODO: i.SetIneffective()
sq := fmt.Sprintf(`UPDATE cfsb.instances SET ineffective_at=CURRENT_TIMESTAMP WHERE dbname='%s'`, i.Database)
log.Trace(fmt.Sprintf(`instances.Instance<%s>#Decommission() SQL > %s`, i.InstanceID, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf("Instance#Decommission(%s) setting inefective_at ! %s", i.InstanceID, err))
return
}
// TODO: tasks.Task{ClusterID: ,Node: ,Role: ,Action:, Data: }.Enqueue()
// Question is how to do this without an import cycle? Some tasks require instances.
sq = fmt.Sprintf(`INSERT INTO tasks.tasks (cluster_id,role,action,data, cluster_service) VALUES ('%s','all','DecommissionDatabase','%s', '%s')`, i.ClusterID, i.Database, i.ClusterService)
log.Trace(fmt.Sprintf(`instances.Instance#Decommission(%s) Scheduling Instance Removal > %s`, i.InstanceID, sq))
_, err = db.Exec(sq)
if err != nil {
log.Error(fmt.Sprintf(`instances.Instance#Decommission(%s) ! %s`, i.InstanceID, err))
}
return
}
示例12: bdrNonLeaderBootstrap
// Non-Leader specifc bootstrapping.
func (r *RDPG) bdrNonLeaderBootstrap() (err error) {
log.Trace(fmt.Sprintf(`rdpg.RDPG<%s>#bdrNonLeaderBootstrap() bootstrapping non-leader...`, ClusterID))
err = r.bdrGroupJoin()
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bdrNonLeaderBootstrap() bdrGroupJoin() ! %s`, ClusterID, err))
r.bootstrapUnlock()
return err // BDR join during bootstrap is critical path, unlock and exit.
}
r.bootstrapUnlock()
err = r.waitForBDRNodes()
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bdrNonLeaderBootstrap() r.waitForBDRNodes() ! %s`, ClusterID, err))
}
p := pg.NewPG(`127.0.0.1`, pgPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("rdpg.RDPG<%s>#bdrNonLeaderBootstrap() ! %s", ClusterID, err))
return
}
defer db.Close()
err = p.WaitForRegClass("cfsb.instances")
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bdrNonLeaderBootstrap() p.WaitForRegClass() ! %s`, ClusterID, err))
}
err = r.waitForWriteMasterIP()
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bdrNonLeaderBootstrap() p.waitForWriteMasterIP() ! %s`, ClusterID, err))
}
return
}
示例13: bootstrapSystem
func (r *RDPG) bootstrapSystem() (err error) {
p := pg.NewPG(`127.0.0.1`, pgPort, `rdpg`, `rdpg`, pgPass)
exts := []string{`pgcrypto`, `pg_stat_statements`, `uuid-ossp`, `hstore`, `pg_trgm`}
err = p.CreateExtensions(`rdpg`, exts)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#initialBootstrap() CreateExtensions() ! %s`, ClusterID, err))
return
}
err = r.InitSchema()
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bootstrapSystem() r.InitSchema(%s) ! %s`, ClusterID, globals.ServiceRole, err))
return
}
cluster, err := NewCluster(ClusterID, r.ConsulClient)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bootstrapSystem(%s) NewCluster() ! %s`, ClusterID, globals.ServiceRole, err))
return err
}
err = cluster.SetWriteMaster(globals.MyIP)
if err != nil {
log.Error(fmt.Sprintf(`rdpg.RDPG<%s>#bootstrapSystem() SetWriteMaster() ! %s`, ClusterID, err))
return
}
return
}
示例14: Find
func (b *Binding) Find() (err error) {
log.Trace(fmt.Sprintf(`cfsb.Binding#Find(%s) ... `, b.BindingID))
if b.BindingID == "" {
return errors.New("Binding ID is empty, can not Binding#Find()")
}
p := pg.NewPG(`127.0.0.1`, pbPort, `rdpg`, `rdpg`, pgPass)
db, err := p.Connect()
if err != nil {
log.Error(fmt.Sprintf("cfsb.Binding#Find(%s) ! %s", b.BindingID, err))
return
}
defer db.Close()
sq := fmt.Sprintf(`SELECT id,instance_id FROM cfsb.bindings WHERE binding_id=lower('%s') LIMIT 1`, b.BindingID)
log.Trace(fmt.Sprintf(`cfsb.Binding#Find(%s) > %s`, b.BindingID, sq))
err = db.Get(b, sq)
if err != nil {
if err == sql.ErrNoRows {
log.Error(fmt.Sprintf("cfsb.Binding#Find(%s) ! Could not find binding with given Binding ID", b.BindingID))
} else {
log.Error(fmt.Sprintf("cfsb.Binding#Find(%s) ! %s", b.BindingID, err))
}
} else {
// TODO: Load creds: b.Creds := Credentials{} ... b.Creds.Find()
}
return
}
示例15: RestoreInPlace
// A wrapper for ImportSqlFile which handles the additional process of
// finding the backup whereever it is stored (local or remote) and putting it in
// the correct place, and then restoring it from that location.
func RestoreInPlace(dbname, basefile string) (err error) {
if strings.Contains(basefile, "/") {
errorMessage := fmt.Sprintf("utils/backup.RestoreInPlace ! '%s' is not a file base name.", basefile, err.Error())
log.Error(errorMessage)
return errors.New(errorMessage)
}
err = StageRestoreInPlace(dbname, basefile)
if err != nil {
log.Error(fmt.Sprintf("utils/backup.RestoreInPlace ! utils/backup.StageRestoreInPlace(%s, %s) erred : %s", dbname, basefile, err.Error()))
return err
}
p := pg.NewPG("127.0.0.1", globals.PBPort, "rdpg", "rdpg", globals.PGPass)
if err != nil {
log.Error(fmt.Sprintf(`utils/backup.RestoreInPlace ! pg.NewPG("127.0.0.1", %s, "rdpg", "rdpg", %s) erred : %s`, globals.PBPort, globals.PGPass, err.Error()))
return err
}
exists, err := DatabaseExists(dbname)
if err != nil {
log.Error(fmt.Sprintf("utils/backup.RestoreInPlace ! utils/backup.DatabaseExists(%s) erred : %s", dbname, err.Error()))
return err
}
if exists {
err = p.DisableDatabase(dbname)
if err != nil {
log.Error(fmt.Sprintf("utils.backup.RestoreInPlace ! pg.DisableDatabase(%s) erred : %s", dbname, err.Error()))
return err
}
err = p.DropDatabase(dbname)
if err != nil {
log.Error(fmt.Sprintf("utils/backup.RestoreInPlace ! pg.DropDatabase(%s) erred : %s", dbname, err.Error()))
return err
}
} else {
errorMessage := fmt.Sprintf("utils/backup.RestoreInPlace ! Restoring database %s doesn't currently exist.", dbname)
log.Warn(errorMessage)
}
username := "u" + strings.TrimPrefix(dbname, "d")
err = p.CreateDatabase(dbname, username)
if err != nil {
log.Error(fmt.Sprintf("utils/backup.StageRestoreInPlace ! pg.CreateDatabase(%s, %s) erred : %s", dbname, username, err.Error()))
return err
}
err = ImportSqlFile(dbname, RestoreLocation(dbname, basefile))
if err != nil {
log.Error(fmt.Sprintf("utils/backup.RestoreInPlace ! utils/backup.ImportSqlFile(%s, %s) erred : %s", dbname, RestoreLocation(dbname, basefile), err.Error()))
return err
}
err = UnstageRestore(dbname, basefile)
if err != nil {
log.Error(fmt.Sprintf("utils/backup.RestoreInPlace ! UnstageRestore(%s, %s) erred : %s", dbname, basefile, err.Error()))
return err
}
return nil
}