Fix database creation on deployment
- Add create_database/0 function to handle database creation before migrations - Make DeviceCache handle missing tables gracefully on startup - Retry device loading after 5 seconds if initial load fails - Prevent deployment failures when database doesn't exist yet 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
f4aabf56c0
commit
cd69de5131
2 changed files with 43 additions and 8 deletions
|
|
@ -53,10 +53,15 @@ defmodule Aprsme.DeviceCache do
|
||||||
@impl true
|
@impl true
|
||||||
def init(_) do
|
def init(_) do
|
||||||
# Load devices on startup
|
# Load devices on startup
|
||||||
load_devices_into_cache()
|
case load_devices_into_cache() do
|
||||||
|
:ok ->
|
||||||
|
# Schedule periodic refresh
|
||||||
|
Process.send_after(self(), :refresh_cache, @refresh_interval)
|
||||||
|
|
||||||
# Schedule periodic refresh
|
:error ->
|
||||||
Process.send_after(self(), :refresh_cache, @refresh_interval)
|
# If initial load failed, retry sooner
|
||||||
|
Process.send_after(self(), :refresh_cache, 5_000)
|
||||||
|
end
|
||||||
|
|
||||||
{:ok, %{}}
|
{:ok, %{}}
|
||||||
end
|
end
|
||||||
|
|
@ -86,12 +91,23 @@ defmodule Aprsme.DeviceCache do
|
||||||
# Private functions
|
# Private functions
|
||||||
|
|
||||||
defp load_devices_into_cache do
|
defp load_devices_into_cache do
|
||||||
devices = Repo.all(Devices)
|
require Logger
|
||||||
|
|
||||||
# Store all devices in cache
|
try do
|
||||||
case Cachex.put(@cache_name, :all_devices, devices) do
|
devices = Repo.all(Devices)
|
||||||
{:ok, true} -> :ok
|
|
||||||
error -> error
|
# Store all devices in cache
|
||||||
|
case Cachex.put(@cache_name, :all_devices, devices) do
|
||||||
|
{:ok, true} -> :ok
|
||||||
|
error -> error
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
error in [Postgrex.Error, DBConnection.ConnectionError] ->
|
||||||
|
# Handle case where database or table doesn't exist yet
|
||||||
|
Logger.warning("Failed to load devices: #{inspect(error)}. Will retry later.")
|
||||||
|
# Store empty list for now
|
||||||
|
Cachex.put(@cache_name, :all_devices, [])
|
||||||
|
:error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,10 +13,29 @@ defmodule Aprsme.Release do
|
||||||
|
|
||||||
# Gettext translations are automatically compiled during Mix compilation
|
# Gettext translations are automatically compiled during Mix compilation
|
||||||
|
|
||||||
|
# Create database if it doesn't exist
|
||||||
|
create_database()
|
||||||
|
|
||||||
# Run migrations
|
# Run migrations
|
||||||
{:ok, _, _} = Ecto.Migrator.with_repo(Aprsme.Repo, &Ecto.Migrator.run(&1, :up, all: true))
|
{:ok, _, _} = Ecto.Migrator.with_repo(Aprsme.Repo, &Ecto.Migrator.run(&1, :up, all: true))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp create_database do
|
||||||
|
require Logger
|
||||||
|
|
||||||
|
case Aprsme.Repo.__adapter__().storage_up(Aprsme.Repo.config()) do
|
||||||
|
:ok ->
|
||||||
|
Logger.info("Database created successfully")
|
||||||
|
|
||||||
|
{:error, :already_up} ->
|
||||||
|
Logger.info("Database already exists")
|
||||||
|
|
||||||
|
{:error, error} ->
|
||||||
|
Logger.error("Failed to create database: #{inspect(error)}")
|
||||||
|
raise "Database creation failed: #{inspect(error)}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def rollback(repo, version) do
|
def rollback(repo, version) do
|
||||||
load_app()
|
load_app()
|
||||||
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
|
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue