How to unf*ck Mastodon
Hello! If you found this page for a reason, I feel sorry for you. Thereâs quite a lot of reasons and ways that Mastodon can break, so I figured Iâd start a little place to collect all these issues, also to remind myself on how I fixed them
Clearing the cache with tootctl
Mastodon at least provides you with a useful little tool to manage and reset things in case of breakage; tootctl
.
One thing that is very worth attempting in the case you ran your migrations, but there are still problems, is to clear the cache.
To clear the cache, simply run: tootcl cache clear
Database Trouble
This is probably going to be your main pain point, as both PostgreSQL can be a pain between upgrades, as well as Rails and Mastodon itself.
Failed/Pending migrations
When I started writing this post, the reason was that I just spent a good few hours trying to fix Mastodon after it had half-completed a bunch of migrations.
The main giveaway for this would be if you start your server, it appears to run, but occasionally thereâs some failed executions of Puma with some PostgreSQL related errors that say something like â ActionView::Template::Error â ⌠âDid you mean? {SOME TABLES OR USERS OR ENTITIES}â
To check all migrations have run successfully, from your mastodon installation, run: RAILS_ENV=production bundle exec rake db:migrate:status
If you run docker-compose, you probably want to run something like docker compose run -i web bash
(Mastodon does not have to run for this)
If you see any status marked as âdownâ here, now would be a good time to upgrade. NOTE: Mastodon MUST be stopped before doing this, or you will corrupt your database.
If you see any status marked as *** NO FILE ***
then something went wrong, you might have to update your local git repo (that is updated separately from the container itself), and then run the migrations again. If it persists, see âFailed migrationsâ.
After you stopped Mastodon:
- run
docker compose run -i web bash
again if you use docker-compose- this should also automatically bring up the postgres database and redis - if you run it in some other way, spin up the database before running the rails bundle command below
- run
RAILS_ENV=production bundle exec rake db:migrate
and do NOT interrupt this process, it should automatically finish - If it has finished, you can exit out of the container if you use docker, and restart your mastodon server as normal. If all goes well it should come back up, if not.. see the next section
Failed migrations
If you still see *** NO FILE ***
or something else is wack and you suspect that the migration didnât go over succesfully, you might have to remove the migrations from the migrations table and run them again. Iâm going to use pgAdmin to edit the database, also since showing this here may be useful for other reasons in the future.
- Again make sure Mastodon is not running
- If you use docker-compose, edit the docker-compose.yml file so it contains pgadmin:
pgadmin: image: dpage/pgadmin4 environment: PGADMIN_DEFAULT_EMAIL: "admin@admin.com" # Set your desired email and password PGADMIN_DEFAULT_PASSWORD: "adminpassword" volumes: - ./pgadmin_data:/var/lib/pgadmin ports: - "127.0.0.1:8080:80" # This would make pgAdmin accessible at http://localhost:8080 on your machine networks: - internal_network - external_network depends_on: - db
- Make sure to edit the networks such that it can contact your mastodon postgresql server
- If you donât use docker-compose, run pgadmin somehow and make sure it has access to the same network as the mastodon postgres server is on
- Go to your schema_migrations table and check all the migrations listed in it.
- Run this query:
DELETE FROM schema_migrations WHERE version::bigint > [last_known_good_migration]
where âlast_known_good_migrationâ is the last migration that you know worked out, or where you want to start (donât go too far back because itâs an intensive process) - Now that you deleted that migration, run the rails migration mentioned above again.
- if the migration fails, check the output, if it mentions that the key/constraint/whatever already exists, re-add the migration to the database using this query:
INSERT INTO schema_migrations (version) VALUES ('MIGRATION_THAT_YOU_JUST_RAN');
- repeat until youâre back at the latest migration
- once youâre done, try and boot mastodon again.