/
database.go
134 lines (106 loc) · 3.31 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package testutil
import (
//"github.com/RichardKnop/go-fixtures"
"context"
"database/sql"
"fmt"
"os"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dbfixture"
"github.com/uptrace/bun/dialect/sqlitedialect"
// Drivers
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
//CreateTestDatabase recreates the test database and
//runs migrations and fixtures as passed in, returning
//a pointer to the database
func CreateTestDatabase(dbPath string, fixtureFiles []string) (*bun.DB, error) {
// Init in-memory test database
inMemoryDB, err := rebuildDatabase(dbPath)
if err != nil {
return nil, err
}
ctx := context.Background()
// Run all migrations
migrations.Init(ctx, inMemoryDB)
migrations.Migrate(ctx, inMemoryDB)
//migrations.MigrateAll(inMemoryDB, migrationFunctions)
// Load data from data
// if err = fixtures.LoadFiles(fixtureFiles, inMemoryDB.DB(), "sqlite"); err != nil {
// return nil, err
// }
for _, fixtureFile := range fixtureFiles {
fixture := dbfixture.New(inMemoryDB)
fixture.Load(ctx, os.DirFS("oauth/fixtures"), fixtureFile)
}
return inMemoryDB, nil
}
//CreateTestDatabasePostgres is similar to CreateTestDatabase but it uses
//Postgres instead of sqlite, this is needed for testing packages that rely
//on some Postgres specifuc features (such as table inheritance)
func CreateTestDatabasePostgres(dbHost, dbUser, dbName string, migrationFunctions []func(*bun.DB) error, fixtureFiles []string) (*bun.DB, error) {
// Postgres test database
db, err := rebuildDatabasePostgres(dbHost, dbUser, dbName)
if err != nil {
return nil, err
}
// Run all migrations
migrations.MigrateAll(db, migrationFunctions)
// Load data from data
if err = fixtures.LoadFiles(fixtureFiles, db.DB(), "postgres"); err != nil {
return nil, err
}
return db, nil
}
//rebuildDatabase attempts to delete an existing in memory
//database and rebuild it, returning a pointer to it
func rebuildDatabase(dbPath string) (*bun.DB, error) {
// Delete the current database if it exists
sqldb, err := sql.Open("sqlite3", ":memory:?cache=shared")
if err != nil {
panic(err)
}
inMemoryDB, err := bun.NewDB(sqldb, sqlitedialect.New())
// err := os.Remove(dbPath)
// if err != nil {
// return nil, err
// }
// Init a new in-memory test database connection
//inMemoryDB, err := gorm.Open("sqlite3", dbPath)
// inMemoryDB, err := sql.Open("sqlite3", ":memory:?cache=shared")
if err != nil {
return nil, err
}
return inMemoryDB, nil
}
//rebuildDatabase attempts to delete an existing Postgres
//database and rebuild it, returning a pointer to it
func rebuildDatabasePostgres(dbHost, dbUser, dbName string) (*bun.DB, error) {
db, err := openPostgresDB(dbHost, dbUser, "template1")
if err != nil {
return nil, err
}
if err := db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s;", dbName)).Error; err != nil {
return nil, err
}
if err := db.Exec(fmt.Sprintf("CREATE DATABASE %s;", dbName)).Error; err != nil {
return nil, err
}
return openPostgresDB(dbHost, dbUser, dbName)
}
func openPostgresDB(dbHost, dbUser, dbName string) (*bun.DB, error) {
// Init a new postgres test database connection
db, err := gorm.Open("postgres",
fmt.Sprintf(
"sslmode=disable host=%s port=5432 user=%s password='' dbname=%s",
dbHost,
dbUser,
dbName,
),
)
if err != nil {
return nil, err
}
return db, nil
}