# Building the image without touching the database

**URL:** <https://meta.discourse.org/t/building-the-image-without-touching-the-database/171454>\
**Category:** Self-hosting\
**Created:** [November 27, 2020, 12:33am UTC](https://meta.discourse.org/t/building-the-image-without-touching-the-database/171454 "2020-11-27T00:33:22Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![pschichtel](https://avatars.discourse-cdn.com/v4/letter/p/6a8cbe/32.png) [@pschichtel](https://meta.discourse.org/u/pschichtel)\
**Post date:** [August 15, 2023, 10:53pm UTC](https://meta.discourse.org/t/building-the-image-without-touching-the-database/171454/11 "2023-08-15T22:53:51Z")

</div>

Ok after having a look at the code I understand what `db:ensure_post_migrations` does. It’s supposed to be used in the same rake execution before db:migrate to ensure that `SKIP_POST_DEPLOYMENT_MIGRATIONS` is set to `0`. My script already ensures that:

the `.gitlab-ci.yml`:

```bash
./migrate.sh pre || echo "Redis not running during pre migrations, skipping..."
docker stack deploy --prune --resolve-image always -c "$STACK.yml" "$STACK"
./docker-stack-wait.sh -t 180 "$STACK"
./migrate.sh post

```

the `migrate.sh`:

```bash
#!/usr/bin/env sh

if ["$(docker ps -q --filter "label=com.docker.stack.namespace=${STACK}" --filter "label=com.docker.swarm.service.name=${STACK}_${DISCOURSE_REDIS_HOST}" | wc -l)" = "0"]
then
    echo "No redis container found, unable to run migrations!"
    exit 1
fi

if ["$1" = "pre"]
then
    skip_post=1
else
    skip_post=0
fi

docker run \
    --rm \
    --name "discourse-migration-${DISCOURSE_DB_HOST}-${DISCOURSE_DB_NAME}" \
    --network "${STACK}_discourse" \
    --workdir /var/www/discourse \
    -u discourse \
    -e SKIP_POST_DEPLOYMENT_MIGRATIONS="$skip_post" \
    -e LANG="${LANG}" \
    -e DISCOURSE_DEFAULT_LOCALE="${DISCOURSE_DEFAULT_LOCALE}" \
    -e DISCOURSE_HOSTNAME="${DISCOURSE_HOSTNAME}" \
    -e DISCOURSE_DEVELOPER_EMAILS="${DISCOURSE_DEVELOPER_EMAILS}" \
    -e DISCOURSE_SMTP_ADDRESS="${DISCOURSE_SMTP_ADDRESS}" \
    -e DISCOURSE_SMTP_PORT="${DISCOURSE_SMTP_PORT}" \
    -e DISCOURSE_DB_USERNAME="${DISCOURSE_DB_USERNAME}" \
    -e DISCOURSE_DB_PASSWORD="${DISCOURSE_DB_PASSWORD}" \
    -e DISCOURSE_DB_HOST="${DISCOURSE_DB_HOST}" \
    -e DISCOURSE_DB_NAME="${DISCOURSE_DB_NAME}" \
    -e DISCOURSE_REDIS_HOST="${DISCOURSE_REDIS_HOST}" \
    "$DISCOURSE_IMAGE" \
    bundle exec rake db:migrate

```

It runs `db:migrate` with `SKIP_POST_DEPLOYMENT_MIGRATIONS=1` in new image on the docker swarm while discourse is still running the old version. Then it deploys the new image to the swarm and waits for that to converge. At the end it runs `db:migrate` again, but with `SKIP_POST_DEPLOYMENT_MIGRATIONS=0`.

This has worked reliably for every version for over 2 years now. Given that it worked for you @simonk, did you do something fundamentally different compared to my script?

---

_[View the full topic](https://meta.discourse.org/t/building-the-image-without-touching-the-database/171454)._
