0
  1. first I have my application and I'm on master, I created a branch called "one" and I switched to it.
  2. I wrote some code then commited it, then I created a model called "category" and commited it
  3. now I don't want this table to exist no more, so I wanted to revert back to the older commit.
  4. I wrote "git log" and I got the ssh of my older commits when there was no category table created yet, then I created a new branch from one of the old commits ,the new branch called "two", then I switched to "two" branch
  5. this means that, now on the new branch called "two", I have no category model created, because this branch was created from an old commit from branch "one" where there's no category model created yet
  6. but what I found was strange, when opening the application on sublime text, there's no model called category, and there are no migrations for a model called category ....... while when try to view the database of the application using "db browser for sqlite", I still see the category model exist even after refresh, and when trying to create the category model on branch "two" and migrate it, there's an error stating that the category model already exists ....

so how could this happen, and do you know any ways to revert back in commits and remove completly a table created before ?

ELTA
  • 1,474
  • 2
  • 12
  • 25
  • When you switch branches back to an older version, do you notice any migrations at all that have anything to do with `category`? (columns, tables, etc.) – Jake Jun 16 '18 at 15:00

3 Answers3

1

Even though you removed the migration file the table is already created. So you could either create a migration to drop the table or recreate the database with the current schema, which does not have the category migration.

The reason is - once the migration runs, the table is created. It will stay there until it is removed or altered, regardless of the files in the application. So you have to create a migration to drop it and then run that after creating it.

Create migration

rails g migration drop_category_table

Inside the file

def change
  drop_table :category
end
kiddorails
  • 12,961
  • 2
  • 32
  • 41
Rockwell Rice
  • 3,376
  • 5
  • 33
  • 61
1

If you cannot figure this out with using git rollbacks, I recommend dropping the table via migration. In your case you could do the following:

In Terminal/Command: rails g migration DropCategoryTable

class DropCategoryTable < ActiveRecord::Migration
  def up
    drop_table :category
  end

  def down
    # recreate table logic here
  end
end

Then back in Terminal/Command: rails db:migrate

Jake
  • 1,086
  • 12
  • 38
0

Your database is a separate entity to your application, so it’s state isn’t a part of your version control or git repo... there’s no clever hooks that exist to help revert the database when you revert commits because reverting commits will only delete the model and migration files, but not the table in the database itself

so You’ll have to rollback the database migrations before reverting your commit.

rake db:rollback

before you git revert.

Conclusion: to drop a table, you have one of two options

First Option

"rollback to remove the model from database + git log to get list of all git versions you have and choose one of them + checkout to that older version of git and create a branch of it which will delete the left migration and model files of the deleted model"

rake db:rollback
git log
git checkout -b old-state 0d1d7fc32

Second Option

"rollback to remove the model + rails destroy to delete the left migration and model files without any need to checkout to older version of git where the files are already deleted"

  rake db:rollback
  rails destroy model <model_name>
Community
  • 1
  • 1
ELTA
  • 1,474
  • 2
  • 12
  • 25