-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add chainlink health command; make DB avialable for testscript client…
…/server tests (#11591)
- Loading branch information
Showing
14 changed files
with
658 additions
and
74 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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package testdb | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/store/dialects" | ||
) | ||
|
||
const ( | ||
// PristineDBName is a clean copy of test DB with migrations. | ||
PristineDBName = "chainlink_test_pristine" | ||
// TestDBNamePrefix is a common prefix that will be auto-removed by the dangling DB cleanup process. | ||
TestDBNamePrefix = "chainlink_test_" | ||
) | ||
|
||
// CreateOrReplace creates a database named with a common prefix and the given suffix, and returns the URL. | ||
// If the database already exists, it will be dropped and re-created. | ||
// If withTemplate is true, the pristine DB will be used as a template. | ||
func CreateOrReplace(parsed url.URL, suffix string, withTemplate bool) (string, error) { | ||
if parsed.Path == "" { | ||
return "", errors.New("path missing from database URL") | ||
} | ||
|
||
// Match the naming schema that our dangling DB cleanup methods expect | ||
dbname := TestDBNamePrefix + suffix | ||
if l := len(dbname); l > 63 { | ||
return "", fmt.Errorf("dbname %v too long (%d), max is 63 bytes. Try a shorter suffix", dbname, l) | ||
} | ||
// Cannot drop test database if we are connected to it, so we must connect | ||
// to a different one. 'postgres' should be present on all postgres installations | ||
parsed.Path = "/postgres" | ||
db, err := sql.Open(string(dialects.Postgres), parsed.String()) | ||
if err != nil { | ||
return "", fmt.Errorf("in order to drop the test database, we need to connect to a separate database"+ | ||
" called 'postgres'. But we are unable to open 'postgres' database: %+v\n", err) | ||
} | ||
defer db.Close() | ||
|
||
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbname)) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to drop postgres migrations test database: %v", err) | ||
} | ||
if withTemplate { | ||
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s WITH TEMPLATE %s", dbname, PristineDBName)) | ||
} else { | ||
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbname)) | ||
} | ||
if err != nil { | ||
return "", fmt.Errorf("unable to create postgres test database with name '%s': %v", dbname, err) | ||
} | ||
parsed.Path = fmt.Sprintf("/%s", dbname) | ||
return parsed.String(), nil | ||
} |
Oops, something went wrong.