mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-12-18 08:26:45 +02:00
e8b993e7da
This allows you to restrict email notifications to only be sent after the build changes from success to failure or failure to success. It errs on the side of sending the notification; if the build is in another state (hung, for instance) or there was no previous build on the branch the email will also be sent. Since the notify plugin shouldn't really have any responsibility for querying the database to find the previous commit's status, we store it on the commit when we save it.
91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"os"
|
|
|
|
"github.com/drone/drone/server/datastore"
|
|
"github.com/drone/drone/server/datastore/migrate"
|
|
|
|
"github.com/BurntSushi/migration"
|
|
_ "github.com/go-sql-driver/mysql"
|
|
_ "github.com/lib/pq"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/russross/meddler"
|
|
)
|
|
|
|
const (
|
|
driverPostgres = "postgres"
|
|
driverSqlite = "sqlite3"
|
|
driverMysql = "mysql"
|
|
)
|
|
|
|
// 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
|
|
case driverMysql:
|
|
meddler.Default = meddler.MySQL
|
|
}
|
|
migration.DefaultGetVersion = migrate.GetVersion
|
|
migration.DefaultSetVersion = migrate.SetVersion
|
|
var migrations = []migration.Migrator{
|
|
migrate.Setup,
|
|
migrate.Migrate_20142110,
|
|
migrate.Migrate_20143012,
|
|
}
|
|
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
|
|
func NewDatastore(db *sql.DB) datastore.Datastore {
|
|
return struct {
|
|
*Userstore
|
|
*Permstore
|
|
*Repostore
|
|
*Commitstore
|
|
}{
|
|
NewUserstore(db),
|
|
NewPermstore(db),
|
|
NewRepostore(db),
|
|
NewCommitstore(db),
|
|
}
|
|
}
|