Deal with a drop_table migration for nonexistent table on heroku

Just delete the offending migration and get on with more interesting things. Clearly there's no "create table" migration for that table so the "drop table" migration has no right to exist. If you really don't want to delete the migration, you can rewrite it using raw SQL:

def up
  connection.execute 'drop table if exists campaigns'
end

The if exists does exactly what you'd think: it only tries to drop the table if it is there.

About this stuff:

I thought you weren't supposed to delete migrations so that anyone else that works on the project in the future can run them and have an accurate DB.

Migrations are meant to get an existing instance of an application from state-A to state-B. If you're starting a fresh instance, you'll be using schema.rb (or structure.sql) to initialize the application and no migrations should be needed. Also, once a migration has been applied to every instance of an application, that migration really isn't needed anymore unless you want to try and go backwards. I tend to clear out old migrations a couple weeks after a release to keep the clutter down.


You can turn off the error PG::UndefinedTable: ERROR: table "campaigns" does not exist when deleting an already non-existent table by adding if_exists: true to drop_table:

def up
  drop_table(:campaigns, if_exists: true)
end

This option is sparsely documented, but exists in Rails since early 2015 and I think it's cleaner than writing SQL by hand as it also handles compatibility with various DB adapters.