Backend: Refactor test db initialization
Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
parent
260cca91fe
commit
c30476f120
4 changed files with 48 additions and 30 deletions
|
@ -58,12 +58,18 @@ func (c *Config) CloseDb() error {
|
|||
// InitDb will initialize the database connection and schema.
|
||||
func (c *Config) InitDb() {
|
||||
entity.SetDbProvider(c)
|
||||
entity.Migrate()
|
||||
entity.MigrateDb()
|
||||
}
|
||||
|
||||
// DropTables drops all tables in the currently configured database (be careful!).
|
||||
func (c *Config) DropTables() {
|
||||
entity.DropTables(c.Db())
|
||||
// ResetDb drops all tables in the currently configured database and re-creates them.
|
||||
func (c *Config) ResetDb(testFixtures bool) {
|
||||
entity.SetDbProvider(c)
|
||||
entity.ResetDb(testFixtures)
|
||||
|
||||
// TODO: Remove when new test fixtures are ready
|
||||
if testFixtures {
|
||||
c.ImportSQL(c.ExamplesPath() + "/fixtures.sql")
|
||||
}
|
||||
}
|
||||
|
||||
// connectToDatabase establishes a database connection.
|
||||
|
|
|
@ -10,7 +10,6 @@ import (
|
|||
|
||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
"github.com/photoprism/photoprism/internal/entity"
|
||||
"github.com/photoprism/photoprism/internal/thumb"
|
||||
"github.com/photoprism/photoprism/pkg/capture"
|
||||
"github.com/photoprism/photoprism/pkg/fs"
|
||||
|
@ -103,20 +102,7 @@ func NewTestConfig() *Config {
|
|||
log.Fatalf("config: %s", err.Error())
|
||||
}
|
||||
|
||||
c.DropTables()
|
||||
|
||||
// Make sure changes have been written to disk.
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
|
||||
c.InitDb()
|
||||
|
||||
// Make sure changes have been written to disk.
|
||||
time.Sleep(250 * time.Millisecond)
|
||||
|
||||
entity.CreateTestFixtures()
|
||||
|
||||
// TODO: Remove when new test fixtures are ready
|
||||
c.ImportSQL(c.ExamplesPath() + "/fixtures.sql")
|
||||
c.ResetDb(true)
|
||||
|
||||
thumb.JpegQuality = c.ThumbQuality()
|
||||
thumb.PreRenderSize = c.ThumbSize()
|
||||
|
|
|
@ -10,6 +10,8 @@ https://github.com/photoprism/photoprism/wiki/Storage
|
|||
package entity
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/photoprism/photoprism/internal/event"
|
||||
)
|
||||
|
@ -22,8 +24,8 @@ func logError(result *gorm.DB) {
|
|||
}
|
||||
}
|
||||
|
||||
// Migrate creates all tables and inserts default entities as needed.
|
||||
func Migrate() {
|
||||
// MigrateDb creates all tables and inserts default entities as needed.
|
||||
func MigrateDb() {
|
||||
Db().AutoMigrate(
|
||||
&Account{},
|
||||
&File{},
|
||||
|
@ -53,8 +55,8 @@ func Migrate() {
|
|||
}
|
||||
|
||||
// DropTables drops database tables for all known entities.
|
||||
func DropTables(db *gorm.DB) {
|
||||
db.DropTableIfExists(
|
||||
func DropTables() {
|
||||
Db().DropTableIfExists(
|
||||
&Account{},
|
||||
&File{},
|
||||
&FileShare{},
|
||||
|
@ -76,3 +78,33 @@ func DropTables(db *gorm.DB) {
|
|||
&Link{},
|
||||
)
|
||||
}
|
||||
|
||||
// ResetDb drops database tables for all known entities and re-creates them with fixtures.
|
||||
func ResetDb(testFixtures bool) {
|
||||
DropTables()
|
||||
|
||||
// Make sure changes have been written to disk.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
MigrateDb()
|
||||
|
||||
if testFixtures {
|
||||
// Make sure changes have been written to disk.
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
CreateTestFixtures()
|
||||
}
|
||||
}
|
||||
|
||||
// InitTestDb connects to and completely initializes the test database incl fixtures.
|
||||
func InitTestDb(dsn string) *Gorm {
|
||||
db := &Gorm{
|
||||
Driver: "mysql",
|
||||
Dsn: dsn,
|
||||
}
|
||||
|
||||
SetDbProvider(db)
|
||||
ResetDb(true)
|
||||
|
||||
return db
|
||||
}
|
||||
|
|
|
@ -15,13 +15,7 @@ func TestMain(m *testing.M) {
|
|||
log.Out = &logBuffer
|
||||
log.SetLevel(logrus.DebugLevel)
|
||||
|
||||
db := &Gorm{
|
||||
Driver: "mysql",
|
||||
Dsn: "photoprism:photoprism@tcp(photoprism-db:4001)/photoprism?parseTime=true",
|
||||
}
|
||||
|
||||
SetDbProvider(db)
|
||||
Migrate()
|
||||
db := InitTestDb("photoprism:photoprism@tcp(photoprism-db:4001)/photoprism?parseTime=true")
|
||||
|
||||
code := m.Run()
|
||||
|
||||
|
|
Loading…
Reference in a new issue