simple-module-db 0.0.45__tar.gz → 0.0.46__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 (139) hide show
  1. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/PKG-INFO +2 -2
  2. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/pyproject.toml +2 -2
  3. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_sqlite_index_compare.py +3 -2
  4. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/migration_portability.py +42 -15
  5. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/migrations.py +15 -5
  6. simple_module_db-0.0.46/tests/test_db_expression_index_warning_scope.py +95 -0
  7. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_migration_portability.py +6 -3
  8. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/.gitignore +0 -0
  9. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/LICENSE +0 -0
  10. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/README.md +0 -0
  11. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/__init__.py +0 -0
  12. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_bulk_audit_records.py +0 -0
  13. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_bulk_pk_update.py +0 -0
  14. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_core_joins.py +0 -0
  15. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_core_walk.py +0 -0
  16. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_engine_policies.py +0 -0
  17. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_entity_selectables.py +0 -0
  18. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_migration_render.py +0 -0
  19. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_outer_joins.py +0 -0
  20. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_persisted_owner.py +0 -0
  21. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/_secondary_scope.py +0 -0
  22. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/admission.py +0 -0
  23. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/audit.py +0 -0
  24. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/audit_redaction.py +0 -0
  25. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/audit_sinks.py +0 -0
  26. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/base.py +0 -0
  27. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/bulk_audit.py +0 -0
  28. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/bulk_audit_targets.py +0 -0
  29. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/bulk_guard.py +0 -0
  30. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/callbacks.py +0 -0
  31. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/core_scope.py +0 -0
  32. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/data_compatibility.py +0 -0
  33. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/deps.py +0 -0
  34. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/engine_options.py +0 -0
  35. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/extensions.py +0 -0
  36. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/flush_guard.py +0 -0
  37. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/independent.py +0 -0
  38. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/insert_guard.py +0 -0
  39. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/insert_scope.py +0 -0
  40. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/leases.py +0 -0
  41. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/listeners.py +0 -0
  42. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/migration_state.py +0 -0
  43. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/mixins.py +0 -0
  44. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/model_registry.py +0 -0
  45. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/pagination.py +0 -0
  46. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/provider.py +0 -0
  47. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/py.typed +0 -0
  48. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/query_filter.py +0 -0
  49. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/query_stats.py +0 -0
  50. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/resource_policy.py +0 -0
  51. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/scope_predicates.py +0 -0
  52. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/search.py +0 -0
  53. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/session.py +0 -0
  54. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/stored_settings.py +0 -0
  55. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/tenancy.py +0 -0
  56. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/tenant_cleanup.py +0 -0
  57. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/transaction.py +0 -0
  58. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/types.py +0 -0
  59. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/user_directory_sql.py +0 -0
  60. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/simple_module_db/writes.py +0 -0
  61. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_alembic_project.py +0 -0
  62. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_audit_models.py +0 -0
  63. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_bulk_audit_support.py +0 -0
  64. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_core_scope_models.py +0 -0
  65. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_db_dml_scope_models.py +0 -0
  66. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_db_read_scope_models.py +0 -0
  67. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_db_shape_matrix.py +0 -0
  68. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/_models.py +0 -0
  69. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/conftest.py +0 -0
  70. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_audit.py +0 -0
  71. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_audit_per_engine.py +0 -0
  72. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_aware_datetime.py +0 -0
  73. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_base.py +0 -0
  74. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_aliased_join_text_scope.py +0 -0
  75. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_aliased_selectable_scope.py +0 -0
  76. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_audit_regressions.py +0 -0
  77. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_bulk_audit.py +0 -0
  78. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_bulk_audit_targets.py +0 -0
  79. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_connection_policy.py +0 -0
  80. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_core_full_setup_join.py +0 -0
  81. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_core_in_orm_select.py +0 -0
  82. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_core_join_scope.py +0 -0
  83. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_core_nested_scope.py +0 -0
  84. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_credential_audit.py +0 -0
  85. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_credential_log_masking.py +0 -0
  86. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_data_compatibility_doctor.py +0 -0
  87. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_dml_target_scope.py +0 -0
  88. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_doctor_session_filters.py +0 -0
  89. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_engine_options.py +0 -0
  90. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_engine_policy_resolution.py +0 -0
  91. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_extensions.py +0 -0
  92. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_extensions_pg.py +0 -0
  93. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_flush_persisted_owner.py +0 -0
  94. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_independent_checkpoint.py +0 -0
  95. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_insert_select_tenant.py +0 -0
  96. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_insert_value_subqueries.py +0 -0
  97. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_lease_lock_wait.py +0 -0
  98. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_listeners_attach.py +0 -0
  99. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_logging.py +0 -0
  100. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_migration_doctor.py +0 -0
  101. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_migration_review_regressions.py +0 -0
  102. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_migration_state.py +0 -0
  103. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_nested_dml_and_bulk_api.py +0 -0
  104. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_pagination.py +0 -0
  105. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_pagination_options.py +0 -0
  106. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_query_stats.py +0 -0
  107. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_relationship_loader_scope.py +0 -0
  108. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_response_finalization.py +0 -0
  109. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_savepoint_callbacks.py +0 -0
  110. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_secondary_scope_execution.py +0 -0
  111. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_shape_matrix_dml.py +0 -0
  112. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_shape_matrix_joins.py +0 -0
  113. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_shape_matrix_selects.py +0 -0
  114. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_sqlite_admission_lock.py +0 -0
  115. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_sqlite_savepoint.py +0 -0
  116. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_sqlite_wal_switch.py +0 -0
  117. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_stored_settings.py +0 -0
  118. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_strict_insert_context.py +0 -0
  119. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_tenant_cleanup.py +0 -0
  120. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_tenant_upsert_guard.py +0 -0
  121. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_user_directory_sql.py +0 -0
  122. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_db_write_marks.py +0 -0
  123. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_default_tenant.py +0 -0
  124. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_independent_session.py +0 -0
  125. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_migrations.py +0 -0
  126. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_migrations_sqlite_index_compare.py +0 -0
  127. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_mixins.py +0 -0
  128. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_multi_tenancy.py +0 -0
  129. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_query_filter_outer_joins.py +0 -0
  130. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_query_filter_rel_outer_joins.py +0 -0
  131. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_query_filters.py +0 -0
  132. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_rollback_logging.py +0 -0
  133. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_session.py +0 -0
  134. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_soft_delete_shapes.py +0 -0
  135. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_tenant_dml.py +0 -0
  136. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_tenant_shapes.py +0 -0
  137. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_tenant_strict.py +0 -0
  138. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_transaction.py +0 -0
  139. {simple_module_db-0.0.45 → simple_module_db-0.0.46}/tests/test_writes.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: simple_module_db
