Removing a field by deleting it from migration and schema files

I am at the very beginning of my project. I just realized that I have a field in one of my tables that should not be there. I can easily do ecto.reset on my database because it is filled with seed data and junk. Just sanity checking that this is ok:

  1. I’ll delete the field in the migration file
  2. I’ll delete the field in the schema file AND update changeset so that it doesn’t try to set that field
  3. I’ll update my template file so that it doesn’t try to display that field anymore
  4. I’ll run ecto.migrate so that the table is updated in ecto and the column that was dedicated to that field is removed

Is that correct? Or do I need to go in and manually delete the column using Navicat?

It’s ok to do so, but You did not say how You create this resource… You might have to clean tests too :slight_smile:

But not point 4 where mix ecto.reset should do. Did You run the migration already? You also have mix ecto.rollback.

@kokolegorille I haven’t even gotten to tests. I’m still just trying to get things to work. :slight_smile: Yes, I had run the migration and finally got a form to take in input and then display it on another page. My struggle has been with many-to-many tables.

But I do have a running version (no tests) and I don’t want to lose all the code I wrote. So I wanted to make sure that things like mix ecto.rollback don’t write over my schema files. Will any of the “ecto” commands just change underlying tables?

So for Step 4, do I do mix Ecto.Reset then mix Ecto.Rollback and finally mix Ecto.Migrate?

The rollback option was in case You want to replace the reset. In case your schema is the last migration, You could rollback, update migration & schema, and migrate again. Without the need to reset the whole database.

If You have used a generator, test files are automatically created, with the fields provided in the params. In that case, You should (but it’s not an obligation) update test files as well to reflect the new list of fields.

It is not the case if You created all the files manually.

Assuming the user you have given to Ecto has enough privileges you can just change your code as you specified – remove the field anywhere you can find it and then just run mix ecto.reset. Should be enough since that command will drop the DB, create it anew, run migrations (where you have removed the field) and run the seeds.

1 Like