1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-18 13:47:47 +02:00

[#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

View File

@ -1,8 +1,14 @@
## (WIP) v0.22.5
- Minor test helpers fixes ([#4600](https://github.com/pocketbase/pocketbase/issues/4600)):
- Call the `OnTerminate` hook on `TestApp.Cleanup()`.
- Automatically run the DB migrations on initializing the test app with `tests.NewTestApp()`.
- Added more elaborate warning message when restoring a backup explaining how the operation works.
- Skip irregular files (symbolic links, sockets, etc.) when restoring a backup zip from the Admin UI or calling `archive.Extract(src, dst)` because they come with too many edge cases and ambiguities.
<details>
<summary><b><i>More contex</i></b></summary>
This was initially reported as security issue (_thanks Harvey Spec_) but in the PocketBase context it is not something that can be exploited without an admin intervention and since the general expectations are that the PocketBase admins can do anything and they are the one who manage their server, this should be treated with the same diligence when using `scp`/`rsync`/`rclone`/etc. with untrusted file sources.
@ -11,6 +17,7 @@
**Or in other words, if someone sends you a file and tell you to upload it to your server (either as backup zip or manually via scp) obviously you shouldn't do that unless you really trust them.**
Keep in mind that there is no "sandbox" at the moment for what admins (or the PocketBase process in general) can execute. This is left to the developers to restrict on application or OS level depending on their needs. If you are self-hosting PocketBase you usually don't have to do that, but if you are offering PocketBase as a service and allow strangers to run their own PocketBase instances on your server then you'll need to implement the isolation mechanisms on your own.
</details>
## v0.22.4

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
}