8000 Add support for MySQL/MariaDB HA · symfony-cli/symfony-cli@dda28ff · GitHub
[go: up one dir, main page]

Skip to content

Commit dda28ff

Browse files
committed
Add support for MySQL/MariaDB HA
1 parent 0a72133 commit dda28ff

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

envs/envs.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ func extractRelationshipsEnvs(env Environment) Envs {
123123
}
124124
prefix = strings.Replace(prefix, "-", "_", -1)
125125

126-
if scheme == "pgsql" || scheme == "mysql" {
126+
// we support mysql and mysql-replica
127+
if scheme == "pgsql" || strings.HasPrefix(scheme.(string), "mysql") {
127128
if scheme == "pgsql" {
128129
// works for both Doctrine and Go
129130
endpoint["scheme"] = "postgres"
@@ -152,7 +153,7 @@ func extractRelationshipsEnvs(env Environment) Envs {
152153
charset := "utf8"
153154
if envCharset := os.Getenv(fmt.Sprintf("%sCHARSET", prefix)); envCharset != "" {
154155
charset = envCharset
155-
} else if scheme == "mysql" {
156+
} else if strings.HasPrefix(scheme.(string), "mysql") {
156157
charset = "utf8mb4"
157158
}
158159
values[fmt.Sprintf("%sURL", prefix)] = values[fmt.Sprintf("%sURL", prefix)] + "&charset=" + charset
@@ -172,7 +173,7 @@ func extractRelationshipsEnvs(env Environment) Envs {
172173
version := strings.SplitN(v.(string), ":", 2)[1]
173174

174175
// we actually provide mariadb not mysql
175-
if endpoint["scheme"].(string) == "mysql" {
176+
if strings.HasPrefix(scheme.(string), "mysql") {
176177
minor := 0
177178
if version == "10.2" {
178179
minor = 7
@@ -211,7 +212,7 @@ func extractRelationshipsEnvs(env Environment) Envs {
211212
values["PGDATABASE"] = path
212213
values["PGUSER"] = endpoint["username"].(string)
213214
values["PGPASSWORD"] = endpoint["password"].(string)
214-
} else if scheme == "mysql" {
215+
} else if strings.HasPrefix(scheme.(string), "mysql") {
215216
values["MYSQL_HOST"] = endpoint["host"].(string)
216217
values["MYSQL_TCP_PORT"] = formatInt(endpoint["port"])
217218
}

envs/envs_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,57 @@ 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+
},
240+
}
241+
242+
rels := extractRelationshipsEnvs(env)
243+
c.Assert(rels["DATABASE_URL"], Equals, "mysql://user@database.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB")
244+
c.Assert(rels["DATABASE_REPLICA_URL"], Equals, "mysql://user@database-replica.internal:3306/main?sslmode=disable&charset=utf8mb4&serverVersion=10.6.0-MariaDB")
245+
}
246+
196247
func (s *ScenvSuite) TestDoctrineConfigTakesPrecedenceDatabaseURLs(c *C) {
197248
env := fakeEnv{
198249
Rels: map[string][]map[string]interface{}{

0 commit comments

Comments
 (0)
0