2015-12-28 20:03:29 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"bitbucket.org/liamstask/goose/lib/goose"
|
|
|
|
"github.com/coreos/clair/config"
|
|
|
|
"github.com/coreos/clair/database"
|
2016-01-08 16:17:32 +00:00
|
|
|
cerrors "github.com/coreos/clair/utils/errors"
|
2015-12-28 20:03:29 +00:00
|
|
|
"github.com/coreos/pkg/capnslog"
|
|
|
|
"github.com/hashicorp/golang-lru"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
"github.com/pborman/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = capnslog.NewPackageLogger("github.com/coreos/clair", "pgsql")
|
|
|
|
|
|
|
|
type pgSQL struct {
|
|
|
|
*sql.DB
|
|
|
|
cache *lru.ARCCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQL) Close() {
|
|
|
|
pgSQL.DB.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open creates a Datastore backed by a PostgreSQL database.
|
|
|
|
//
|
|
|
|
// It will run immediately every necessary migration on the database.
|
|
|
|
func Open(config *config.DatabaseConfig) (database.Datastore, error) {
|
|
|
|
// Run migrations.
|
2016-01-08 16:17:32 +00:00
|
|
|
if err := migrate(config.Source); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return nil, database.ErrCantOpen
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open database.
|
|
|
|
db, err := sql.Open("postgres", config.Source)
|
|
|
|
if err != nil {
|
2016-01-08 16:17:32 +00:00
|
|
|
log.Error(err)
|
|
|
|
return nil, database.ErrCantOpen
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize cache.
|
|
|
|
// TODO(Quentin-M): Benchmark with a simple LRU Cache.
|
|
|
|
var cache *lru.ARCCache
|
|
|
|
if config.CacheSize > 0 {
|
|
|
|
cache, _ = lru.NewARC(config.CacheSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pgSQL{DB: db, cache: cache}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:17:32 +00:00
|
|
|
// migrate runs all available migrations on a pgSQL database.
|
|
|
|
func migrate(dataSource string) error {
|
2015-12-28 20:03:29 +00:00
|
|
|
log.Info("running database migrations")
|
|
|
|
|
|
|
|
_, filename, _, _ := runtime.Caller(1)
|
|
|
|
migrationDir := path.Join(path.Dir(filename), "/migrations/")
|
|
|
|
conf := &goose.DBConf{
|
|
|
|
MigrationsDir: migrationDir,
|
|
|
|
Driver: goose.DBDriver{
|
|
|
|
Name: "postgres",
|
|
|
|
OpenStr: dataSource,
|
|
|
|
Import: "github.com/lib/pq",
|
|
|
|
Dialect: &goose.PostgresDialect{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the most recent revision available from the migrations folder.
|
|
|
|
target, err := goose.GetMostRecentDBVersion(conf.MigrationsDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run migrations
|
|
|
|
err = goose.RunMigrations(conf, conf.MigrationsDir, target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("database migration ran successfully")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:17:32 +00:00
|
|
|
// createDatabase creates a new database.
|
2015-12-28 20:03:29 +00:00
|
|
|
// The dataSource parameter should not contain a dbname.
|
2016-01-08 16:17:32 +00:00
|
|
|
func createDatabase(dataSource, databaseName string) error {
|
2015-12-28 20:03:29 +00:00
|
|
|
// Open database.
|
|
|
|
db, err := sql.Open("postgres", dataSource)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open database (CreateDatabase): %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Create database.
|
2016-01-20 00:17:08 +00:00
|
|
|
_, err = db.Exec("CREATE DATABASE " + databaseName)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create database: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:17:32 +00:00
|
|
|
// dropDatabase drops an existing database.
|
2015-12-28 20:03:29 +00:00
|
|
|
// The dataSource parameter should not contain a dbname.
|
2016-01-08 16:17:32 +00:00
|
|
|
func dropDatabase(dataSource, databaseName string) error {
|
2015-12-28 20:03:29 +00:00
|
|
|
// Open database.
|
|
|
|
db, err := sql.Open("postgres", dataSource)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not open database (DropDatabase): %v", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
// Drop database.
|
2016-01-20 00:17:08 +00:00
|
|
|
_, err = db.Exec("DROP DATABASE " + databaseName)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2016-01-12 15:40:46 +00:00
|
|
|
return fmt.Errorf("could not drop database: %v", err)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pgSQLTest wraps pgSQL for testing purposes.
|
|
|
|
// Its Close() method drops the database.
|
|
|
|
type pgSQLTest struct {
|
|
|
|
*pgSQL
|
|
|
|
dataSource string
|
|
|
|
dbName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pgSQL *pgSQLTest) Close() {
|
|
|
|
pgSQL.DB.Close()
|
2016-01-08 16:17:32 +00:00
|
|
|
dropDatabase(pgSQL.dataSource+"dbname=postgres", pgSQL.dbName)
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenForTest creates a test Datastore backed by a new PostgreSQL database.
|
|
|
|
// It creates a new unique and prefixed ("test_") database.
|
|
|
|
// Using Close() will drop the database.
|
|
|
|
func OpenForTest(name string, withTestData bool) (*pgSQLTest, error) {
|
|
|
|
dataSource := "host=127.0.0.1 sslmode=disable "
|
|
|
|
dbName := "test_" + strings.ToLower(name) + "_" + strings.Replace(uuid.New(), "-", "_", -1)
|
|
|
|
|
|
|
|
// Create database.
|
2016-01-08 16:17:32 +00:00
|
|
|
err := createDatabase(dataSource+"dbname=postgres", dbName)
|
2015-12-28 20:03:29 +00:00
|
|
|
if err != nil {
|
2016-01-08 16:17:32 +00:00
|
|
|
log.Error(err)
|
|
|
|
return nil, database.ErrCantOpen
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open database.
|
|
|
|
db, err := Open(&config.DatabaseConfig{Source: dataSource + "dbname=" + dbName, CacheSize: 0})
|
|
|
|
if err != nil {
|
2016-01-08 16:17:32 +00:00
|
|
|
dropDatabase(dataSource, dbName)
|
|
|
|
log.Error(err)
|
|
|
|
return nil, database.ErrCantOpen
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load test data if specified.
|
|
|
|
if withTestData {
|
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
d, _ := ioutil.ReadFile(path.Join(path.Dir(filename)) + "/testdata/data.sql")
|
|
|
|
_, err = db.(*pgSQL).Exec(string(d))
|
|
|
|
if err != nil {
|
2016-01-20 00:17:08 +00:00
|
|
|
dropDatabase(dataSource+"dbname=postgres", dbName)
|
2016-01-08 16:17:32 +00:00
|
|
|
log.Error(err)
|
|
|
|
return nil, database.ErrCantOpen
|
2015-12-28 20:03:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pgSQLTest{pgSQL: db.(*pgSQL), dataSource: dataSource, dbName: dbName}, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:17:32 +00:00
|
|
|
// handleError logs an error with an extra description and masks the error if it's an SQL one.
|
|
|
|
// This ensures we never return plain SQL errors and leak anything.
|
|
|
|
func handleError(desc string, err error) error {
|
|
|
|
log.Errorf("%s: %v", desc, err)
|
|
|
|
|
|
|
|
if _, ok := err.(*pq.Error); ok {
|
|
|
|
return database.ErrBackendException
|
|
|
|
} else if err == sql.ErrNoRows {
|
|
|
|
return cerrors.ErrNotFound
|
2016-01-12 15:40:46 +00:00
|
|
|
} else if err == sql.ErrTxDone || strings.HasPrefix(err.Error(), "sql:") {
|
2016-01-08 16:17:32 +00:00
|
|
|
return database.ErrBackendException
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:03:29 +00:00
|
|
|
// isErrUniqueViolation determines is the given error is a unique contraint violation.
|
|
|
|
func isErrUniqueViolation(err error) bool {
|
|
|
|
pqErr, ok := err.(*pq.Error)
|
|
|
|
return ok && pqErr.Code == "23505"
|
|
|
|
}
|