Custom migration is not executed
I added an own migration to the db service like this:
db:
#container_name: wanderer-db
image: flomp/wanderer-db
depends_on:
search:
condition: service_healthy
environment:
<<: *cenv
POCKETBASE_ENCRYPTION_KEY: ${POCKETBASE_ENCRYPTION_KEY}
POCKETBASE_SUPERUSER_USERNAME: ${POCKETBASE_SUPERUSER_USERNAME}
POCKETBASE_SUPERUSER_PASSWORD: ${POCKETBASE_SUPERUSER_PASSWORD}
ports:
- "8090:8090"
networks:
- wanderer
restart: unless-stopped
volumes:
- db:/pb_data
- ./migrations/1745849033_create_superuser.go:/migrations/1745849033_create_superuser.go
The migration contains this:
package migrations
import (
"os"
"log"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
)
func init() {
username := os.Getenv("POCKETBASE_SUPERUSER_USERNAME");
password := os.Getenv("POCKETBASE_SUPERUSER_PASSWORD");
m.Register(func(app core.App) error {
if (username == "" || password == "") {
log.Printf("Username or password for superadmin needs to be set for auto creation");
return nil
}
collection, err := app.FindCollectionByNameOrId("_superusers")
record := core.NewRecord(collection)
record.set("email", username)
record.set("password", password)
log.Printf("Create superuser record for %s", username);
return app.Save(record)
}, func(app core.App) error {
record, err := app.FindAuthRecordByEmail("_superusers", username)
if err != nil {
return err
}
return app.Delete(record)
})
}
With that i start the services with an empty db folder. After the start i try to check if any migrations need to be executed on the shell with:
docker compose exec -it db sh
/ # /pocketbase migrate up
No new migrations to apply.
So the system is thinking that every migration is processed but the user is not added to _superusers.
What do i need to change, to get this working?
If the migration did not exist when the pocketbase binary was built, it will not execute. In case you really need this migration you will need to build your own docker image of wanderer-db.
However, if you are just trying to add a new superuser, you can simply follow the instructions at: https://wanderer.to/guides/custom-categories/#backend-access
I see that it's possible to add the user via shell.
My idea is, to setup the system without the need to manually interact with it. It's good to see that it's not necessary to access the webbackend to create the user, but i would prefer to not need to interact with it at all.
The next migration i'd like to integrate is the OIDC config. If i could get both running, i'd need to open port 8090 at all.
What is the reason that the migration doesn't get executed? Via the volume mapping the file is available in the /migrations folder. Is there something else needed to let the system know that migrations are present?
go is a compiled language. That means all the information is "baked" into the binary at build time. When you add a migration after building, the binary has no information about that migration.
Well thought that go / pocketbase would have found a solution around that. Sadly not so much. Thanks for explaining.