...

/

Application Integration with Database

Application Integration with Database

Learn how to modify your application to work with the database.

We'll cover the following...

Now that we have a database, we need to set up our application to connect to it.

Note: You are encouraged to follow along and update your testing_ecto application if you have a local setup. Otherwise, we have updated all the files for you in the code widget at the end of the lesson.

Updating our application

The first thing is that we need to add two new dependencies into the deps section of your mix.exs file:

Press + to interact
#file path -> testing_ecto/mix.exs
{:ecto_sql, "~> 3.4"},
{:postgrex, "~> 0.15.0"}

Ecto SQL provides your application with the additional code needed to connect to a database. Postgres provides the adapter that Ecto SQL needs to work specifically with a Postgres database.

Once added, Run mix deps.get at the root directory of our testing_ecto application to pull down the new libraries.

In the testing_ecto codebase, We’ve included a couple of very basic configuration files (testing_ecto/config/config.exs and testing_ecto/config/test.exs) and a very basic repo file (testing_ecto/lib/repo.ex).

Press + to interact
test.exs
dev.exs
config.exs
#file path -> testing_ecto/config/test.exs
use Mix.Config
config :testing_ecto, TestingEcto.Repo,
username: "postgres",
password: "postgres",
database: "testing_ecto_test",
pool: Ecto.Adapters.SQL.Sandbox
config :logger, :console, level: :warn

These are hard-coded with values that will work with the instance of Postgres created by the docker-compose file we provided. The only thing that might not be standard boilerplate is that we’ve added the option migration_timestamps: [type: :utc_datetime_usec] to our database configs. This will make ...