From 945d0db1f625b5896b471e1888ea42bc61dbec41 Mon Sep 17 00:00:00 2001
From: Thomas Schneider <thomas@fsmpi.rwth-aachen.de>
Date: Tue, 17 Sep 2024 17:07:53 +0200
Subject: [PATCH] Move alembic.ini inside Python package

Before this, it was not shipped with the sdist or wheel and thus migrations were
unavailable for package-based installs.
---
 pyproject.toml                                     | 1 +
 schilder2000/cli.py                                | 4 ++++
 alembic.ini => schilder2000/migrations/alembic.ini | 2 +-
 3 files changed, 6 insertions(+), 1 deletion(-)
 rename alembic.ini => schilder2000/migrations/alembic.ini (94%)

diff --git a/pyproject.toml b/pyproject.toml
index 1f8f77e..fa3ab38 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -57,6 +57,7 @@ migrate = "flask -A schilder2000 alembic upgrade head"
 includes = [
     "schilder2000/**/*.py",
     "schilder2000/templates",
+    "schilder2000/migrations",
     # Referenced files in manifest.json included via pdm_build.py
 ]
 
diff --git a/schilder2000/cli.py b/schilder2000/cli.py
index 4619945..1d8b98c 100644
--- a/schilder2000/cli.py
+++ b/schilder2000/cli.py
@@ -1,5 +1,7 @@
 import sys
+import os
 from os.path import basename
+from pathlib import Path
 
 import click
 from flask.cli import with_appcontext
@@ -13,6 +15,8 @@ from alembic import config
 @with_appcontext
 def alembic(argv):
     """Wrap the alembic CLI tool."""
+    configfile = Path(__file__).parent / "migrations" / "alembic.ini"
+    os.environ.setdefault("ALEMBIC_CONFIG", str(configfile))
     prog = " ".join([basename(sys.argv[0]), sys.argv[1]])
     cli = config.CommandLine(prog=prog)
     cli.main(argv=argv)
diff --git a/alembic.ini b/schilder2000/migrations/alembic.ini
similarity index 94%
rename from alembic.ini
rename to schilder2000/migrations/alembic.ini
index 358051b..a12d1f3 100644
--- a/alembic.ini
+++ b/schilder2000/migrations/alembic.ini
@@ -1,5 +1,5 @@
 [alembic]
-script_location = schilder2000/migrations
+script_location = %(here)s
 
 [post_write_hooks]
 hooks = ruff ruff_format
-- 
GitLab