2014-02-07 10:10:01 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
|
2014-03-08 05:19:28 +00:00
|
|
|
"github.com/drone/drone/pkg/database/migrate"
|
2014-02-07 10:10:01 +00:00
|
|
|
"github.com/drone/drone/pkg/database/schema"
|
2014-03-08 05:19:28 +00:00
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
_ "github.com/lib/pq"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
|
|
|
"github.com/russross/meddler"
|
2014-02-07 10:10:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// global instance of our database connection.
|
|
|
|
var db *sql.DB
|
|
|
|
|
2014-03-08 05:19:28 +00:00
|
|
|
func Init(name, datasource string) error {
|
|
|
|
driver := map[string]struct {
|
|
|
|
Md *meddler.Database
|
|
|
|
Mg migrate.DriverFunction
|
|
|
|
}{
|
|
|
|
"sqlite3": {
|
|
|
|
meddler.SQLite,
|
|
|
|
migrate.SQLite,
|
|
|
|
},
|
|
|
|
"mysql": {
|
|
|
|
meddler.MySQL,
|
|
|
|
migrate.MySQL,
|
|
|
|
},
|
|
|
|
"postgresql": {
|
|
|
|
meddler.PostgreSQL,
|
|
|
|
migrate.PostgreSQL,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
meddler.Default = driver[name].Md
|
|
|
|
migrate.Driver = driver[name].Mg
|
|
|
|
|
|
|
|
db, err := sql.Open(name, datasource)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
Set(db)
|
|
|
|
|
|
|
|
migration := migrate.New(db)
|
|
|
|
migration.All().Migrate()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-07 10:10:01 +00:00
|
|
|
// Set sets the default database.
|
|
|
|
func Set(database *sql.DB) {
|
|
|
|
// set the global database
|
|
|
|
db = database
|
|
|
|
|
|
|
|
// load the database schema. If this is
|
|
|
|
// a new database all the tables and
|
|
|
|
// indexes will be created.
|
|
|
|
if err := schema.Load(db); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|