// Init connects to database and performs migration if necessary. // // Database driver name and data source information is provided by user // from within command line, and error checking is deferred to sql.Open. // // Init will just bail out and returns error if driver name // is not listed, no fallback nor default driver sets here. func Init(name, datasource string) error { var err error driver := map[string]struct { Md *meddler.Database Mg migrate.DriverBuilder }{ "sqlite3": { meddler.SQLite, migrate.SQLite, }, "mysql": { meddler.MySQL, migrate.MySQL, }, } if drv, ok := driver[name]; ok { meddler.Default = drv.Md migrate.Driver = drv.Mg } else { return fmt.Errorf("%s driver not found", name) } db, err = sql.Open(name, datasource) if err != nil { return err } migration := migrate.New(db) if err := migration.All().Migrate(); err != nil { return err } return nil }
// setup the database connection and register with the // global database package. func setupDatabase() { // inform meddler and migration we're using sqlite meddler.Default = meddler.SQLite migrate.Driver = migrate.SQLite // connect to the SQLite database db, err := sql.Open(driver, datasource) if err != nil { log.Fatal(err) } database.Set(db) migration := migrate.New(db) migration.All().Migrate() }
func Setup() { // create an in-memory database db, _ = sql.Open("sqlite3", ":memory:") // make sure all the tables and indexes are created database.Set(db) migration := migrate.New(db) migration.All().Migrate() // create dummy user data user1 := User{ Password: "******", Name: "Brad Rydzewski", Email: "*****@*****.**", Gravatar: "8c58a0be77ee441bb8f8595b7f1b4e87", Token: "123", Admin: true} user2 := User{ Password: "******", Name: "Thomas Burke", Email: "*****@*****.**", Gravatar: "c62f7126273f7fa786274274a5dec8ce", Token: "456", Admin: false} user3 := User{ Password: "******", Name: "Carlos Morales", Email: "*****@*****.**", Gravatar: "c2180a539620d90d68eaeb848364f1c2", Token: "789", Admin: false} database.SaveUser(&user1) database.SaveUser(&user2) database.SaveUser(&user3) // create dummy team data team1 := Team{ Slug: "drone", Name: "Drone", Email: "*****@*****.**", Gravatar: "8c58a0be77ee441bb8f8595b7f1b4e87"} team2 := Team{ Slug: "github", Name: "Github", Email: "*****@*****.**", Gravatar: "61024896f291303615bcd4f7a0dcfb74"} team3 := Team{ Slug: "golang", Name: "Golang", Email: "*****@*****.**", Gravatar: "991695cc770c6b8354b68cd18c280b95"} database.SaveTeam(&team1) database.SaveTeam(&team2) database.SaveTeam(&team3) // create team membership data database.SaveMember(user1.ID, team1.ID, RoleOwner) database.SaveMember(user2.ID, team1.ID, RoleAdmin) database.SaveMember(user3.ID, team1.ID, RoleWrite) database.SaveMember(user1.ID, team2.ID, RoleOwner) database.SaveMember(user2.ID, team2.ID, RoleAdmin) database.SaveMember(user3.ID, team2.ID, RoleWrite) database.SaveMember(user1.ID, team3.ID, RoleOwner) // create dummy repo data repo1 := Repo{ Slug: "github.com/drone/drone", Host: "github.com", Owner: "drone", Name: "drone", Private: true, Disabled: false, SCM: "git", URL: "[email protected]:drone/drone.git", Username: "******", Password: "******", PublicKey: "public key", PrivateKey: "private key", UserID: user1.ID, TeamID: team1.ID, } repo2 := Repo{ Slug: "bitbucket.org/drone/test", Host: "bitbucket.org", Owner: "drone", Name: "test", Private: false, Disabled: false, SCM: "hg", URL: "https://bitbucket.org/drone/test", Username: "******", Password: "******", PublicKey: "public key", PrivateKey: "private key", UserID: user1.ID, TeamID: team1.ID, } repo3 := Repo{ Slug: "bitbucket.org/brydzewski/test", Host: "bitbucket.org", Owner: "brydzewski", Name: "test", Private: false, Disabled: false, SCM: "hg", URL: "https://bitbucket.org/brydzewski/test", Username: "******", Password: "******", PublicKey: "public key", PrivateKey: "private key", UserID: user2.ID, } database.SaveRepo(&repo1) database.SaveRepo(&repo2) database.SaveRepo(&repo3) commit1 := Commit{ RepoID: repo1.ID, Status: "Success", Hash: "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", Branch: "master", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } commit2 := Commit{ RepoID: repo1.ID, Status: "Failure", Hash: "0eb2fa13e9f4139e803b6ad37831708d4786c74a", Branch: "master", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } commit3 := Commit{ RepoID: repo1.ID, Status: "Failure", Hash: "60a7fe87ccf01d0152e53242528399e05acaf047", Branch: "dev", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } commit4 := Commit{ RepoID: repo2.ID, Status: "Success", Hash: "a4078d1e9a0842cdd214adbf0512578799a4f2ba", Branch: "master", Author: user1.Email, Gravatar: user1.Gravatar, Message: "commit message", } // create dummy commit data database.SaveCommit(&commit1) database.SaveCommit(&commit2) database.SaveCommit(&commit3) database.SaveCommit(&commit4) // create dummy build data database.SaveBuild(&Build{CommitID: commit1.ID, Slug: "node_0.10", Status: "Success", Duration: 60}) database.SaveBuild(&Build{CommitID: commit1.ID, Slug: "node_0.09", Status: "Success", Duration: 70}) database.SaveBuild(&Build{CommitID: commit2.ID, Slug: "node_0.10", Status: "Success", Duration: 10}) database.SaveBuild(&Build{CommitID: commit2.ID, Slug: "node_0.09", Status: "Failure", Duration: 65}) database.SaveBuild(&Build{CommitID: commit3.ID, Slug: "node_0.10", Status: "Failure", Duration: 50}) database.SaveBuild(&Build{CommitID: commit3.ID, Slug: "node_0.09", Status: "Failure", Duration: 55}) }