[#4600] autorun migrations for the test app and call the OnTerminate hook on TestApp.Cleanup

This commit is contained in:
Gani Georgiev
2024-03-20 22:42:20 +02:00
parent 48153d4542
commit 03cec9a5ac
2 changed files with 47 additions and 0 deletions
+40
View File
@@ -9,8 +9,12 @@ import (
"runtime"
"sync"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/migrations/logs"
"github.com/pocketbase/pocketbase/tools/mailer"
"github.com/pocketbase/pocketbase/tools/migrate"
)
// TestApp is a wrapper app instance used for testing.
@@ -39,6 +43,7 @@ func (t *TestApp) Cleanup() {
t.ResetEventCalls()
t.ResetBootstrapState()
t.TestMailer.Reset()
t.OnTerminate().Trigger(&core.TerminateEvent{App: t})
if t.DataDir() != "" {
os.RemoveAll(t.DataDir())
@@ -115,6 +120,11 @@ func NewTestApp(optTestDataDir ...string) (*TestApp, error) {
return nil, err
}
// apply any missing migrations
if err := runMigrations(app); err != nil {
return nil, err
}
// force disable request logs because the logs db call execute in a separate
// go routine and it is possible to panic due to earlier api test completion.
app.Settings().Logs.MaxDays = 0
@@ -545,3 +555,33 @@ func copyFile(src string, dest string) error {
return nil
}
// @todo replace with app.RunMigrations on merge with the refactoring.
func runMigrations(app core.App) error {
connections := []struct {
db *dbx.DB
migrationsList migrate.MigrationsList
}{
{
db: app.DB(),
migrationsList: migrations.AppMigrations,
},
{
db: app.LogsDB(),
migrationsList: logs.LogsMigrations,
},
}
for _, c := range connections {
runner, err := migrate.NewRunner(c.db, c.migrationsList)
if err != nil {
return err
}
if _, err := runner.Up(); err != nil {
return err
}
}
return nil
}