Skip to content

Commit

Permalink
Move db migration to postgres stop-command
Browse files Browse the repository at this point in the history
  • Loading branch information
dhelonious committed Sep 16, 2024
1 parent 7f0b474 commit 09e39df
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
6 changes: 1 addition & 5 deletions snap/hooks/post-refresh
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ if [ "$SNAP_VERSION" != "$CURRENT_SNAP_VERSION" ]; then
link_mastodon_config
upgrade_mastodon_config

echo Update mastodon database
mastodon_migrate_db

echo Remove logs and tempfiles
mastodon_rails log:clear tmp:clear
migration_required
fi
fi

Expand Down
2 changes: 2 additions & 0 deletions snap/hooks/pre-refresh
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ if [ "$PG_VERSION" != "$CURRENT_PG_VERSION" ]; then
echo Create postgres backup
pg_dumpall.wrapper -f "$SNAP_COMMON/database.sql"
fi

pre_migration_required
2 changes: 2 additions & 0 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ apps:

postgres:
command: bin/run_as_daemon_user postgres.wrapper
stop-command: bin/migrate
stop-timeout: 30m
daemon: simple
plugs:
- network
Expand Down
18 changes: 18 additions & 0 deletions src/mastodon/bin/migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash -e

. "$SNAP/mastodon.env"

if [ "$(snapctl get migration-required)" == true ]; then
postgres_waitready
mastodon_rails db:migrate
mastodon_rails log:clear tmp:clear
reset_migration_required
reset_pre_migration_required
elif [ "$(snapctl get pre-migration-required)" == true ]; then
postgres_waitready
SKIP_POST_DEPLOYMENT_MIGRATIONS=true \
mastodon_rails db:migrate
mastodon_rails log:clear tmp:clear
reset_pre_migration_required
fi

2 changes: 1 addition & 1 deletion src/mastodon/bin/restore
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ echo Restore database
$SNAP/bin/psql.wrapper -f "$backup_dir/database.sql" template1 > /dev/null
if [ "$BACKUP_VERSION" != "$SNAP_VERSION" ]; then
echo Migrate database
mastodon_migrate_db > /dev/null
$SNAP/bin/migrate > /dev/null
fi

if [ -f "$backup_dir/redis.rdb" ]; then
Expand Down
25 changes: 16 additions & 9 deletions src/mastodon/mastodon.env
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,6 @@ setup_mastodon_database() {
psql.wrapper -c "alter user $MASTODON_DBUSER createdb;"
}

mastodon_migrate_db() {
postgres_waitready
mastodon_rails db:migrate
}

mastodon_migrate_db_pre_deployment() {
SKIP_POST_DEPLOYMENT_MIGRATIONS=true mastodon_migrate_db
}

generate_mastodon_vapid_secrets() {
eval $(mastodon_rails mastodon:webpush:generate_vapid_key)
export VAPID_PRIVATE_KEY="$VAPID_PRIVATE_KEY"
Expand Down Expand Up @@ -233,6 +224,22 @@ recompile_if_required() {
fi
}

migration_required() {
snapctl set migration-required=true
}

reset_migration_required() {
snapctl set migration-required=false
}

pre_migration_required() {
snapctl set pre-migration-required=true
}

reset_pre_migration_required() {
snapctl set pre-migration-required=false
}

get_status_max_chars_file() {
run_as_daemon_user sed -rn "s/^.*MAX_CHARS = ([0-9]+).*$/\1/p" "$status_length_validator"
}
Expand Down
Empty file modified src/snap/bin/save_file
100644 → 100755
Empty file.

0 comments on commit 09e39df

Please sign in to comment.