Rails remove old models with migrations

后端 未结 7 1916
傲寒
傲寒 2021-02-13 01:41

I have a bunch of rails models that i\'m re-writing into a single model to simplify my code and reduce unnecessary tables.

I\'m wondering what the best way to delete a

相关标签:
7条回答
  • 2021-02-13 02:04

    What about doing ruby script/destroy model? That should take care of the model and the migration.

    0 讨论(0)
  • 2021-02-13 02:06

    The question is a bit stale now, but I just did:

    rails destroy scaffold <ModelName> -p
    

    The -p flag shows "pretend" output, which is good for seeing what will happen. Remove the '-p' flag and the results will match the output. This cleaned the entire collection of M-V-C files + testing + js files + the original migration, no problem.

    I guess if you are one who likes to hand edit your migrations and include multiple steps in each, losing the original migration could break db:setup, so buyer beware. Keeping one action == one migration file should avoid this potential snafu.

    0 讨论(0)
  • 2021-02-13 02:09

    If you'd like to completely get rid of of a model and its table do this:

    rails destroy model Name
    
    0 讨论(0)
  • 2021-02-13 02:16

    Depending on how far into development or production you are, you may want to migrate the models out safely using a migration to remove/backup data or what not. Then as bobbywilson0 suggested, using

    script/destroy model
    

    or if you rspec anything

    script/destroy rspec_model
    

    This will remove any spec tests as well.

    Or you can always just drag them to the trash folder.

    0 讨论(0)
  • 2021-02-13 02:19

    If you'd rather have a manual based answer:

    First run the following command to identify which migrations you want removed:

    rake db:migrate:status
    

    Feel free to grep -i on it too if you're confident of your naming scheme.

    Make note of all the "add x to model name" and similar alterations to your Model. These can be removed using:

    rails d migration AddXToModelName
    

    Do this for every migration besides the initial create migration. The following command will take care of the initial create migration and the files associated with the model:

    rails d model ModelName
    
    0 讨论(0)
  • 2021-02-13 02:23

    You can take a look at this one at rails guide. But I suggest, if it is possible, you should delete the models and all references to the models. This will probably save time later as you don't need to maintain the dead code in the codebase.

    0 讨论(0)
提交回复
热议问题