Can I run mix phx.gen.live after already running mix phx.gen.context?

When I started my project, I was following two courses that used the following to set up a new project:

mix phx.new project_name --live
mix phx.gen.context Accounts User users name:string age:integer
mix ecto.migrate

I’ve now added a lot of code to my context, schema and .leex files to get all of my relationships to work. I don’t want to lose this code! BUT … I’ve been reading the Programming Phoenix Liveview book by Tate & DeBenedetto and I’ve realized that I should have used mix phx.gen.live in order to get LiveView code and templates generated for me (eg index_live.ex, form_component.ex, modal_component.ex, etc).

Can I safely run the following to get the templates but NOT touch my context and schemas:
mix phx.gen.live --no-context --no-schema

OR … should I set up a second dummy directory and completely rerun my project using mix phx.gen.live and then copy over the template files that I didn’t get using phx.gen.context. My only concern about doing this is that I don’t know if phx.gen.live is inserting code in files that I might not see.

Are you using git or other source control? If so, just run it (after a commit) and use your tool of choice to inspect the differences. If you don’t like what you see, you can easily revert they changes.

If not, probably time to :wink:

Good idea! I am using git, although I’m learning Elixir, Ecto, Phoenix and git all at once. I’m on a bit of a steep learning curve. That said, I think I’ve figured out how I can diff the files using git and Visual Studio. Thanks for the idea!

UPDATE: I ran
mix phx.gen.live Resources Resource resource title:string description:string --context --no-schema

I’m abbreviating things, but the bottom line is that it did NOT write over any of the logic I had written in my context and schema with those names.

It modified one file - myproject_web.ex

  • added one line in view_helpers function:
    import MyProjectWeb.LiveHlpers

It then added the following files to my project:

modal_component.ex
form_component.ex
form_component.html.leex
index.ex
index.html.leex
show.ex
show.html.leex
myContext_live_test.ex

There was a naming conflict for the routes but that was easy to fix. Everything works as before. No lost code. Yay!

3 Likes