lucky_migrator icon indicating copy to clipboard operation
lucky_migrator copied to clipboard

Allow running something in a block when adding a required field to an existing table

Open paulcsmith opened this issue 7 years ago • 3 comments

This issue (https://github.com/luckyframework/lucky_migrator/issues/12) is very helpful, but sometimes filling the table with a static value is not ideal.

For example, you may want to add a slug to blog posts, in which case you'd want to generate the slug for each post and then save it.

We should make another version of adding a required field that accepts a block:

alter :posts do
  add slug : String do 
    # Will set all the slugs
    generate_slugs_for_posts
  end
  # The field will be marked as required (NOT NULL) once the block has finished
end

paulcsmith avatar Dec 20 '17 20:12 paulcsmith

@mikeeus Would you be interested in trying this one?

paulcsmith avatar Apr 22 '18 03:04 paulcsmith

This looks like an interesting feature to build. Can you give me any pointers on how to start?

Can the user run anything in the block? For example do we just add yield to the add macro and execute anything?

mikeeus avatar Jul 16 '18 17:07 mikeeus

@mikeeus I'd love to give some pointers. I think the block should only be accepted when adding a required (non ? column) when altering a table. For everything else, it should not be added.

The block could be anything the user wants. Does that help? Let me know if you need any more info!

paulcsmith avatar Jul 18 '18 20:07 paulcsmith