-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from hsinhoyeh/issue/33
allow mysql to use customized database
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package dockertest | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"net/url" | ||
"time" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
// SetupPostgreSQLContainer sets up a real PostgreSQL instance for testing purposes, | ||
|
@@ -39,3 +43,38 @@ func ConnectToPostgreSQL(tries int, delay time.Duration, connector func(url stri | |
} | ||
return c, errors.New("Could not set up PostgreSQL container.") | ||
} | ||
|
||
// SetUpPostgreDatabase connects postgre container with given $connectURL and also creates a new database named $databaseName | ||
// A modified url used to connect the created database will be returned | ||
func SetUpPostgreDatabase(databaseName, connectURL string) (modifiedURL string, err error) { | ||
db, err := sql.Open("postgres", connectURL) | ||
if err != nil { | ||
return "", err | ||
} | ||
defer db.Close() | ||
|
||
count := 0 | ||
err = db.QueryRow( | ||
fmt.Sprintf("SELECT COUNT(*) FROM pg_catalog.pg_database WHERE datname = '%s' ;", databaseName)). | ||
Scan(&count) | ||
if err != nil { | ||
return "", err | ||
} | ||
if count == 0 { | ||
// not found for $databaseName, create it | ||
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", databaseName)) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
|
||
// replace dbname in url | ||
// from: postgres://postgres:[email protected]:9071/postgres?sslmode=disable | ||
// to: postgres://postgres:[email protected]:9071/$databaseName?sslmode=disable | ||
u, err := url.Parse(connectURL) | ||
if err != nil { | ||
return "", err | ||
} | ||
u.Path = fmt.Sprintf("/%s", databaseName) | ||
return u.String(), nil | ||
} |