github.com/wanliu/go-oauth2-server@v0.0.0-20180817021415-f928fa1580df/test-util/database.go (about) 1 package testutil 2 3 import ( 4 "fmt" 5 "log" 6 "os" 7 "os/exec" 8 9 "github.com/RichardKnop/go-fixtures" 10 "github.com/jinzhu/gorm" 11 "github.com/wanliu/go-oauth2-server/util/migrations" 12 // Drivers 13 _ "github.com/lib/pq" 14 _ "github.com/mattn/go-sqlite3" 15 ) 16 17 // CreateTestDatabase recreates the test database and 18 // runs migrations and fixtures as passed in, returning 19 // a pointer to the database 20 func CreateTestDatabase(dbPath string, migrationFunctions []func(*gorm.DB) error, fixtureFiles []string) (*gorm.DB, error) { 21 22 // Init in-memory test database 23 inMemoryDB, err := rebuildDatabase(dbPath) 24 if err != nil { 25 return nil, err 26 } 27 28 // Run all migrations 29 migrations.MigrateAll(inMemoryDB, migrationFunctions) 30 31 // Load data from data 32 if err = fixtures.LoadFiles(fixtureFiles, inMemoryDB.DB(), "sqlite"); err != nil { 33 return nil, err 34 } 35 36 return inMemoryDB, nil 37 } 38 39 // CreateTestDatabasePostgres is similar to CreateTestDatabase but it uses 40 // Postgres instead of sqlite, this is needed for testing packages that rely 41 // on some Postgres specifuc features (such as table inheritance) 42 func CreateTestDatabasePostgres(dbUser, dbName string, migrationFunctions []func(*gorm.DB) error, fixtureFiles []string) (*gorm.DB, error) { 43 44 // Postgres test database 45 db, err := rebuildDatabasePostgres(dbUser, dbName) 46 if err != nil { 47 return nil, err 48 } 49 50 // Run all migrations 51 migrations.MigrateAll(db, migrationFunctions) 52 53 // Load data from data 54 if err = fixtures.LoadFiles(fixtureFiles, db.DB(), "postgres"); err != nil { 55 return nil, err 56 } 57 58 return db, nil 59 } 60 61 // rebuildDatabase attempts to delete an existing in memory 62 // database and rebuild it, returning a pointer to it 63 func rebuildDatabase(dbPath string) (*gorm.DB, error) { 64 // Delete the current database if it exists 65 os.Remove(dbPath) 66 67 // Init a new in-memory test database connection 68 inMemoryDB, err := gorm.Open("sqlite3", dbPath) 69 if err != nil { 70 return nil, err 71 } 72 return inMemoryDB, nil 73 } 74 75 // rebuildDatabase attempts to delete an existing Postgres 76 // database and rebuild it, returning a pointer to it 77 func rebuildDatabasePostgres(dbUser, dbName string) (*gorm.DB, error) { 78 dropPostgresDB(dbUser, dbName) 79 80 if err := createPostgresDB(dbUser, dbName); err != nil { 81 return nil, err 82 } 83 84 return openPostgresDB(dbUser, dbName) 85 } 86 87 func openPostgresDB(dbUser, dbName string) (*gorm.DB, error) { 88 // Init a new postgres test database connection 89 db, err := gorm.Open("postgres", 90 fmt.Sprintf( 91 "sslmode=disable host=localhost port=5432 user=%s password='' dbname=%s", 92 dbUser, 93 dbName, 94 ), 95 ) 96 if err != nil { 97 return nil, err 98 } 99 return db, nil 100 } 101 102 func createPostgresDB(dbUser, dbName string) error { 103 // Create a new test database 104 createDbCmd := fmt.Sprintf("createdb -U %s %s", dbUser, dbName) 105 log.Println(createDbCmd) 106 out, err := exec.Command("sh", "-c", createDbCmd).Output() 107 if err != nil { 108 log.Printf("%v", string(out)) 109 return err 110 } 111 return nil 112 } 113 114 func dropPostgresDB(dbUser, dbName string) { 115 // Delete the current database if it exists 116 dropDbCmd := fmt.Sprintf("dropdb --if-exists -U %s %s", dbUser, dbName) 117 fmt.Println(dropDbCmd) 118 exec.Command("sh", "-c", dropDbCmd).Output() 119 }