Adding Ecto to Live View App

I created an application with mix phx.new --no-ecto .... Now I realize that I do need a database. What is the best way to now add the default ecto functionality?

Make a new app with the same name and move the stuff over from the new one.
Ith should be changes to mix.exs, config-files and a lib/my_app/repo.ex
Also check the test files. Should be changes there as well

2 Likes

These are the files that don’t get added when the --no-ecto flag is specified so one approach is to generate a new app and copy those files over to your existing application.

Update: This approach would not be recommended for apps where other mix phx.gen.X tasks were subsequently run to generate resources since…

2 Likes

I have been here in the past.

First commit all the changes on your current project:

git commit ...

Next, has suggested by @andreaseriksson you need to create another app with the same name in a separated folder:

cd ...
mix phx.new ...

Now copy the new app over your current app:

cp -r new-app/* current-app

Now see the differences with git:

git diff

Next use git add -p to stage only the changes you are interested in.

2 Likes

A lot of other stuff will be different, like Context, Controllers and Tests.

1 Like

Here’s a good exercise to visualise all the differences in a linux terminal:

  1. Create 2 new projects, one with ecto and one without, in their own directories
  2. Make sure they both have the same APP name!
  3. Don’t fetch and install dependencies
  4. Run the linux diff command to show a side-by-side comparison
  5. See the exact differences between the generated projects
  6. Profit!
$ mkdir test_ecto_generation
$ cd test_ecto_generation

$ mix phx.new --no-ecto --app ecto_diff no_ecto
...
Fetch and install dependencies? [Yn] n
...

$ mix phx.new --app ecto_diff with_ecto
...
Fetch and install dependencies? [Yn] n
...

$ ls
no_ecto  with_ecto

$ diff -dry --suppress-common-lines --color=always with_ecto/ no_ecto/

diff -dry --suppress-common-lines '--color=always' with_ecto/.formatter.exs no_ecto/.formatter.exs
  import_deps: [:ecto, :ecto_sql, :phoenix],                  |   import_deps: [:phoenix],
  subdirectories: ["priv/*/migrations"],                      <
  inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,e |   inputs: ["*.{heex,ex,exs}", "{config,lib,test}/**/*.{heex,e
diff -dry --suppress-common-lines '--color=always' with_ecto/config/config.exs no_ecto/config/config.exs
config :ecto_diff,                                            <
  ecto_repos: [EctoDiff.Repo]                                 <
                                                              <
  live_view: [signing_salt: "iprzzO3C"]                       |   live_view: [signing_salt: "0T8jW/Us"]
diff -dry --suppress-common-lines '--color=always' with_ecto/config/dev.exs no_ecto/config/dev.exs
# Configure your database                                     <
config :ecto_diff, EctoDiff.Repo,                             <
  username: "postgres",                                       <
  password: "postgres",                                       <
  hostname: "localhost",                                      <
  database: "ecto_diff_dev",                                  <
  stacktrace: true,                                           <
  show_sensitive_data_on_connection_error: true,              <
  pool_size: 10                                               <

[PLUS MANY MORE DIFFERENCES]

To clarify, the command diff -dry --suppress-common-lines --color=always with_ecto/ no_ecto/ will do a minimal d, recursive r, side-by-side y diff of all the files and subdirectories between with_ecto/ and no_ecto with a colour output, only showing the relevant changes. It’s not the prettiest output, and it also re-prints the original command for each file name without any nice new lines between to divide things visually. But it does the trick.

Maybe also add -W 200 to widen the output to 200 columns otherwise the diff cuts off some of the lines.

1 Like

You can give this a try: Elixir Diff

1 Like

Thanks for the help, everyone!