-
Notifications
You must be signed in to change notification settings - Fork 6
/
postgresql.go
92 lines (79 loc) · 2.57 KB
/
postgresql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package database
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
// Postgres
type Postgres struct {
BaseDatabaseInstance
db *gorm.DB
}
// initPostgresInstance opens a postgresql connection to the host
// and returns a DatabaseInstance object, holding the connection object
func initPostgresInstance(host string, username string, password string, sslmode string, port string) (DatabaseInstance, error) {
db, err := gorm.Open("postgres", fmt.Sprintf("user=%s host=%s sslmode=%s password=%s port=%s", username, host, sslmode, password, port))
if err != nil {
return nil, err
}
return &Postgres{
BaseDatabaseInstance: BaseDatabaseInstance{
instanceMetadata: InstanceMetadata{
ConnectionInformation: ConnectionInformation{
Credentials: Credentials{
Username: username,
Password: password,
},
Endpoint: host,
Port: port,
},
},
},
db: db,
}, nil
}
// CreateDatabase creates a db within the DB instance
// and creates and grants all read/write privileges
// to the requested userame
func (p *Postgres) CreateDatabaseAndUser(dbName string, username string, password string) (*DatabaseMetadata, error) {
if p.db == nil {
return nil, fmt.Errorf(NilDBInstanceErr, "postgres")
}
// we have to use fmt.Sprintf here because of an issue with the underlying driver
// and properly sanitizing create database statements
if err := p.db.Exec(fmt.Sprintf(`CREATE DATABASE "%s";`, dbName)).Error; err != nil {
return nil, err
}
// create user
if err := p.db.Exec(fmt.Sprintf(`CREATE USER "%s" with ENCRYPTED PASSWORD '%s';`, username, password)).Error; err != nil {
return nil, err
}
// create user permissions
if err := p.db.Exec(fmt.Sprintf(`GRANT CONNECT ON DATABASE "%s" TO "%s";`, dbName, username)).Error; err != nil {
return nil, err
}
return &DatabaseMetadata{
Name: dbName,
Credentials: Credentials{
Username: username,
Password: password,
},
}, nil
}
// DeleteDatabaseAndUser deletes the requested database
// on the shared Postgres instance and also any roles
// associated with the database
func (p *Postgres) DeleteDatabaseAndUser(dbName string, dbUser string) error {
if p.db == nil {
return fmt.Errorf(NilDBInstanceErr, "postgres")
}
// we have to use fmt.Sprintf here because of an issue with the underlying driver
// and properly sanitizing create database statements
if err := p.db.Exec(fmt.Sprintf(`DROP DATABASE "%s";`, dbName)).Error; err != nil {
return err
}
if err := p.db.Exec(fmt.Sprintf(`DROP USER "%s";`, dbUser)).Error; err != nil {
return err
}
return nil
}