Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
9c663b1b
Commit
9c663b1b
authored
Jan 09, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: merge mysql dsn with params
parent
777ed899
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
2 deletions
+21
-2
mysql.go
store/db/mysql/mysql.go
+21
-2
No files found.
store/db/mysql/mysql.go
View file @
9c663b1b
...
@@ -4,6 +4,7 @@ import (
...
@@ -4,6 +4,7 @@ import (
"context"
"context"
"database/sql"
"database/sql"
"fmt"
"fmt"
"net/url"
"github.com/go-sql-driver/mysql"
"github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
"github.com/pkg/errors"
...
@@ -23,9 +24,13 @@ func NewDB(profile *profile.Profile) (store.Driver, error) {
...
@@ -23,9 +24,13 @@ func NewDB(profile *profile.Profile) (store.Driver, error) {
// Open MySQL connection with parameter.
// Open MySQL connection with parameter.
// multiStatements=true is required for migration.
// multiStatements=true is required for migration.
// See more in: https://github.com/go-sql-driver/mysql#multistatements
// See more in: https://github.com/go-sql-driver/mysql#multistatements
dsn
:=
fmt
.
Sprintf
(
"%s?multiStatements=true"
,
profile
.
DSN
)
dsn
,
err
:=
mergeDSNWithParams
(
profile
.
DSN
,
map
[
string
]
string
{
"multiStatements"
:
"true"
,
})
if
err
!=
nil
{
return
nil
,
err
}
var
err
error
driver
:=
DB
{
profile
:
profile
}
driver
:=
DB
{
profile
:
profile
}
driver
.
config
,
err
=
mysql
.
ParseDSN
(
dsn
)
driver
.
config
,
err
=
mysql
.
ParseDSN
(
dsn
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -104,3 +109,17 @@ func (d *DB) GetCurrentDBSize(ctx context.Context) (int64, error) {
...
@@ -104,3 +109,17 @@ func (d *DB) GetCurrentDBSize(ctx context.Context) (int64, error) {
func
(
d
*
DB
)
Close
()
error
{
func
(
d
*
DB
)
Close
()
error
{
return
d
.
db
.
Close
()
return
d
.
db
.
Close
()
}
}
func
mergeDSNWithParams
(
baseDSN
string
,
params
map
[
string
]
string
)
(
string
,
error
)
{
parsedDSN
,
err
:=
url
.
Parse
(
baseDSN
)
if
err
!=
nil
{
return
""
,
errors
.
Wrapf
(
err
,
"failed to parse DSN: %s"
,
baseDSN
)
}
existingParams
:=
parsedDSN
.
Query
()
for
key
,
value
:=
range
params
{
existingParams
.
Add
(
key
,
value
)
}
parsedDSN
.
RawQuery
=
existingParams
.
Encode
()
return
parsedDSN
.
String
(),
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment