simple-module-core 0.0.44__tar.gz → 0.0.45__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/PKG-INFO +1 -1
  2. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/pyproject.toml +1 -1
  3. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/__main__.py +10 -3
  4. simple_module_core-0.0.45/simple_module_core/_migration_doctor.py +56 -0
  5. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_expression_index.py +30 -5
  6. simple_module_core-0.0.45/simple_module_core/diagnostics/_migration.py +96 -0
  7. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/dotenv.py +23 -0
  8. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_diagnostics.py +19 -0
  9. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_expression_index_diagnostics.py +36 -0
  10. simple_module_core-0.0.44/simple_module_core/diagnostics/_migration.py +0 -45
  11. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/.gitignore +0 -0
  12. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/LICENSE +0 -0
  13. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/README.md +0 -0
  14. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/__init__.py +0 -0
  15. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/_data_doctor.py +0 -0
  16. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/_i18n_flatten.py +0 -0
  17. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/_i18n_translator.py +0 -0
  18. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/_permission_grants.py +0 -0
  19. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/_request_hooks.py +0 -0
  20. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/activity_feed.py +0 -0
  21. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/app_state.py +0 -0
  22. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/audit_links.py +0 -0
  23. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/body_limits.py +0 -0
  24. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/correlation.py +0 -0
  25. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/cors.py +0 -0
  26. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/csp.py +0 -0
  27. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/data_compatibility.py +0 -0
  28. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/datetimes.py +0 -0
  29. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/deprecation.py +0 -0
  30. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/design_packs.py +0 -0
  31. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/__init__.py +0 -0
  32. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_coupling.py +0 -0
  33. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_css.py +0 -0
  34. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_db_extensions.py +0 -0
  35. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_dependencies.py +0 -0
  36. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_durable_names.py +0 -0
  37. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_heavy_task_imports.py +0 -0
  38. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_i18n.py +0 -0
  39. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_inertia_api.py +0 -0
  40. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_js_workspace.py +0 -0
  41. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_module.py +0 -0
  42. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_pages.py +0 -0
  43. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_runner.py +0 -0
  44. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_spa_mount.py +0 -0
  45. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_tenancy.py +0 -0
  46. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/diagnostics/_types.py +0 -0
  47. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/discovery.py +0 -0
  48. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/durable_events.py +0 -0
  49. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/environments.py +0 -0
  50. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/events.py +0 -0
  51. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/exceptions.py +0 -0
  52. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/feature_flags.py +0 -0
  53. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/health.py +0 -0
  54. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/i18n.py +0 -0
  55. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/i18n_overrides.py +0 -0
  56. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/invalidation.py +0 -0
  57. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/invalidation_refresh.py +0 -0
  58. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/leases.py +0 -0
  59. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/mail.py +0 -0
  60. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/menu.py +0 -0
  61. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/module.py +0 -0
  62. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/module_meta.py +0 -0
  63. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/module_settings.py +0 -0
  64. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/path_rules.py +0 -0
  65. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/permission_denied.py +0 -0
  66. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/permissions.py +0 -0
  67. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/public_routes.py +0 -0
  68. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/py.typed +0 -0
  69. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/rate_limit.py +0 -0
  70. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/redirect_safety.py +0 -0
  71. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/resources.py +0 -0
  72. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/secret_fields.py +0 -0
  73. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/services.py +0 -0
  74. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/settings_base.py +0 -0
  75. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/settings_hydrate.py +0 -0
  76. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/setup_steps.py +0 -0
  77. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/singleflight.py +0 -0
  78. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/spa_mount.py +0 -0
  79. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/stored_settings.py +0 -0
  80. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/tenancy.py +0 -0
  81. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/tenant_events.py +0 -0
  82. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/user_directory.py +0 -0
  83. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/user_events.py +0 -0
  84. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/simple_module_core/versioning.py +0 -0
  85. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_audit_links.py +0 -0
  86. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_auth_provider_selection.py +0 -0
  87. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_app_state.py +0 -0
  88. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_data_compatibility_doctor.py +0 -0
  89. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_datetimes.py +0 -0
  90. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_inline_delivery_context.py +0 -0
  91. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_invalidation_refresh.py +0 -0
  92. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_leases.py +0 -0
  93. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_lifecycle_docs.py +0 -0
  94. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_mail_resolution.py +0 -0
  95. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_module_settings.py +0 -0
  96. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_settings_source_cache.py +0 -0
  97. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_singleflight.py +0 -0
  98. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_core_user_directory.py +0 -0
  99. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_csp_registry.py +0 -0
  100. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_css_diagnostics.py +0 -0
  101. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_css_scanner.py +0 -0
  102. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_db_extensions_diagnostics.py +0 -0
  103. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_deprecation_helper.py +0 -0
  104. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_design_packs.py +0 -0
  105. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_diagnostics_tenancy.py +0 -0
  106. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_discovery.py +0 -0
  107. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_discovery_missing_deps.py +0 -0
  108. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_doctor_cli_overrides.py +0 -0
  109. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_dotenv.py +0 -0
  110. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_durable_events.py +0 -0
  111. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_environments.py +0 -0
  112. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_events.py +0 -0
  113. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_events_inline.py +0 -0
  114. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_feature_flags.py +0 -0
  115. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_feature_flags_decorator.py +0 -0
  116. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_health_registry.py +0 -0
  117. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_heavy_task_imports_diagnostics.py +0 -0
  118. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_i18n.py +0 -0
  119. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_i18n_diagnostics.py +0 -0
  120. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_i18n_overrides.py +0 -0
  121. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_invalidation.py +0 -0
  122. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_invalidation_handlers.py +0 -0
  123. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_menu.py +0 -0
  124. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_module_base.py +0 -0
  125. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_module_diagnostics.py +0 -0
  126. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_path_rule_registries.py +0 -0
  127. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_permission_sources.py +0 -0
  128. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_permissions.py +0 -0
  129. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_permissions_admin_principal.py +0 -0
  130. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_public_routes.py +0 -0
  131. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_public_routes_rate_precedence.py +0 -0
  132. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_redirect_safety.py +0 -0
  133. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_services.py +0 -0
  134. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_settings_base.py +0 -0
  135. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_sm003_page_render.py +0 -0
  136. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_sm009_dynamic_coupling.py +0 -0
  137. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_sm034_durable_names.py +0 -0
  138. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_tenant_exists.py +0 -0
  139. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_tenant_roles.py +0 -0
  140. {simple_module_core-0.0.44 → simple_module_core-0.0.45}/tests/test_versioning.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: simple_module_core
