Skip to content

Commit 09eee20

Browse files
fix: run database seeds on Docker startup to create default admin user
Seeds were never executed in Docker deployments — only migrations ran. This meant no default admin user existed, making login impossible. Closes #5
1 parent 0e615a6 commit 09eee20

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,6 @@ EXPOSE 4000
7575
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
7676
CMD curl -f http://localhost:4000/health || exit 1
7777

78-
CMD /app/bin/zentinel_cp eval "ZentinelCp.Release.migrate()" && /app/bin/zentinel_cp start
78+
CMD /app/bin/zentinel_cp eval "ZentinelCp.Release.migrate()" && \
79+
/app/bin/zentinel_cp eval "ZentinelCp.Release.seed()" && \
80+
/app/bin/zentinel_cp start

lib/zentinel_cp/release.ex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
defmodule ZentinelCp.Release do
22
@moduledoc """
3-
Release tasks for running migrations in production.
3+
Release tasks for running migrations and seeds in production.
44
55
Usage:
66
77
bin/zentinel_cp eval "ZentinelCp.Release.migrate()"
8+
bin/zentinel_cp eval "ZentinelCp.Release.seed()"
89
"""
910

1011
@app :zentinel_cp
@@ -17,6 +18,21 @@ defmodule ZentinelCp.Release do
1718
end
1819
end
1920

21+
def seed do
22+
load_app()
23+
24+
for repo <- repos() do
25+
{:ok, _, _} =
26+
Ecto.Migrator.with_repo(repo, fn _repo ->
27+
seed_file = Application.app_dir(@app, "priv/repo/seeds.exs")
28+
29+
if File.exists?(seed_file) do
30+
Code.eval_file(seed_file)
31+
end
32+
end)
33+
end
34+
end
35+
2036
def rollback(repo, version) do
2137
load_app()
2238
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))

0 commit comments

Comments
 (0)