Skip to content

Commit 94b8ca5

Browse files
fix: add release module and auto-migrate on Docker startup
Add SentinelCp.Release with migrate/rollback for running Ecto migrations in production releases. Update Dockerfile CMD to run migrations before starting the server so docker compose up works out of the box.
1 parent 55c3b0b commit 94b8ca5

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,4 @@ 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/sentinel_cp", "start"]
78+
CMD /app/bin/sentinel_cp eval "SentinelCp.Release.migrate()" && /app/bin/sentinel_cp start

lib/sentinel_cp/release.ex

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule SentinelCp.Release do
2+
@moduledoc """
3+
Release tasks for running migrations in production.
4+
5+
Usage:
6+
7+
bin/sentinel_cp eval "SentinelCp.Release.migrate()"
8+
"""
9+
10+
@app :sentinel_cp
11+
12+
def migrate do
13+
load_app()
14+
15+
for repo <- repos() do
16+
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
17+
end
18+
end
19+
20+
def rollback(repo, version) do
21+
load_app()
22+
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
23+
end
24+
25+
defp repos do
26+
Application.fetch_env!(@app, :ecto_repos)
27+
end
28+
29+
defp load_app do
30+
Application.load(@app)
31+
end
32+
end

0 commit comments

Comments
 (0)