3
- Version: 0.0.44
3
+ Version: 0.0.45
4
4
  Summary: Module-system primitives for the simple_module framework — ModuleBase, discovery, diagnostics, events
5
5
  Project-URL: Homepage, https://github.com/antosubash/simple_module_python
6
6
  Project-URL: Repository, https://github.com/antosubash/simple_module_python
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "simple_module_core"
3
- version = "0.0.44"
3
+ version = "0.0.45"
4
4
  description = "Module-system primitives for the simple_module framework — ModuleBase, discovery, diagnostics, events"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -25,6 +25,7 @@ import sys
25
25
  from pathlib import Path
26
26
 
27
27
  from simple_module_core._data_doctor import data_compatibility_diagnostics
28
+ from simple_module_core._migration_doctor import migration_diagnostics
28
29
  from simple_module_core.diagnostics import (
29
30
  Diagnostic,
30
31
  DiagnosticLevel,
@@ -39,7 +40,7 @@ from simple_module_core.discovery import (
39
40
  select_auth_provider,
40
41
  topological_sort,
41
42
  )
42
- from simple_module_core.dotenv import find_env_file, parse_dotenv
43
+ from simple_module_core.dotenv import absolutize_sqlite_url, find_env_file, parse_dotenv
43
44
  from simple_module_core.exceptions import InvalidModuleError
44
45
 
45
46
 
@@ -77,8 +78,13 @@ def _load_i18n_settings_from_env() -> tuple[list[str] | None, str]:
77
78
 
78
79
 
79
80
  def _database_url() -> str:
80
- """``SM_DATABASE_URL``, defaulting like ``Settings.database_url``."""
81
- return os.environ.get("SM_DATABASE_URL", "sqlite+aiosqlite:///./app.db")
81
+ """``SM_DATABASE_URL``, defaulting and anchored like ``Settings.database_url``.
82
+
83
+ A relative SQLite path is resolved against the project root, as the app
84
+ resolves it, so doctor run from ``host/`` reads the app's database.
85
+ """
86
+ url = os.environ.get("SM_DATABASE_URL", "sqlite+aiosqlite:///./app.db")
87
+ return absolutize_sqlite_url(url, anchor=find_env_file().parent)
82
88
 
83
89
 
84
90
  def _database_dialect() -> str:
@@ -188,6 +194,7 @@ def main() -> int:
188
194
  database_dialect=_database_dialect(),
189
195
  )