3
- Version: 0.0.45
3
+ Version: 0.0.46
4
4
  Summary: Per-module SQLModel Base, async session, standard mixins (Audit, SoftDelete, MultiTenant, Versioned) for simple_module
5
5
  Project-URL: Homepage, https://github.com/antosubash/simple_module_python
6
6
  Project-URL: Repository, https://github.com/antosubash/simple_module_python
@@ -25,7 +25,7 @@ Requires-Dist: aiosqlite<0.23,>=0.20
25
25
  Requires-Dist: alembic<2,>=1.16.3
26
26
  Requires-Dist: asyncpg<0.33,>=0.30
27
27
  Requires-Dist: psycopg2-binary<3,>=2.9
28
- Requires-Dist: simple-module-core==0.0.45
28
+ Requires-Dist: simple-module-core==0.0.46
29
29
  Requires-Dist: sqlalchemy[asyncio]<3,>=2.0.2
30
30
  Requires-Dist: sqlmodel<0.1,>=0.0.22
31
31
  Description-Content-Type: text/markdown
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "simple_module_db"
3
- version = "0.0.45"
3
+ version = "0.0.46"
4
4
  description = "Per-module SQLModel Base, async session, standard mixins (Audit, SoftDelete, MultiTenant, Versioned) for simple_module"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -28,7 +28,7 @@ dependencies = [
28
28
  # The async app code goes through asyncpg; migration env.py rewrites the
29
29
  # URL to +psycopg2 because Alembic's online migrations are sync-only.
30
30
  "psycopg2-binary>=2.9,<3",
31
- "simple_module_core==0.0.45",
31
+ "simple_module_core==0.0.46",
32
32
  "sqlalchemy[asyncio]>=2.0.2,<3",
33
33
  "sqlmodel>=0.0.22,<0.1",
34
34
  ]
