File size: 4,592 Bytes
1f10f31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package config

import (
	"errors"
	"fmt"
	"net/url"
	"strconv"

	"github.com/samber/lo"
	"github.com/spf13/viper"
)

type PostgresConfig struct {
	// PostgresConnectionParams is the PostgreSQL connection parameters, URL and PostgresConnectionParams are mutually exclusive.
	PostgresConnectionParams `mapstructure:",squash"`

	// URL is the PostgreSQL database connection URL.
	URL string `yaml:"url"`

	// AutoMigrate is a flag that indicates whether the database should be automatically migrated.
	// Supported values are:
	// - "false" to disable auto-migration at startup
	// - "migration" to use the migrations directory
	// - "migration-job" to wait for a separate migration job
	AutoMigrate AutoMigrate `yaml:"autoMigrate"`
}

// Validate validates the configuration.
func (c PostgresConfig) Validate() error {
	var errs []error
	if c.URL == "" && c.PostgresConnectionParams.IsEmpty() {
		errs = append(errs, errors.New("database URL or connection params are required"))
	}

	if c.URL != "" && !c.PostgresConnectionParams.IsEmpty() {
		errs = append(errs, errors.New("database URL and connection params are mutually exclusive"))
	}

	if err := c.AutoMigrate.Validate(); err != nil {
		errs = append(errs, err)
	}

	return errors.Join(errs...)
}

func (c PostgresConfig) AsURL() string {
	if c.URL != "" {
		return c.URL
	}
	return c.PostgresConnectionParams.AsURL()
}

func ConfigurePostgres(v *viper.Viper, prefix string) {
	v.SetDefault(AddPrefix(prefix, "url"), "")
	v.SetDefault(AddPrefix(prefix, "options.poolMaxConns"), 0)
	v.SetDefault(AddPrefix(prefix, "options.applicationName"), "")
	v.SetDefault(AddPrefix(prefix, "options.sslVerify"), "")
	v.SetDefault(AddPrefix(prefix, "options.sslRootCert"), "")
	v.SetDefault(AddPrefix(prefix, "host"), "")
	v.SetDefault(AddPrefix(prefix, "port"), 0)
	v.SetDefault(AddPrefix(prefix, "database"), "")
	v.SetDefault(AddPrefix(prefix, "user"), "")
	v.SetDefault(AddPrefix(prefix, "password"), "")
}

type AutoMigrate string

const (
	AutoMigrateMigration    AutoMigrate = "migration"
	AutoMigrateMigrationJob AutoMigrate = "migration-job"
	AutoMigrateOff          AutoMigrate = "false"
)

func (a AutoMigrate) Enabled() bool {
	// For all other values it's enabled
	return a != "false"
}

func (a AutoMigrate) Validate() error {
	switch a {
	case AutoMigrateMigration, AutoMigrateMigrationJob, AutoMigrateOff:
		return nil
	case "ent":
		return errors.New("ent auto-migration is no longer supported; run 'openmeter-jobs migrate adopt-ent' once, then run the normal migration (see docs/database-migration.md)")
	default:
		return errors.New("invalid auto-migrate value")
	}
}

type PostgresConnectionParams struct {
	Host     string `yaml:"host"`
	Port     uint16 `yaml:"port"`
	Database string `yaml:"database"`
	User     string `yaml:"user"`
	Password string `yaml:"password"`

	Options PostgresConnectionOptions `yaml:"options"`
}

func (c PostgresConnectionParams) Validate() error {
	var errs []error

	if c.Host == "" {
		errs = append(errs, errors.New("host is required"))
	}

	if c.Database == "" {
		errs = append(errs, errors.New("database is required"))
	}

	if c.User == "" {
		errs = append(errs, errors.New("user is required"))
	}

	if err := c.Options.Validate(); err != nil {
		errs = append(errs, err)
	}

	return errors.Join(errs...)
}

func (c PostgresConnectionParams) IsEmpty() bool {
	return lo.IsEmpty(c)
}

func (c PostgresConnectionParams) AsURL() string {
	host := c.Host
	if c.Port != 0 {
		host = fmt.Sprintf("%s:%d", c.Host, c.Port)
	}

	runtimeParams := make(url.Values)

	if c.Options.ApplicationName != "" {
		runtimeParams.Set("application_name", c.Options.ApplicationName)
	}

	if c.Options.PoolMaxConns != 0 {
		runtimeParams.Set("pool_max_conns", strconv.Itoa(c.Options.PoolMaxConns))
	}

	if c.Options.SSLVerify != "" {
		runtimeParams.Set("sslmode", c.Options.SSLVerify)
	}

	if c.Options.SSLRootCert != "" {
		runtimeParams.Set("sslrootcert", c.Options.SSLRootCert)
	}

	url := url.URL{
		Scheme:   "postgresql",
		User:     url.UserPassword(c.User, c.Password),
		Host:     host,
		Path:     c.Database,
		RawQuery: runtimeParams.Encode(),
	}

	return url.String()
}

type PostgresConnectionOptions struct {
	PoolMaxConns    int    `yaml:"poolMaxConns"`
	ApplicationName string `yaml:"applicationName"`
	SSLVerify       string `yaml:"sslVerify"`
	SSLRootCert     string `yaml:"sslRootCert"`
}

func (c PostgresConnectionOptions) Validate() error {
	var errs []error

	if c.PoolMaxConns < 0 {
		errs = append(errs, errors.New("poolMaxConns must be greater than 0"))
	}

	return errors.Join(errs...)
}