#how to init a db service so that the exec to init the db does not get cached?

1 messages · Page 1 of 1 (latest)

grand jungle
#

for some reason, this is getting cached, which means the schema doesn't get created:

func (m *CassandraProxy) PostgresService(directoryArg *dagger.Directory) *dagger.Service {
    c := dag.Container().
        From("postgres:15").
        WithEnvVariable("POSTGRES_USER", "postgres").
        WithEnvVariable("POSTGRES_PASSWORD", "postgres").
        WithEnvVariable("POSTGRES_DB", "postgres").
        WithEnvVariable("POSTGRES_HOST", "db-postgres")
    svc := c.WithExec([]string{"docker-entrypoint.sh", "postgres"}).
        WithExposedPort(5432).AsService()
    initCmd := `until psql -e \
            -h postgres -U postgres -d postgres \
            -c 'CREATE SCHEMA "test"'; do
            echo 'retrying schema creation';
            done;`
    _, err := c.
        WithServiceBinding("postgres", svc).
        WithEnvVariable("PGPASSWORD", "postgres").
        WithExec([]string{"sh", "-xc",
            initCmd,
        }).Sync(context.Background())

    if err != nil {
        panic(fmt.Errorf("could not init postgres db: %s", err))
    }
    return svc
}
narrow rivet
#

I've run into this as well, and I put a cache buster at the top of the dag for the service. I find that it's fine performance-wise because setting up the database isn't a very long process anyway so it's fine if it's not cached

postgres := dag.Container().From("postgres").
        WithEnvVariable("NOCACHE", time.Now().String()).
        WithEnvVariable("POSTGRES_PASSWORD", "postgres").
        WithExposedPort(5432).AsService()