@@ -11,8 +11,9 @@ schema. Postgres reflects the ordering, so it compares these faithfully.
11
11
  ``make_include_object`` can skip it on SQLite. Anything else still reaches
12
12
  Alembic: a different column list, a uniqueness change, or a real expression
13
13
  such as ``lower(email)``. A change of ordering alone (``DESC`` to ``ASC``) is not
14
- reported on SQLite, which cannot reflect ordering at all; the Postgres round
15
- trip (``make migrations-roundtrip-pg``) is what catches it.
14
+ reported on SQLite, which cannot reflect ordering at all; a PostgreSQL
15
+ round trip (``make migrations-roundtrip-pg`` in the framework repository) is what
16
+ catches it.
16
17
  """
17
18
 
18
19
  from __future__ import annotations
@@ -7,7 +7,8 @@ or never sees, and neither ``alembic check`` nor the boot check notices:
7
7
  integer default on a boolean column. :func:`rewrite_boolean_defaults` and
8
8
  :func:`render_boolean_default` emit ``sa.false()`` / ``sa.true()`` instead.
9
9
  * Expression indexes (``lower(email)``) are skipped on SQLite because it cannot
10
- reflect them. :func:`warn_unverifiable_expression_indexes` says so loudly.
10
+ reflect them. :func:`warn_unverifiable_expression_indexes` names the ones a
11
+ revision misses.
11
12
  * ``op.drop_table`` leaves ``CREATE TYPE`` behind on Postgres, so a downgrade
12
13
  followed by an upgrade fails with ``DuplicateObject``. Use
13
14
  :func:`drop_enums_if_postgres` in the revision's ``downgrade``.
@@ -23,8 +24,11 @@ from collections.abc import Iterator
23
24
  from typing import Any
24
25
 
25
26
  import sqlalchemy as sa
26
- from alembic.operations.ops import AddColumnOp, AlterColumnOp, CreateTableOp
27
- from simple_module_core.diagnostics._expression_index import find_expression_indexes
27
+ from alembic.operations.ops import AddColumnOp, AlterColumnOp, CreateIndexOp, CreateTableOp
28
+ from simple_module_core.diagnostics._expression_index import (
29
+ expression_index_advice,
30
+ find_expression_indexes,
31
+ )
28
32
  from sqlalchemy.dialects import postgresql
29
33
  from sqlalchemy.schema import DefaultClause
30
34
  from sqlalchemy.sql.elements import ColumnElement, False_, TextClause, True_
@@ -116,38 +120,61 @@ def rewrite_boolean_defaults(upgrade_ops: Any) -> None:
116
120
  column.server_default = DefaultClause(replacement)
117
121
 
118
122
 
119
- def warn_unverifiable_expression_indexes(dialect_name: str | None, metadata: sa.MetaData) -> list:
120
- """Warn when autogenerating on SQLite while models declare expression indexes.
121
-
122
- A warning rather than an error: autogenerate also runs for ordinary,
123
- unrelated revisions, and failing every one of them for an index that is
124
- already in history would block normal work. The warning is emitted on every
125
- run, names each index, and ``alembic check`` on Postgres
126
- (``make migrations-roundtrip-pg``) is the real gate. Returns the
123
+ def warn_unverifiable_expression_indexes(
124
+ dialect_name: str | None,
125
+ metadata: sa.MetaData,
126
+ upgrade_ops: Any = None,
127
+ existing_index_names: set[str] | None = None,
128
+ ) -> list:
129
+ """Warn when a revision autogenerated on SQLite misses expression indexes.
130
+
131
+ SQLite cannot reflect an expression index's definition, so autogenerate
132
+ skips them, but it does record their names. The revision hook passes
133
+ ``upgrade_ops`` (after re-emitting the indexes of new tables) and the index
134
+ names the database already has; then only indexes that are in neither are
135
+ named: one added to an existing table, typically. A fresh initial schema
136
+ creates them, and a revision on another table leaves them alone, so both
137
+ stay silent (GH #917). Without those arguments every expression index in
138
+ ``metadata`` is named.
139
+
140
+ A warning rather than an error: a PostgreSQL run is the real gate, and an
141
+ index whose name exists may still differ in definition. Returns the
127
142
  ``(table, index)`` pairs it warned about.
128
143
  """
