Skip to content

Commit 6e75a50

Browse files
committed
Add support for MySQL/MariaDB/PostgreSQL HA
1 parent 0a72133 commit 6e75a50

File tree

2 files changed

+102
-6
lines changed

2 files changed

+102
-6
lines changed

envs/envs.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,11 @@ func extractRelationshipsEnvs(env Environment) Envs {
123123
}
124124
prefix = strings.Replace(prefix, "-", "_", -1)
125125

126-
if scheme == "pgsql" || scheme == "mysql" {
127-
if scheme == "pgsql" {
126+
// HA support via scheme-replica
127+
isPostgreSQL := strings.HasPrefix(scheme.(string), "pgsql")
128+
isMySQL := strings.HasPrefix(scheme.(string), "mysql")
129+
if isPostgreSQL || isMySQL {
130+
if isPostgreSQL {
128131
// works for both Doctrine and Go
129132
endpoint["scheme"] = "postgres"
130133
}
@@ -152,7 +155,7 @@ func extractRelationshipsEnvs(env Environment) Envs {
152155
charset := "utf8"
153156
if envCharset := os.Getenv(fmt.Sprintf("%sCHARSET", prefix)); envCharset != "" {
154157
charset = envCharset
155-
} else if scheme == "mysql" {
158+
} else if isMySQL {
156159
charset = "utf8mb4"
157160
}
158161
values[fmt.Sprintf("%sURL", prefix)] = values[fmt.Sprintf("%sURL", prefix)] + "&charset=" + charset
@@ -172,7 +175,7 @@ func extractRelationshipsEnvs(env Environment) Envs {
172175
version := strings.SplitN(v.(string), ":", 2)[1]
173176

174177
// we actually provide mariadb not mysql
175-
if endpoint["scheme"].(string) == "mysql" {
178+
if isMySQL {
176179
minor := 0
177180
if version == "10.2" {
178181
minor = 7
@@ -205,13 +208,13 @@ func extractRelationshipsEnvs(env Environment) Envs {
205208
values[fmt.Sprintf("%sDATABASE", prefix)] = path
206209

207210
if env.Local() {
208-
if scheme == "pgsql" {
211+
if isPostgreSQL {
209212
values["PGHOST"] = endpoint["host"].(string)
210213
values["PGPORT"] = formatInt(endpoint["port"])
211214
values["PGDATABASE"] = path
212215
values["PGUSER"] = endpoint["username"].(string)
213216
values["PGPASSWORD"] = endpoint["password"].(string)
214-
} else if scheme == "mysql" {
217+
} else if isMySQL {
215218
values["MYSQL_HOST"] = endpoint["host"].(string)
216219
values["MYSQL_TCP_PORT"] = formatInt(endpoint["port"])
217220
}

envs/envs_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,99 @@ func (s *ScenvSuite) TestCloudTunnelDatabaseURLs(c *C) {
193193
c.Assert(rels["POSTGRESQL_URL"], Equals, "postgres://main:main@127.0.0.1:30000/main?sslmode=disable&charset=utf8&serverVersion=13")
194194
}
195195

196+
func (s *ScenvSuite) TestCloudHADatabaseURLs(c *C) {
197+
env := fakeEnv{
198+
Rels: map[string][]map[string]interface{}{
199+
"database-replica": {
200+
{
201+
"username": "user",
202+
"scheme": "mysql",
203+
"service": "db",
204+
"fragment": interface{}(nil),
205+
"ip": "169.254.150.110",
206+
"hostname": "e3n2frcxjqipslsc6sq7rfmwzm.db.service.._.platform.sh",
207+
"port": 3306,
208+
"cluster": "gqiujktuqrcxm-main-bvxea6i",
209+
"host": "database-replica.internal",
210+
"rel": "mysql-replica",
211+
"path": "main",
212+
"query": map[string]interface{}{"is_master": false},
213+
"password": "",
214+
"type": "mysql:10.6",
215+
"public": false,
216+
"host_mapped": false,
217+
},
218+
},
219+
"database": {
220+
{
221+
"username": "user",
222+
"scheme": "mysql",
223+
"service": "db",
224+
"fragment": interface{}(nil),
225+
"ip": "169.254.193.18",
226+
"hostname": "jvlu7c7jx3nzt3cowwkcrslhcq.db.service.._.platform.sh",
227+
"port": 3306,
228+
"cluster": "gqiujktuqrcxm-main-bvxea6i",
229+
"host": "database.internal",
230+
"rel": "mysql",
231+
"path": "main",
232+
"query": map[string]interface{}{"is_master": true},
233+
"password": "",
234+
"type": "mysql:10.6",
235+
"public": false,
236+
"host_mapped": false,
237+
},
238+
},
239+
"psql-replica": {
240+
{
241+
"username": "user",
242+
"scheme": "pgsql",
243+
"service": "db",
244+
"fragment": interface{}(nil),
245+
"ip": "169.254.150.110",
246+
"hostname": "e3n2frcxjqipslsc6sq7rfmwzm.db.service.._.platform.sh",
247+
"port": 5432,
248+
"cluster": "gqiujktuqrcxm-main-bvxea6i",
249+
"host": "psql-replica.internal",
250+
"rel": "pgsql-replica",
251+
"path": "main",
252+
"query": map[string]interface{}{"is_master": false},
253+
"password": "",
254+
"type": "postgresql:15",
255+
"public": false,
256+
"host_mapped": false,
257+
},
258+
},
259+
"psql": {
260+
{
261+
"username": "user",
262+
"scheme": "pgsql",
263+
"service": "db",
264+
"fragment": interface{}(nil),
265+
"ip": "169.254.193.18",
266+
"hostname": "jvlu7c7jx3nzt3cowwkcrslhcq.db.service.._.platform.sh",
267+
"port": 5432,
268+
"cluster": "gqiujktuqrcxm-main-bvxea6i",
269+
"host": "psql.internal",
270+
"rel": "pgsql",
271+
"path": "main",
272+
"query": map[string]interface{}{"is_master": true},
273+
"password": "",
274+
"type": "postgresql:15",
275+
"public": false,
276+
"host_mapped": false,
277+
},
278+
},
279+
},
280+
}
281+
282+
rels := extractRelationshipsEnvs(env)
283+
c.Assert(rels["DATABASE_URL"], Equals, "mysql://user@database.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB")
284+
c.Assert(rels["DATABASE_REPLICA_URL"], Equals, "mysql://user@database-replica.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB")
285+
c.Assert(rels["PSQL_URL"], Equals, "postgres://user@psql.internal:5432/main?sslmode=disable&charset=utf8&serverVersion=15")
286+
c.Assert(rels["PSQL_REPLICA_URL"], Equals, "postgres://user@psql-replica.internal:5432/main?sslmode=disable&charset=utf8&serverVersion=15")
287+
}
288+
196289
func (s *ScenvSuite) TestDoctrineConfigTakesPrecedenceDatabaseURLs(c *C) {
197290
env := fakeEnv{
198291
Rels: map[string][]map[string]interface{}{

0 commit comments

Comments
 (0)