forked from shoekstra/go-dbmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.go
95 lines (77 loc) · 2.31 KB
/
postgres.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
93
94
95
package dbmanager
import (
"database/sql"
"fmt"
"log"
_ "github.com/jackc/pgx/v5/stdlib"
)
type postgresManager struct {
databaseManager
}
// newPostgresManager creates a new PostgreSQL manager.
func newPostgresManager(options ...func(*Connection)) Manager {
manager := &postgresManager{
databaseManager: databaseManager{
connection: Connection{
Database: "postgres",
Port: "5432",
SSLMode: "disable",
},
},
}
manager.initialize(options...)
return manager
}
// Connect connects to the PostgreSQL server.
func (m *postgresManager) Connect() error {
log.Printf("Connecting to %s:%s as %s\n", m.connection.Host, m.connection.Port, m.connection.Username)
db, err := sql.Open("pgx", m.connectionString(m.connection))
if err != nil {
return fmt.Errorf("error connecting to PostgreSQL database: %w", err)
}
m.db = db
if err := m.db.Ping(); err != nil {
return fmt.Errorf("error pinging PostgreSQL database: %w", err)
}
log.Printf("Connected to PostgreSQL database %s on %s", m.connection.Database, m.connection.Host)
return nil
}
// connectionStrings returns a list of connection strings for the specified database.
func (m *postgresManager) connectionString(connection Connection) string {
connectionString := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=%s",
connection.Host, connection.Port, connection.Username, connection.Database, connection.SSLMode)
if m.connection.Password != "" {
connectionString += fmt.Sprintf(" password=%s", m.connection.Password)
}
return connectionString
}
// Disconnect disconnects from the PostgreSQL server.
func (m *postgresManager) Disconnect() error {
log.Println("Disconnecting...")
if err := m.db.Close(); err != nil {
return fmt.Errorf("error closing connection to PostgreSQL database: %w", err)
}
return nil
}
// Manage manages the databases and users based on the provided options.
func (m *postgresManager) Manage(databases []Database, users []User) error {
// Create users
for _, user := range users {
if err := m.CreateUser(user); err != nil {
return err
}
}
// Create databases
for _, database := range databases {
if err := m.CreateDatabase(database); err != nil {
return err
}
}
// Grant permissions
for _, user := range users {
if err := m.GrantPermissions(user); err != nil {
return err
}
}
return nil
}