# README
DB
A Go package to handle database connections.
Currently supports databases utilising the following protocols:
- MySQL
- PostgreSQL
- MSSQL
Github | https://github.com/usvc/go-db |
Gitlab | https://gitlab.com/usvc/modules/go/db |
Usage
Importing
import "github.com/usvc/go-db"
Creating a new database connection
if err := db.Init(Options{
Username: "user",
Password: "password",
Database: "schema",
Hostname: "localhost",
Port: 3306,
}); err != nil {
log.Printf("an error occurred while creating the connection: %s", err)
}
Creating a new, named database connection
if err := db.Init(Options{
ConnectionName: "non-default",
Username: "user",
Password: "password",
Database: "schema",
Hostname: "localhost",
Port: 3306,
}); err != nil {
log.Printf("an error occurred while creating the connection: %s", err)
}
Retrieving a database connection
// retrieve the 'default' connection
connection := db.Get()
if connection == nil {
log.Println("connection 'default' does not exist")
}
Retrieving a named database connection
// retrieve the 'non-default' connection
connection := db.Get("non-default")
if connection == nil {
log.Println("connection 'non-default' does not exist")
}
Importing an existing connection
var existingConnection *sql.DB
// ... initialise `existingConnection` by other means ...
if err := db.Import(existingConnection, "default"); err != nil {
log.Printf("connection 'default' seems to already exist: %s\n", err)
}
Verifying a connection works
var existingConnection *sql.DB
// ... initialise `existingConnection` by other means ...
db.Import(existingConnection, "existing-connection")
if err := db.Check("existing-connection"); err != nil {
log.Printf("connection 'existing-connection' could not connect: %s\n", err)
}
Development Runbook
Getting Started
- Clone this repository
- Run
make deps
to pull in external dependencies - Write some awesome stuff
- Run
make test
to ensure unit tests are passing - Push
Continuous Integration (CI) Pipeline
On Github
Github is used to deploy binaries/libraries because of it's ease of access by other developers.
Releasing
Releasing of the binaries can be done via Travis CI.
- On Github, navigate to the tokens settings page (by clicking on your profile picture, selecting Settings, selecting Developer settings on the left navigation menu, then Personal Access Tokens again on the left navigation menu)
- Click on Generate new token, give the token an appropriate name and check the checkbox on
public_repo
within the repo header - Copy the generated token
- Navigate to travis-ci.org and access the cooresponding repository there. Click on the More options button on the top right of the repository page and select Settings
- Scroll down to the section on Environment Variables and enter in a new NAME with
RELEASE_TOKEN
and the VALUE field cooresponding to the generated personal access token, and hit Add
On Gitlab
Gitlab is used to run tests and ensure that builds run correctly.
Version Bumping
- Run
make .ssh
- Copy the contents of the file generated at
./.ssh/id_rsa.base64
into an environment variable namedDEPLOY_KEY
in Settings > CI/CD > Variables - Navigate to the Deploy Keys section of the Settings > Repository > Deploy Keys and paste in the contents of the file generated at
./.ssh/id_rsa.pub
with the Write access allowed checkbox enabled
DEPLOY_KEY
: generate this by runningmake .ssh
and copying the contents of the file generated at./.ssh/id_rsa.base64
DockerHub Publishing
- Login to https://hub.docker.com, or if you're using your own private one, log into yours
- Navigate to your security settings at the
/settings/security
endpoint - Click on Create Access Token, type in a name for the new token, and click on Create
- Copy the generated token that will be displayed on the screen
- Enter the following varialbes into the CI/CD Variables page at Settings > CI/CD > Variables in your Gitlab repository:
DOCKER_REGISTRY_URL
: The hostname of the Docker registry (defaults todocker.io
if not specified)DOCKER_REGISTRY_USERNAME
: The username you used to login to the Docker registryDOCKER_REGISTRY_PASSWORD
: The generated access token
Licensing
Code in this package is licensed under the MIT license (click to see full text))
# Packages
No description provided by the author
# Functions
Check verifies that a connection can be made using the configuration provided in the connection :options parameter.
CloseAll attempts to close all established connections, returning a list of errors in cases where the connection could not be closed.
Get returns the instance of the database connection.
Import imports an existing database connection if another connection with the same name does not exist (an error is returned if so).
Init initialises the `db` module so that Get can be called anywhere in the consuming package.
# Constants
DefaultConnectionName is the assigned driver name when no .ConnectionName property is specified in Options.
DefaultDatabaseName is the default schema which the connection will connect to.
DefaultDriver is the assigned driver name when no .Driver property is specified in Options.
DefaultHostname is the default hostname at which the database server is reachable at.
DefaultPassword is the default password to use to login to the database service.
DefaultPortMySQL is the default MySQL port.
DefaultPortPostgreSQL is the default PostgreSQL port.
DefaultUser is the default username to use to login to the database service.
DriverMSSQL is the key for the Microsoft SQL Server driver.
DriverMySQL is the key for the MySQL driver.
DriverPostgreSQL is the key for the PostgreSQL driver.
# Variables
SupportedDrivers is a list of driver names which the `db` package supports.