Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 - PostgreSQL removing migration where column and index has already been destroyed

I have already destroyed a scaffold "Location" and its table but I forgot to rollback/remove a migrated column "slug and its index" before destroying the "Location" table . How can I remove the migration for slug and index.

Migrated slug column

class AddSlugToLocations < ActiveRecord::Migration[5.1]
  def change
    add_column :locations, :slug, :string
    add_index :locations, :slug
  end
end

Your help will be really appreciated, heroku won't deploy because of this issue

I have tried ActiveRecord::SchemaMigration.where(version=20180412191332).delete_all but no luck

Also tried rake db:migrate:down VERSION=20180412191332 but giving error: No indexes found on locations with the options provided.

like image 598
Charlie Avatar asked Dec 11 '22 07:12

Charlie


2 Answers

You manually deleted a table from your application's database. Now running migrations raises an error because one of the migrations is trying to alter the table that doesn't exist anymore.

Just delete the failing migration from your codebase and redeploy your application.

From your question, I assume that the migration with the number 20180412191332 causes the issue. Find that migration file in db/migrate/20180412191332_...rb, delete it and redeploy to Heroku.

like image 74
spickermann Avatar answered Jan 11 '23 23:01

spickermann


If you're having trouble deleting the migration from your, schema, you could try to do it in the SQL console :

USE mytable/dev;
DELETE FROM schema_migrations WHERE version = '20180412191332';
like image 41
Samy Kacimi Avatar answered Jan 12 '23 00:01

Samy Kacimi