2015-05-11 07:45:31 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"os"
|
|
|
|
|
2015-05-17 18:10:43 +00:00
|
|
|
"github.com/drone/drone/pkg/store"
|
|
|
|
"github.com/drone/drone/pkg/store/builtin/migrate"
|
2015-05-11 07:45:31 +00:00
|
|
|
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/BurntSushi/migration"
|
|
|
|
_ "github.com/drone/drone/Godeps/_workspace/src/github.com/lib/pq"
|
|
|
|
_ "github.com/drone/drone/Godeps/_workspace/src/github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/russross/meddler"
|
2015-05-11 07:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
driverPostgres = "postgres"
|
|
|
|
driverSqlite = "sqlite3"
|
|
|
|
driverMysql = "mysql"
|
|
|
|
)
|
|
|
|
|
2015-06-15 06:26:49 +00:00
|
|
|
func init() {
|
|
|
|
store.Register("sqlite3", NewDriver)
|
|
|
|
store.Register("postgres", NewDriver)
|
|
|
|
}
|
|
|
|
|
2015-08-06 15:54:47 +00:00
|
|
|
func NewDriver(driver, datasource string) (store.Store, error) {
|
2015-06-15 06:26:49 +00:00
|
|
|
conn, err := Connect(driver, datasource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return New(conn), nil
|
|
|
|
}
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
// Connect is a helper function that establishes a new
|
|
|
|
// database connection and auto-generates the database
|
|
|
|
// schema. If the database already exists, it will perform
|
|
|
|
// and update as needed.
|
|
|
|
func Connect(driver, datasource string) (*sql.DB, error) {
|
|
|
|
switch driver {
|
|
|
|
case driverPostgres:
|
|
|
|
meddler.Default = meddler.PostgreSQL
|
|
|
|
case driverSqlite:
|
|
|
|
meddler.Default = meddler.SQLite
|
|
|
|
}
|
|
|
|
|
|
|
|
migration.DefaultGetVersion = migrate.GetVersion
|
|
|
|
migration.DefaultSetVersion = migrate.SetVersion
|
|
|
|
var migrations = []migration.Migrator{
|
|
|
|
migrate.Setup,
|
|
|
|
}
|
|
|
|
return migration.Open(driver, datasource, migrations)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustConnect is a helper function that creates a
|
|
|
|
// new database connection and auto-generates the
|
|
|
|
// database schema. An error causes a panic.
|
|
|
|
func MustConnect(driver, datasource string) *sql.DB {
|
|
|
|
db, err := Connect(driver, datasource)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// mustConnectTest is a helper function that creates a
|
|
|
|
// new database connection using environment variables.
|
|
|
|
// If not environment varaibles are found, the default
|
|
|
|
// in-memory SQLite database is used.
|
|
|
|
func mustConnectTest() *sql.DB {
|
|
|
|
var (
|
|
|
|
driver = os.Getenv("TEST_DRIVER")
|
|
|
|
datasource = os.Getenv("TEST_DATASOURCE")
|
|
|
|
)
|
|
|
|
if len(driver) == 0 {
|
|
|
|
driver = driverSqlite
|
|
|
|
datasource = ":memory:"
|
|
|
|
}
|
|
|
|
db, err := Connect(driver, datasource)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Datastore
|
2015-05-17 18:10:43 +00:00
|
|
|
func New(db *sql.DB) store.Store {
|
2015-05-11 07:45:31 +00:00
|
|
|
return struct {
|
|
|
|
*Userstore
|
|
|
|
*Repostore
|
2015-06-23 03:45:08 +00:00
|
|
|
*Buildstore
|
2015-06-19 00:36:52 +00:00
|
|
|
*Jobstore
|
2015-05-11 07:45:31 +00:00
|
|
|
*Blobstore
|
|
|
|
*Starstore
|
|
|
|
*Tokenstore
|
2015-05-18 22:47:13 +00:00
|
|
|
*Agentstore
|
2015-05-11 07:45:31 +00:00
|
|
|
}{
|
|
|
|
NewUserstore(db),
|
|
|
|
NewRepostore(db),
|
2015-06-23 03:45:08 +00:00
|
|
|
NewBuildstore(db),
|
2015-06-19 00:36:52 +00:00
|
|
|
NewJobstore(db),
|
2015-05-11 07:45:31 +00:00
|
|
|
NewBlobstore(db),
|
|
|
|
NewStarstore(db),
|
|
|
|
NewTokenstore(db),
|
2015-05-18 22:47:13 +00:00
|
|
|
NewAgentstore(db),
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
|
|
|
}
|