Skip to content

Commit ac63d1f

Browse files
authored
Merge pull request #19 from unicef/fix/Align-db-models
Align db models
2 parents c49ad29 + 781ca8d commit ac63d1f

4 files changed

Lines changed: 406 additions & 330 deletions

File tree

docs/src/commands.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@ This command runs migrations, creates extra permissions, and removes stale conte
3030
- `--no-static`: Do not run collectstatic.
3131
- `--admin-email`: Admin email.
3232
- `--admin-password`: Admin password.
33+
34+
## db_align
35+
36+
This command aligns unmanaged HOPE models with the current HOPE database schema by regenerating `modules/hope/models/_inspect.py`.
37+
38+
### Arguments
39+
40+
- `table`: Optional list of tables to introspect. If omitted, all configured HOPE tables are introspected.
41+
- `--database`: Database alias to introspect. Defaults to `hope_ro`.
42+
- `--schema`: Schema to introspect. Defaults to `public`.
43+
- `--output-file`: Destination filename under `modules/hope/models/`. Defaults to `_inspect.py`.

src/hope_portal/api/urls.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
from django.urls import include, path
2-
31
from .router import Router
42

53
app_name = "api"
64

75
router = Router()
86

97

10-
urlpatterns = [
11-
path("", include(router.urls)),
12-
]
8+
urlpatterns = router.urls
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from argparse import ArgumentParser
2+
from typing import Any
3+
4+
from django.core.management import BaseCommand, call_command
5+
6+
7+
class Command(BaseCommand):
8+
help = "Align HOPE unmanaged models with the current HOPE database schema."
9+
requires_system_checks = []
10+
requires_migrations_checks = False
11+
12+
def add_arguments(self, parser: ArgumentParser) -> None:
13+
parser.add_argument(
14+
"table",
15+
action="store",
16+
nargs="*",
17+
type=str,
18+
help="Optional list of tables to introspect. Introspects all configured HOPE tables when omitted.",
19+
)
20+
parser.add_argument(
21+
"--database",
22+
action="store",
23+
dest="database",
24+
default="hope_ro",
25+
help='Database alias to introspect. Defaults to "hope_ro".',
26+
)
27+
parser.add_argument(
28+
"--schema",
29+
action="store",
30+
dest="schema",
31+
default="public",
32+
help='Database schema to introspect. Defaults to "public".',
33+
)
34+
parser.add_argument(
35+
"--output-file",
36+
action="store",
37+
dest="output_file",
38+
default="_inspect.py",
39+
type=str,
40+
help='Destination filename under "modules/hope/models/". Defaults to "_inspect.py".',
41+
)
42+
43+
def handle(self, *args: Any, **options: Any) -> None:
44+
table: list[str] = options["table"]
45+
database = options["database"]
46+
schema = options["schema"]
47+
output_file = options["output_file"]
48+
49+
self.stdout.write(
50+
f"Aligning HOPE schema from database='{database}', schema='{schema}' into '{output_file}'",
51+
self.style.WARNING,
52+
)
53+
54+
call_command(
55+
"inspect_hope",
56+
*table,
57+
database=database,
58+
schema=schema,
59+
output_file=output_file,
60+
)
61+
62+
self.stdout.write(
63+
"Schema alignment complete. Restart application workers to load updated models.",
64+
self.style.SUCCESS,
65+
)

0 commit comments

Comments
 (0)