Remove that Migration in Rails 7.2

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.

要查看或添加评论,请登录

Mason Roberts的更多文章

  • A Simple Concept - part 05

    A Simple Concept - part 05

    Now that we have an admin, let's give them a dashboard! So what are we going to need here? Holding true to the Rails…

  • A Simple Concept - 04

    A Simple Concept - 04

    Alright! We are doing great work i think. This article will dive into Devise.

  • A simple concept -03

    A simple concept -03

    We are making good progress I think. Have a home page with a navbar, footer, hero section and the newsletter form that…

  • A simple concept -02

    A simple concept -02

    Alright! In the first article we talked about what we were building. In the second article we created the base app and…

  • A simple concept - 01

    A simple concept - 01

    What do you say to getting this started? I can't wait either! If you have not played with Rails, you are about to have…

  • A simple concept | Rails 00

    A simple concept | Rails 00

    I love iced coffee. Caramel or French Vanilla please.

  • When wrong is not wrong

    When wrong is not wrong

    If you are new to development, there is one thing I would like to tell you, that others may have tried to say, but you…

  • Rails Blog. Prerequisite

    Rails Blog. Prerequisite

    Have you ever wanted a place online to write articles, give opinions, share your thoughts and voice with the world. ME…

  • PostgreSQL Options - Rails 7

    PostgreSQL Options - Rails 7

    When it comes to databases you have choices. Many choices.

  • Rails 7, AWS S3, & Codeium

    Rails 7, AWS S3, & Codeium

    I have been playing around with Rails 7 lately. I love it.

社区洞察

其他会员也浏览了