129
144
  if dialect_name != "sqlite":
130
145
  return []
131
146
  found = find_expression_indexes(metadata.tables.values())
147
+ if upgrade_ops is not None:
148
+ emitted = {
149
+ op.index_name for op in iter_ops_recursive(upgrade_ops) if isinstance(op, CreateIndexOp)
150
+ }
151
+ existing = existing_index_names or set()
152
+ found = [(t, i) for t, i in found if i not in emitted and i not in existing]
132
153
  if not found:
133
154
  return []
134
155
  listing = "\n".join(f" - {index} on {table}" for table, index in found)
135
156
  logger.warning(
136
157
  "\n%s\n"
137
158
  "WARNING: autogenerating on SQLite. SQLite cannot reflect expression indexes,\n"
138
- "so this revision does NOT account for the following (they are neither\n"
139
- "created nor verified here) and `alembic check` will not notice drift:\n"
159
+ "so this revision does NOT create the following, which the models declare\n"
160
+ "and the database does not have:\n"
140
161
  "%s\n"
141
- "Add them to the revision by hand and verify on PostgreSQL before merging\n"
142
- "(`make migrations-roundtrip-pg`). See docs/module-authoring.md, Migrations.\n"
162
+ "Add them to the revision by hand and %s.\n"
143
163
  "%s",
144
164
  "=" * 78,
145
165
  listing,
166
+ expression_index_advice(),
146
167
  "=" * 78,
147
168
  )
148
169
  return found
149
170
 
150
171
 
172
+ def sqlite_index_names(connection: Any) -> set[str]:
173
+ """Every index name in a SQLite database, expression indexes included."""
174
+ rows = connection.execute(sa.text("SELECT name FROM sqlite_master WHERE type = 'index'"))
175
+ return {row[0] for row in rows}
176
+
177
+
151
178
  def drop_enums_if_postgres(op: Any, *names: str) -> None:
