Remove that Migration in Rails 7.2
I am into updating various freelance profiles to reflect what I can do in Rails. That is what I want to work with or specialize in. I have been working on a project called Copywriter. This is a Figma design that I created many years back, that is being modified to redesign the LKM Creative site which is a team of brilliant copywriters based in South Africa.
I knew that the site would have several models and I wanted a Admin to be able to create those records, so when I installed Devise, I created the admin model. Now that I am about to create the dashboard and I am thinking things through, I realize that I really needed the admin role.
Every decisions has a effect. On a personal level, I am having financial concerns. So I plan to set this up on Render. Making this decision means that some functionality will need to be compromised. I can't use a email service because render does not allow access to the ports I will need. So I plan to create a internal email service within the app. Nothing fancy. Super basic. With that decision, I need to have users that can be created by the admin but can not register themselves. I could just keep the admin and use roles in that model, but that would be unnecessarily confusing to others.
So, let's rollback that migration. How will I do that? Let's get at it.
First, what do I really need to do here? I need to find the time stamp of the migration that created the Admin model. I need to remove the admin.rb in the models folder, and I need to remove the admin route.
For me the migration that created the admin model is labeled: 20241117012325_devise_create_admins.rb. I need that time stamp at the beginning for the command to rollback. I did not use the rollback command however. When I did so I got a error that suggested using "db:migrate:down". Here is the full command I used to rollback that specific migration.
领英推荐
bin/rails db:migrate:down VERSION=20241117012325
Rails then confirmed the rollback. Here is the log I got back.
== 20241117012325 DeviseCreateAdmins: reverting ===============================
-- remove_index(:admins, :reset_password_token, {:unique=>true})
-> 0.0762s
-- remove_index(:admins, :email, {:unique=>true})
-> 0.0060s
-- drop_table(:admins)
-> 0.0045s
== 20241117012325 DeviseCreateAdmins: reverted (0.0950s) ======================
With the migration gone, I now delete the admin.rb model file in the models folder. I no longer need it. I remove the devise admin route from the routes.rb file and all done! No more admin.
Now I can use Devise to create a User model and add the enum for the roles. For me, there is a little more clean up so if you have written test and created the admin in your fixture, make sure to clean that up. I will just delete that fixture as I no longer have the admin. I also will fix my seed file as I no longer have a admin.
If you were have a issue, or were wondering, or needed a updated way to rollback and not use "rake" then I hope this helps.