190
196
  diagnostics.extend(override_findings)
197
+ diagnostics.extend(migration_diagnostics(modules, _database_url(), find_env_file().parent))
191
198
  diagnostics.extend(data_compatibility_diagnostics(modules, _database_url()))
192
199
  # A missing dependency only warns at a dev boot, but production refuses
193
200
  # to start with it (GH #501), so doctor reports it as what it will be.
@@ -0,0 +1,56 @@
1
+ """Doctor's migration checks (SM010, SM011) against ``SM_DATABASE_URL`` (#895)."""
2
+
3
+ import asyncio
4
+ import importlib.util
5
+ from collections.abc import Sequence
6
+ from pathlib import Path
7
+
8
+ from simple_module_core.diagnostics import Diagnostic, DiagnosticLevel
9
+ from simple_module_core.diagnostics._migration import MigrationDiagnostics, module_table_names
10
+ from simple_module_core.module import ModuleBase
11
+
12
+
13
+ def _alembic_ini(project_root: Path) -> Path | None:
14
+ """``alembic.ini`` at the project root, else ``host/alembic.ini``, as the CLI resolves it."""
15
+ for candidate in (project_root / "alembic.ini", project_root / "host" / "alembic.ini"):
16
+ if candidate.is_file():
17
+ return candidate
18
+ return None
19
+
20
+
21
+ def migration_diagnostics(
22
+ modules: Sequence[ModuleBase], database_url: str, project_root: Path
23
+ ) -> list[Diagnostic]:
24
+ # Core-only installations have neither Alembic nor a database runtime.
25
+ if importlib.util.find_spec("simple_module_db") is None:
26
+ return []
27
+ ini = _alembic_ini(project_root)
28
+ if ini is None:
29
+ return []
30
+ from simple_module_db.migration_state import read_migration_state
31
+
32
+ try:
33
+ state = asyncio.run(read_migration_state(database_url, ini))
34
+ except Exception:
35
+ # Unreachable is a warning, as for SM033: doctor also runs where no
36
+ # database is up. A revision file that fails to import lands here too.
37
+ # The error text can carry credentials, so it is not shown.
38
+ return [
39
+ Diagnostic(
40
+ level=DiagnosticLevel.WARNING,
41
+ code="SM010",
42
+ message="Could not read the migration state (database or Alembic revisions); "
43
+ "SM010 and SM011 were not checked",
44
+ module_name="migrations",
45
+ suggestion="Check SM_DATABASE_URL and that the database is running, and that "
46
+ "the revisions load (`make migration-history`).",
47
+ )
48
+ ]
49
+ if state is None:
50
+ return []
51
+ checks = MigrationDiagnostics()
52
+ behind = checks.check_heads(state.current, state.heads)
53
+ if behind:
54
+ # Behind head, a missing table may simply be in a pending revision.
55
+ return behind
56
+ return checks.check_table_coverage(module_table_names(modules), set(state.tables))
@@ -25,13 +25,37 @@ if TYPE_CHECKING:
25
25
  logger = logging.getLogger(__name__)
26
26
 
27
27
 
