Skip to content

Commit

Permalink
Improve db-migrate and db-rollback commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dhelonious committed Sep 19, 2024
1 parent a7c0686 commit f1857c9
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 17 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,12 @@ Statistics are compiled every night. So the number of users should be correct wi

Go to *Preferences/Administration/Server settings/Branding* and add your *Contact username* and *Contact e-mail*.

## Requests fail after upgrade

If some requests fail after upgrading to a newer version, and there are log entries showing errors about missing columns and tables, the database has not been migrated properly. Use `mastodon-server.db-migrate` to start the migration manually. Use `mastodon-server.db-rollback` to rollback a database migration, which may be necessary when downgrading.

See the [Mastodon troubleshooting page](https://docs.joinmastodon.org/admin/troubleshooting/#after-an-upgrade-to-a-newer-version-some-requests-fail-and-the-logs-show-error-messages-about-missing-columns-or-tables-why) for more information.

## My postgres database has stopped working, how can I recover my data?

If your postgres database has stopped working, for example due to a failed upgrade, first backup the postgres data dir `/var/snap/mastodon-server/current/postgres/data/` and the password file `/var/snap/mastodon-server/common/secrets/postgres`.
Expand Down
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ Changelog for 4.3.0-beta.2snap1:
* Add explicit builds for libvips and ffmpeg
* Use non-root user for nginx, acme.sh, redis, and Mastodon (backend, streaming, sidekiq)
* Add redis database to exports
* Add `db-migrate` and `db-rollback` commands

The result is a lighter and more secure snap file with a smaller file size.
4 changes: 2 additions & 2 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ apps:
- network-bind

db-migrate:
command: bin/migrate
command: bin/db-migrate
plugs:
- network

db-rollback:
command: bin/rollback
command: bin/db-rollback
plugs:
- network

Expand Down
12 changes: 12 additions & 0 deletions src/mastodon/bin/db-migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash -e

. "$SNAP/mastodon.env"

echo "CAUTION: This command will make changes to your database!"
echo "Make sure that you have a recent database export."
read -rp "Do you want to continue? [y/N] " response
if ! [[ "$response" =~ ^([yY])$ ]]; then
exit 0
fi

mastodon_db_migrate "$@"
12 changes: 12 additions & 0 deletions src/mastodon/bin/db-rollback
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash -e

. "$SNAP/mastodon.env"

echo "CAUTION: This command will make changes to your database!"
echo "Make sure that you have a recent database export."
read -rp "Do you want to continue? [y/N] " response
if ! [[ "$response" =~ ^([yY])$ ]]; then
exit 0
fi

mastodon_db_rollback "$@"
6 changes: 0 additions & 6 deletions src/mastodon/bin/migrate

This file was deleted.

4 changes: 2 additions & 2 deletions src/mastodon/bin/migrate_if_required
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
. "$SNAP/mastodon.env"

if [ "$(snapctl get migration-required)" == true ]; then
$SNAP/bin/migrate
mastodon_db_migrate
elif [ "$(snapctl get pre-migration-required)" == true ]; then
SKIP_POST_DEPLOYMENT_MIGRATIONS=true $SNAP/bin/migrate
SKIP_POST_DEPLOYMENT_MIGRATIONS=true mastodon_db_migrate
fi

reset_migration_required
Expand Down
2 changes: 1 addition & 1 deletion src/mastodon/bin/restore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ echo Restore backup from $backup_dir

BACKUP_VERSION="$(cat $backup_dir/version)"
if [ "$BACKUP_VERSION" != "$SNAP_VERSION" ]; then
echo ATTENTION: You are trying to restore a backup from a different version!
echo CAUTION: You are trying to restore a backup from another version!
echo "Snap version: $SNAP_VERSION"
echo "Backup version: $BACKUP_VERSION"
echo The backup may be incompatible or changes may be required.
Expand Down
6 changes: 0 additions & 6 deletions src/mastodon/bin/rollback

This file was deleted.

10 changes: 10 additions & 0 deletions src/mastodon/mastodon.env
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,13 @@ check_status_max_chars_counter() {
export STATUS_MAX_CHARS_COUNTER="$STATUS_MAX_CHARS_COUNTER"
done
}

mastodon_db_migrate() {
postgres_waitready
mastodon_rails db:migrate "$@"
}

mastodon_db_rollback() {
postgres_waitready
mastodon_rails db:rollback "$@"
}

0 comments on commit f1857c9

Please sign in to comment.