Mix phx.gen: Add not-null modifier to Phoenix generators

Recently (last week) this PR was merged in Ruby on Rails repository and i thought it could be nice to have in phoenix, basically what it does is to add a “!” operator at the end of each type, so in summary we would type:

$ mix phx.gen.schema Blog.Post blog_posts title:string! views:integer!

and the current output would be generated:

defmodule MyApp.Repo.Migrations.CreateBlogPosts do
  use Ecto.Migration

  def change do
    create table(:blog_posts) do
      add :title, :string, null: false
      add :views, :integer, null: false

      timestamps(type: :utc_datetime)
    end
  end
end

If you think this make sense as i do and like the idea i think would be great for me to work in this feature since i’m always looking to ways to contribute with code for phoenix, but i would need some help and i’d be really greateful if you guys could help me do it.

IMPORTANT: There’s my first feature suggestion and i don’t know very well how things are done here, sorry if the post does not follow any standard or something like that.

8 Likes

It’s a reasonable feature. I occasionally add null: false after generating migrations.

Let’s hear what other people think.

If you have buy-in from the Phoenix team I can help you both suggesting pointers for where to look at in the code base as well a reviewing a PR.

3 Likes

I do like the idea and I believe I’ve seen it in other ecosystems as well. But there must always be kept a balance between over-loading a DSL and the usefulness that the new feature will bring. It’s not a clear win in favor of adding the bang variant.

3 Likes

Thanks for your reply and point of view, so if i got it you meant that’s not worthy to add bang variable in generators right? I can be wrong but In my opinion, this feature is classified as a small change that would help a lot in everyday life of phoenix devs, i already had to generate long schemas and was kind of boring to add null: false in almost every field. Anyway, thanks for your reply :wave:

Thanks bro, i think would be great to have this feature cause we’ll be able to generate almost everything in migration only using mix phx task.

I’d really love this help, would be very welcome mate :saluting_face:

(If your driving motivation is to contribute something, and you are interested in the code generators, this open issue might serve as a good entrypoint to the topic Calling phx.mix.gen without specifying attributes will succeed but create broken code · Issue #5870 · phoenixframework/phoenix · GitHub)

1 Like

Gonna take a look bro, i’m really looking forward code contribution in phoenix. Can i send you a DM in forum chat ?