152
179
  """Drop the named enum types on PostgreSQL; a no-op elsewhere.
153
180
 
@@ -36,6 +36,7 @@ from simple_module_db.extensions import ( # noqa: F401 (ensure_db_extensions r
36
36
  from simple_module_db.migration_portability import (
37
37
  iter_ops_recursive,
38
38
  rewrite_boolean_defaults,
39
+ sqlite_index_names,
39
40
  warn_unverifiable_expression_indexes,
40
41
  )
41
42
 
@@ -195,8 +196,8 @@ def make_process_revision_directives(
195
196
  alone, which keeps this dialect-agnostic rather than special-casing SQLite.
196
197
 
197
198
  It also rewrites ``0``/``1``/``false``/``true`` server defaults on Boolean
198
- columns to ``sa.false()``/``sa.true()`` and, when running on SQLite, warns
199
- that expression indexes cannot be verified (see
199
+ columns to ``sa.false()``/``sa.true()`` and, when running on SQLite, names
200
+ the expression indexes the revision misses (see
200
201
  :mod:`simple_module_db.migration_portability`).
201
202
 
202
203
  Call as::
@@ -213,15 +214,17 @@ def make_process_revision_directives(
213
214
  expression_indexes.setdefault(table.name, []).append(index)
214
215
 
215
216
  def process_revision_directives(context, revision, directives):
216
- warn_unverifiable_expression_indexes(
217
- getattr(getattr(context, "dialect", None), "name", None), metadata
218
- )
217
+ dialect_name = getattr(getattr(context, "dialect", None), "name", None)
219
218
  for script in directives:
220
219
  upgrade_ops = getattr(script, "upgrade_ops", None)
221
220
  if upgrade_ops is not None:
222
221
  rewrite_boolean_defaults(upgrade_ops)
223
222
  if expression_indexes:
224
223
  _inject_create_index_after_create_table(upgrade_ops, expression_indexes)
224
+ # After the re-emission, so an index this revision creates is not reported.
225
+ warn_unverifiable_expression_indexes(
226
+ dialect_name, metadata, upgrade_ops, _existing_sqlite_indexes(context)
227
+ )
225
228
  downgrade_ops = getattr(script, "downgrade_ops", None)
226
229
  if downgrade_ops is not None:
227
230
  # A downgrade re-creates dropped tables/columns from reflected
@@ -233,6 +236,13 @@ def make_process_revision_directives(
233
236
  return process_revision_directives
234
237
 
235
238
 
239
+ def _existing_sqlite_indexes(context) -> set[str]:
240
+ connection = getattr(context, "connection", None)
241
+ if connection is None or connection.dialect.name != "sqlite":
242
+ return set()
243
+ return sqlite_index_names(connection)
244
+
245
+
236
246
  def _existing_index_names(container, op_type) -> set[str | None]:
237
247
  """Names of every ``op_type`` index op already present anywhere under ``container``."""
238
248
  return {
@@ -0,0 +1,95 @@
1
+ """The SQLite expression-index warning names only what the revision missed (GH #917).
2
+
3
+ The hook used to warn about every expression index in the metadata, before it
4
+ had re-emitted them, so a fresh app's initial schema (which does create the
5
+ index) and every unrelated module migration told users to hand-edit it in.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import logging
11
+
12
+ import sqlalchemy as sa
13
+ from alembic.autogenerate import produce_migrations
14
+ from alembic.runtime.migration import MigrationContext
15
+ from simple_module_core.diagnostics._expression_index import expression_index_advice
16
+ from simple_module_db.migrations import make_process_revision_directives, render_item
17
+
18
+
19
+ def _metadata(*, extra_column: bool = False) -> sa.MetaData:
20
+ md = sa.MetaData()
21
+ sa.Table(
22
+ "people",
23
+ md,
24
+ sa.Column("id", sa.Integer, primary_key=True),
25
+ sa.Column("email", sa.String),
26
+ *([sa.Column("nickname", sa.String)] if extra_column else []),
27
+ sa.Index("ix_people_email_lower", sa.text("lower(email)"), unique=True),
28
+ )
29
+ sa.Table("orders", md, sa.Column("id", sa.Integer, primary_key=True))
30
+ return md
31
+
32
+
33
+ def _run_hook(existing: sa.MetaData | None, target: sa.MetaData, caplog) -> str:
34
+ engine = sa.create_engine("sqlite://")
35
+ with engine.connect() as conn:
36
+ if existing is not None:
37
+ existing.create_all(conn)
38
+ ctx = MigrationContext.configure(conn, opts={"render_item": render_item})
39
+ script = produce_migrations(ctx, target)
40
+ with caplog.at_level(logging.WARNING, logger="alembic.env"):
41
+ make_process_revision_directives(target)(ctx, None, [script])
42
+ return caplog.text
43
+
44
+
45
+ def test_initial_schema_that_creates_the_index_is_silent(caplog) -> None:
46
+ assert "ix_people_email_lower" not in _run_hook(None, _metadata(), caplog)
47
+
48
+
49
+ def test_revision_on_another_table_is_silent(caplog) -> None:
50
+ existing = _metadata()
51
+ existing.remove(existing.tables["orders"])
52
+ assert "ix_people_email_lower" not in _run_hook(existing, _metadata(), caplog)
53
+
54
+
55
+ def test_revision_altering_a_table_whose_index_exists_is_silent(caplog) -> None:
56
+ """SQLite records the index's name even though it cannot reflect it."""
57
+ text = _run_hook(_metadata(), _metadata(extra_column=True), caplog)
58
+ assert "ix_people_email_lower" not in text
59
+
60
+
61
+ def test_an_expression_index_added_to_an_existing_table_warns(caplog) -> None:
62
+ """Autogenerate emits nothing for it on SQLite, so the revision misses it."""
63
+ existing = _metadata()
64
+ existing.tables["people"].indexes.clear()
65
+ text = _run_hook(existing, _metadata(), caplog)
66
+ assert "ix_people_email_lower on people" in text
67
+
68
+
69
+ def test_advice_outside_this_repo_points_at_what_exists(tmp_path, monkeypatch) -> None:
70
+ monkeypatch.chdir(tmp_path)
71
+ advice = expression_index_advice()
72
+ assert "migrations-roundtrip-pg" not in advice
73
+ assert "alembic -c host/alembic.ini check" in advice
74
+ assert "PostgreSQL" in advice
75
+
76
+
77
+ def test_advice_in_a_checkout_with_the_roundtrip_target(tmp_path, monkeypatch) -> None:
78
+ (tmp_path / "Makefile").write_text("migrations-roundtrip-pg:\n\techo\n", encoding="utf-8")
79
+ monkeypatch.chdir(tmp_path)
80
+ assert "make migrations-roundtrip-pg" in expression_index_advice()
81
+
82
+
83
+ def test_a_ci_prefixed_target_is_not_the_roundtrip_target(tmp_path, monkeypatch) -> None:
84
+ (tmp_path / "Makefile").write_text("ci-migrations-roundtrip-pg:\n\techo\n", encoding="utf-8")
85
+ monkeypatch.chdir(tmp_path)
86
+ assert "make migrations-roundtrip-pg" not in expression_index_advice()
87
+
88
+
89
+ def test_dropping_the_index_does_not_count_as_creating_it() -> None:
90
+ from alembic.operations.ops import DropIndexOp, UpgradeOps
91
+ from simple_module_db.migration_portability import warn_unverifiable_expression_indexes
92
+
93
+ ops = UpgradeOps(ops=[DropIndexOp("ix_people_email_lower", table_name="people")])
94
+ found = warn_unverifiable_expression_indexes("sqlite", _metadata(), ops, set())
95
+ assert found == [("people", "ix_people_email_lower")]
@@ -103,10 +103,13 @@ def test_expression_index_warning_silent_elsewhere(caplog):
103
103
  assert caplog.text == ""
104
104
 
105
105
 
106
- def test_hook_warns_during_autogenerate_on_sqlite(caplog):
106
+ def test_hook_is_silent_when_the_revision_creates_the_index(caplog):
107
+ """The re-emitted index is in the revision (GH #917); scope tests in
108
+ test_db_expression_index_warning_scope.py."""
107
109
  with caplog.at_level(logging.WARNING, logger="alembic.env"):
108
- _autogenerate(_metadata(with_index=True))
109
- assert "ix_flags_email_lower" in caplog.text
110
+ code, _, _ = _autogenerate(_metadata(with_index=True))
111
+ assert "ix_flags_email_lower" in code
112
+ assert "ix_flags_email_lower" not in caplog.text
110
113
 
111
114
 
112
115
  def test_plain_column_index_is_not_flagged():