28
- def index_is_expression_based(index: Any) -> bool:
29
- """True when any element of ``index`` is an expression rather than a plain column.
28
+ _ORDERING_MODIFIERS = frozenset({"asc_op", "desc_op", "nulls_first_op", "nulls_last_op"})
29
+
30
+
31
+ def unwrap_ordering(expr: Any) -> Any:
32
+ """``expr`` without its ``desc()``/``asc()``/``nulls_first()``/``nulls_last()`` wrappers."""
33
+ while getattr(getattr(expr, "modifier", None), "__name__", None) in _ORDERING_MODIFIERS:
34
+ expr = expr.element
35
+ return expr
36
+
30
37
 
31
- A plain ``Column`` is attached to its table; a ``text()`` or function
32
- element is not.
38
+ def _is_column(expr: Any) -> bool:
39
+ """A column, possibly wrapped in ``desc()``/``asc()``/``nulls_last()``.
40
+
41
+ SQLite reflects ``created_at DESC`` as the column ``created_at`` and Alembic
42
+ compares it as one (GH #869), so an ordering keeps an element a column. A
43
+ plain ``Column`` is attached to its table; ``sa.column("x")`` is a named,
44
+ non-literal column; ``text()``, ``literal_column()``, a function or a bare
45
+ string label are expressions.
33
46
  """
34
- return any(getattr(expr, "table", None) is None for expr in index.expressions)
47
+ expr = unwrap_ordering(expr)
48
+ if hasattr(expr, "element"):
49
+ # Still wraps something once the ordering is gone: a label over an
50
+ # arbitrary expression (``lower(email).label("x")``) or ``sa.desc("col")``'s
51
+ # bare-string label. A column wraps nothing.
52
+ return False
53
+ return getattr(expr, "table", None) is not None or getattr(expr, "is_literal", None) is False
54
+
55
+
56
+ def index_is_expression_based(index: Any) -> bool:
57
+ """True when any element of ``index`` is an expression rather than a plain column."""
58
+ return not all(_is_column(expr) for expr in index.expressions)
35
59
 
36
60
 
37
61
  def find_expression_indexes(tables: Iterable[Any]) -> list[tuple[str, str]]:
@@ -103,4 +127,5 @@ __all__ = [
103
127
  "find_expression_indexes",
104
128
  "index_is_expression_based",
105
129
  "module_all_tables",
130
+ "unwrap_ordering",
106
131
  ]
@@ -0,0 +1,96 @@
1
+ """Alembic migration-state diagnostics (SM010, SM011)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Iterable, Set
6
+ from typing import TYPE_CHECKING
7
+
8
+ from simple_module_core.diagnostics._expression_index import module_all_tables
9
+ from simple_module_core.diagnostics._types import Diagnostic, DiagnosticLevel
10
+
11
+ if TYPE_CHECKING:
12
+ from simple_module_core.module import ModuleBase
13
+
14
+ _CODE_BEHIND = "SM010"
15
+ _MODULE_NAME = "migrations"
16
+ _SUGGEST_MIGRATE = "Run: make migrate"
17
+
18
+
19
+ def module_table_names(modules: Iterable[ModuleBase]) -> set[str]:
20
+ """Names of the tables declared in each module's ``models`` (the set SM026 checks)."""
21
+ return {table.name for mod in modules for table in module_all_tables(mod)}
22
+
23
+
24
+ class MigrationDiagnostics:
25
+ """Validates database migration state."""
26
+
27
+ def check_heads(self, current: Set[str], heads: Set[str]) -> list[Diagnostic]:
28
+ """SM010 for a database missing any head; the boot check's ``heads - current`` rule.
29
+
30
+ A never-migrated database is a WARNING: a production boot sends it to
31
+ ``/setup`` rather than refusing it. One behind on any branch is an ERROR,
32
+ because a production boot refuses it.
33
+ """
34
+ missing = heads - current
35
+ if not missing:
36
+ return []
37
+ if not current:
38
+ return [
39
+ Diagnostic(
40
+ level=DiagnosticLevel.WARNING,
41
+ code=_CODE_BEHIND,
42
+ message="Database has never been migrated (no alembic_version)",
43
+ module_name=_MODULE_NAME,
44
+ suggestion=_SUGGEST_MIGRATE,
45
+ )
46
+ ]
47
+ return [
48
+ Diagnostic(
49
+ level=DiagnosticLevel.ERROR,
50
+ code=_CODE_BEHIND,
51
+ message=(
52
+ f"Database is behind: missing heads {', '.join(map(repr, sorted(missing)))}"
53
+ ),
54
+ module_name=_MODULE_NAME,
55
+ suggestion=_SUGGEST_MIGRATE,
56
+ )
57
+ ]
58
+
59
+ def check_revision_mismatch(
60
+ self,
61
+ current_revision: str | None,
62
+ head_revision: str | None,
63
+ ) -> list[Diagnostic]:
64
+ """SM010: Error if database is not at the migration head."""
65
+ if current_revision == head_revision:
66
+ return []
67
+ return [
68
+ Diagnostic(
69
+ level=DiagnosticLevel.ERROR,
70
+ code=_CODE_BEHIND,
71
+ message=(f"Database at revision {current_revision!r}, expected {head_revision!r}"),
72
+ module_name=_MODULE_NAME,
73
+ suggestion=_SUGGEST_MIGRATE,
74
+ )
75
+ ]
76
+
77
+ def check_table_coverage(
78
+ self,
79
+ module_tables: set[str],
80
+ migrated_tables: set[str],
81
+ ) -> list[Diagnostic]:
82
+ """SM011: Warning for module tables a database at every head does not have."""
83
+ missing = module_tables - migrated_tables
84
+ return [
85
+ Diagnostic(
86
+ level=DiagnosticLevel.WARNING,
87
+ code="SM011",
88
+ message=(
89
+ f"Table '{table}' is declared in models but missing from the migrated "
90
+ "database, so no migration creates it"
91
+ ),
92
+ module_name=_MODULE_NAME,
93
+ suggestion=f'Run: make migration msg="add {table}"',
94
+ )
95
+ for table in sorted(missing)
96
+ ]
@@ -1,5 +1,8 @@
1
1
  """Minimal ``.env`` parser + env-var helpers — dependency-free.
2
2
 
3
+ Also holds ``absolutize_sqlite_url``, which anchors a relative SQLite
4
+ ``SM_DATABASE_URL`` at the project root for both the app and the doctor CLI.
5
+
3
6
  Used in places that can't or shouldn't pull in ``pydantic-settings`` (the
4
7
  diagnostics CLI runs before the host package is imported; the users-module
5
8
  bootstrap runs after settings are constructed and needs to read values that
@@ -139,3 +142,23 @@ def env_bool(name: str, default: bool = False) -> bool:
139
142
  if raw in BOOL_LITERALS_FALSE:
140
143
  return False
141
144
  return default
145
+
146
+
147
+ def absolutize_sqlite_url(url: str, *, anchor: Path) -> str:
148
+ """Rewrite a relative sqlite path to an absolute one under ``anchor``.
149
+
150
+ ``sqlite+aiosqlite:///./host/app.db`` means "relative to the project
151
+ root" by convention, but SQLAlchemy resolves it against the process cwd
152
+ — correct in the web process (which chdirs) and silently wrong in every
153
+ CLI run from a subdirectory. Absolute paths (``:////...``), ``:memory:``,
154
+ SQLite URI-mode paths (``file:...?uri=true``), and non-sqlite URLs pass
155
+ through untouched.
156
+ """
157
+ if not url.startswith("sqlite"):
158
+ return url
159
+ scheme, sep, rest = url.partition(":///")
160
+ if not sep or not rest or rest.startswith(("/", ":memory:", "file:")):
161
+ return url
162
+ path_part, query_sep, query = rest.partition("?")
163
+ resolved = (anchor / path_part).resolve()
164
+ return f"{scheme}:///{resolved}{query_sep}{query}"
@@ -52,6 +52,25 @@ class TestMigrationDiagnostics:
52
52
  )
53
53
  assert len(results) == 0
54
54
 
55
+ def test_check_heads_at_every_head(self):
56
+ assert MigrationDiagnostics().check_heads({"a", "b"}, {"a", "b"}) == []
57
+
58
+ def test_check_heads_never_migrated_warns(self):
59
+ (finding,) = MigrationDiagnostics().check_heads(set(), {"a", "b"})
60
+ assert finding.code == "SM010"
61
+ assert finding.level == DiagnosticLevel.WARNING
62
+ assert "make migrate" in finding.suggestion
63
+
64
+ def test_check_heads_behind_names_the_missing_heads(self):
65
+ (finding,) = MigrationDiagnostics().check_heads({"a", "old"}, {"a", "b", "c"})
66
+ assert finding.code == "SM010"
67
+ assert finding.level == DiagnosticLevel.ERROR
68
+ assert "missing heads 'b', 'c'" in finding.message
69
+
70
+ def test_check_heads_extra_revision_is_not_behind(self):
71
+ # Same rule as the boot check: only heads the database lacks count.
72
+ assert MigrationDiagnostics().check_heads({"a", "b", "extra"}, {"a", "b"}) == []
73
+
55
74
 
56
75
  class TestPrintDiagnostics:
57
76
  async def test_writes_to_stderr(self, capsys):
@@ -84,3 +84,39 @@ def test_module_without_models_is_skipped():
84
84
  meta = ModuleMeta(name="NoModels")
85
85
 
86
86
  assert module_all_tables(NoModels()) == []
87
+
88
+
89
+ def test_a_descending_column_is_a_column_not_an_expression():
90
+ """SQLite reflects ``created_at DESC`` as a column and Alembic compares it (GH #869)."""
91
+ md = sa.MetaData()
92
+ table = sa.Table(
93
+ "t",
94
+ md,
95
+ sa.Column("id", sa.Integer, primary_key=True),
96
+ sa.Column("created_at", sa.DateTime),
97
+ sa.Index("ix_t_named", "id", sa.desc(sa.column("created_at"))),
98
+ )
99
+ sa.Index("ix_t_bound", table.c.created_at.desc().nulls_last())
100
+ assert find_expression_indexes([table]) == []
101
+
102
+
103
+ def test_text_ordering_and_literal_columns_still_count_as_expressions():
104
+ md = sa.MetaData()
105
+ table = sa.Table(
106
+ "t",
107
+ md,
108
+ sa.Column("id", sa.Integer, primary_key=True),
109
+ sa.Column("created_at", sa.DateTime),
110
+ sa.Index("ix_t_text", "id", sa.text("created_at DESC")),
111
+ sa.Index("ix_t_label", "id", sa.desc("created_at")),
112
+ sa.Index("ix_t_literal", sa.desc(sa.literal_column("created_at"))),
113
+ )
114
+ sa.Index("ix_t_labelled_func", sa.func.lower(table.c.id).label("x"))
115
+ sa.Index("ix_t_cast", sa.cast(table.c.created_at, sa.String).desc())
116
+ assert find_expression_indexes([table]) == [
117
+ ("t", "ix_t_cast"),
118
+ ("t", "ix_t_label"),
119
+ ("t", "ix_t_labelled_func"),
120
+ ("t", "ix_t_literal"),
121
+ ("t", "ix_t_text"),
122
+ ]
@@ -1,45 +0,0 @@
1
- """Alembic migration-state diagnostics (SM010, SM011)."""
2
-
3
- from __future__ import annotations
4
-
5
- from simple_module_core.diagnostics._types import Diagnostic, DiagnosticLevel
6
-
7
-
8
- class MigrationDiagnostics:
9
- """Validates database migration state."""
10
-
11
- def check_revision_mismatch(
12
- self,
13
- current_revision: str | None,
14
- head_revision: str | None,
15
- ) -> list[Diagnostic]:
16
- """SM010: Error if database is not at the migration head."""
17
- if current_revision == head_revision:
18
- return []
19
- return [
20
- Diagnostic(
21
- level=DiagnosticLevel.ERROR,
22
- code="SM010",
23
- message=(f"Database at revision {current_revision!r}, expected {head_revision!r}"),
24
- module_name="migrations",
25
- suggestion="Run: make migrate",
26
- )
27
- ]
28
-
29
- def check_table_coverage(
30
- self,
31
- module_tables: set[str],
32
- migrated_tables: set[str],
33
- ) -> list[Diagnostic]:
34
- """SM011: Warning if module tables are missing from migration history."""
35
- missing = module_tables - migrated_tables
36
- return [
37
- Diagnostic(
38
- level=DiagnosticLevel.WARNING,
39
- code="SM011",
40
- message=f"Table '{table}' declared in models but not found in migration history",
41
- module_name="migrations",
42
- suggestion=f'Run: make migration msg="add {table}"',
43
- )
44
- for table in sorted(missing)
45
- ]