Skip to content

Commit 3c21e7b

Browse files
Taureclaude
andauthored
fix: correct bugs and gaps in database setup chapter (#10)
* docs: expand views, auth & sessions chapter with template basics, CSRF, and simplified login flow - Add ErlyDTL syntax reference table, base layout with template inheritance - Add CSRF token to login form (required by nova_csrf_plugin) - Document all template options (view, headers, status_code) - Move credential validation from security function into controller - Simplify session_auth to return {redirect, "/login"} instead of bare false - Remove manual session ID generation — use Nova's auto-created sessions - Consolidate routes from three groups to two (public + protected) - Add use_sessions config, session API return types, and delete/1 cookie behavior Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: correct bugs and gaps in database setup chapter Cross-referenced against Kura and rebar3_kura source to fix: - config/0 now matches generator output (application:get_env for database, <<"postgres">> password, ?MODULE pool) - Clarify kura_repo behaviour only requires config/0 callback - Rewrite "Configuring the repo" to explain env-specific overrides - Add kura_migrator:migrate/1 call in supervisor startup - Fix provider hook from {post,...} to {pre,...} - Fix query example to show actual return format ({ok, [#{...}]}) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d64a39d commit 3c21e7b

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/data-layer/setup.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ This creates `src/blog_repo.erl` — a module that wraps all database operations
5050
preload/3, transaction/1, multi/1, query/2]).
5151

5252
config() ->
53-
#{pool => blog_repo,
54-
database => <<"blog_dev">>,
53+
Database = application:get_env(blog, database, <<"blog_dev">>),
54+
#{pool => ?MODULE,
55+
database => Database,
5556
hostname => <<"localhost">>,
5657
port => 5432,
5758
username => <<"postgres">>,
58-
password => <<>>,
59+
password => <<"postgres">>,
5960
pool_size => 10}.
6061

6162
start() -> kura_repo_worker:start(?MODULE).
@@ -76,7 +77,7 @@ multi(Multi) -> kura_repo_worker:multi(?MODULE, Multi).
7677
query(SQL, Params) -> kura_repo_worker:query(?MODULE, SQL, Params).
7778
```
7879

79-
Every function delegates to `kura_repo_worker` with the repo module as the first argument. The `config/0` callback tells Kura how to connect to PostgreSQL.
80+
The `kura_repo` behaviour only requires one callback — `config/0` — which tells Kura how to connect to PostgreSQL. Every other function is a convenience delegation to `kura_repo_worker`.
8081

8182
The setup command also creates `src/migrations/` for migration files.
8283

@@ -109,7 +110,9 @@ docker compose up -d
109110

110111
## Configuring the repo
111112

112-
Update `config/dev_sys.config.src` to include the repo config. The repo module reads its config from `config/0`, but you can also configure it through `sys.config` if you prefer environment variable substitution:
113+
Notice that `config/0` uses `application:get_env(blog, database, <<"blog_dev">>)` for the database name. This means you can override it per environment through `sys.config` without touching the module.
114+
115+
For example, to use a separate test database, add a `blog` section to your test sys.config:
113116

114117
```erlang
115118
[
@@ -138,10 +141,15 @@ Update `config/dev_sys.config.src` to include the repo config. The repo module r
138141
decode_json_body => true
139142
}}
140143
]}
144+
]},
145+
{blog, [
146+
{database, <<"blog_test">>}
141147
]}
142148
].
143149
```
144150

151+
The `blog_dev` default in `config/0` works for development without any sys.config entry. Override just the keys you need per environment.
152+
145153
## Starting the repo in the supervisor
146154

147155
The repo needs to be started when your application boots. Add it to your supervisor in `src/blog_sup.erl`:
@@ -158,22 +166,27 @@ start_link() ->
158166

159167
init([]) ->
160168
blog_repo:start(),
169+
kura_migrator:migrate(blog_repo),
161170
{ok, {#{strategy => one_for_one, intensity => 5, period => 10}, []}}.
162171
```
163172

164-
`blog_repo:start()` creates the pgo connection pool using the config from `config/0`.
173+
`blog_repo:start()` creates the pgo connection pool using the config from `config/0`. `kura_migrator:migrate/1` then runs any pending migrations — it tracks which versions have been applied in a `schema_migrations` table.
174+
175+
```admonish tip
176+
Auto-migrating on startup is convenient during development. For production, run migrations as a separate step before deploying (e.g. a release command or CI job) so that failures don't prevent the application from starting.
177+
```
165178

166179
## Adding the rebar3_kura compile hook
167180

168181
To get automatic migration generation (covered in the next chapter), add a provider hook to `rebar.config`:
169182

170183
```erlang
171184
{provider_hooks, [
172-
{post, [{compile, {kura, compile}}]}
185+
{pre, [{compile, {kura, compile}}]}
173186
]}.
174187
```
175188

176-
This runs `rebar3 kura compile` after every `rebar3 compile`, scanning your schemas and generating migrations for any changes.
189+
This runs `rebar3 kura compile` before every `rebar3 compile`, scanning your schemas and generating migrations for any changes.
177190

178191
## Verifying the connection
179192

@@ -186,11 +199,11 @@ rebar3 nova serve
186199
You should see the application start without errors. If the database is unreachable, you will see a connection error in the logs. Verify from the shell:
187200

188201
```erlang
189-
1> blog_repo:query("SELECT 1", []).
190-
{ok, #{command => select, num_rows => 1, rows => [{1}]}}
202+
1> blog_repo:query("SELECT 1 AS result", []).
203+
{ok, [#{result => 1}]}
191204
```
192205

193-
Two commands and you have a database layer.
206+
`query/2` returns `{ok, Rows}` where each row is a map with atom keys — the same format you will see from all Kura query functions.
194207

195208
---
196209

0 commit comments

Comments
 (0)