surreal-orm-lite 0.36.3__tar.gz → 0.36.4__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 (50) hide show
  1. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/CHANGELOG.md +57 -0
  2. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/PKG-INFO +14 -3
  3. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/README.md +13 -2
  4. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/pyproject.toml +1 -1
  5. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/__init__.py +1 -1
  6. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/connection_manager.py +19 -4
  7. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_base.py +30 -9
  8. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/search.py +98 -11
  9. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/transaction.py +47 -4
  10. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/.gitignore +0 -0
  11. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/LICENSE +0 -0
  12. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/Makefile +0 -0
  13. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/_sdk.py +0 -0
  14. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/aggregations.py +0 -0
  15. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/auth.py +0 -0
  16. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/cache.py +0 -0
  17. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/changefeed.py +0 -0
  18. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/concurrency.py +0 -0
  19. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/connection_config.py +0 -0
  20. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/constants.py +0 -0
  21. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/decoding.py +0 -0
  22. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/define_parser.py +0 -0
  23. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/edge.py +0 -0
  24. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/embedded.py +0 -0
  25. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/encoding.py +0 -0
  26. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/enum.py +0 -0
  27. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/exceptions.py +0 -0
  28. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/functions.py +0 -0
  29. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/geo.py +0 -0
  30. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/introspection.py +0 -0
  31. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/live.py +0 -0
  32. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_auth.py +0 -0
  33. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_ddl.py +0 -0
  34. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/model_generator.py +0 -0
  35. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/py.typed +0 -0
  36. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/q.py +0 -0
  37. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/query_set.py +0 -0
  38. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/routing.py +0 -0
  39. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/schema.py +0 -0
  40. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/schema_state.py +0 -0
  41. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/signals.py +0 -0
  42. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/subquery.py +0 -0
  43. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/surql_literal.py +0 -0
  44. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/testing/__init__.py +0 -0
  45. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/testing/database.py +0 -0
  46. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/testing/fixtures.py +0 -0
  47. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/time_travel.py +0 -0
  48. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/types.py +0 -0
  49. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/utils.py +0 -0
  50. {surreal_orm_lite-0.36.3 → surreal_orm_lite-0.36.4}/src/surreal_orm_lite/vector.py +0 -0
@@ -5,6 +5,63 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.36.4] - 2026-10-24
9
+
10
+ Follow-ups to the review of v0.36.2 (#322): an index that `remove_index(tx=…)` removed earlier
11
+ in a transaction no longer blocks `remove_analyzer(tx=…)` or `define_analyzer(tx=…)` in the same
12
+ block, `define_indexes(tx=…)` no longer defines an index with an analyzer the block removed or
13
+ redefined, and the v0.36.2 fix is tested on vector (HNSW) indexes. No new public API.
14
+
15
+ ### Fixed
16
+
17
+ - **`remove_analyzer(tx=…)` and `define_analyzer(tx=…)` no longer count an index the block
18
+ removed** (#323). Both find the indexes that use the analyzer by reading `INFO FOR TABLE`
19
+ outside the transaction, where an index that `remove_index(tx=…)` removed earlier in the block
20
+ still stands. Once a block had removed the last index using an analyzer, `remove_analyzer(tx=…)`
21
+ was refused on SurrealDB 2.x and the embedded engine ("in use by index …"), and
22
+ `define_analyzer(…, overwrite=True, tx=…)` was refused on every engine as a redefinition with
23
+ indexes to rebuild. Both now leave out the indexes the transaction noted as removed: the
24
+ analyzer can be removed, or redefined without `REBUILD INDEX`, in the same block, on SurrealDB
25
+ 2.x and 3.x, over WebSocket and HTTP and on the embedded engine. An index still standing keeps
26
+ the refusal, with the same message: another table's, another index of the same table, one
27
+ removed then defined again in the block, one removed with a raw `tx.add("REMOVE INDEX …")`
28
+ (not tracked), or any index when no `tx=` is given. SurrealDB 3.x already accepted the removal,
29
+ since the server checks it in the transaction. An index queued earlier in a buffered block is
30
+ still not seen.
31
+ - **`define_analyzer(tx=…)` after `remove_analyzer(tx=…)` of the same name defines the analyzer
32
+ again** (#323). It compared the analyzer with committed state, where the removed one still
33
+ stood, so an unchanged definition (or any definition without `overwrite=True`) applied nothing
34
+ and the commit left the database without the analyzer. The removed analyzer now counts as
35
+ missing: it is defined again after its removal, with no index read nor rebuilt.
36
+
37
+ ### Changed
38
+
39
+ - **`define_indexes(tx=…)` refuses an index whose analyzer the block removed or redefined**
40
+ (#323), whether the model declares the analyzer as an `Analyzer` or names it by `str`. Until
41
+ v0.36.3 it read the analyzers from committed state only, so it defined such an index: after
42
+ `remove_analyzer(tx=…)`, the index stood without its analyzer once the block committed, and
43
+ every write to the table was then refused; after `define_analyzer(…, overwrite=True, tx=…)`,
44
+ SurrealDB built the index over the table's records with the committed definition, not the new
45
+ one (on SurrealDB 3.x a search then missed those records, without an error). A removed analyzer
46
+ now counts as missing (an `Analyzer` is refused as one to define in a transaction, a name as one
47
+ that does not exist), and an index whose analyzer the block redefined is refused with a new
48
+ message. Both refusals raise `SurrealDbError` before any DDL and do not fail the block. To
49
+ migrate an index to a new analyzer, call `define_analyzer()` without `tx=` first.
50
+
51
+ ### Notes
52
+
53
+ - **Tests**: the v0.36.2 fix (#312) is now covered on a vector (HNSW) index: `remove_index(tx=…)`
54
+ then `define_indexes(tx=…)` in one block, over WebSocket, HTTP and the embedded engine, on an
55
+ empty table and on one holding a record. The index stands after the commit and serves
56
+ `similar_to()`; on SurrealDB 2.x, a table holding a record fails the commit with the retryable
57
+ `SurrealDbConflictError` and keeps the index, as the README's v0.36.2 row says.
58
+ - **Docs**: README section 36 (Transactions) and the docstrings of `define_indexes()`,
59
+ `remove_index()`, `define_analyzer()` and `remove_analyzer()` describe these cases. No
60
+ behaviour-table row changes: for an index removed with `remove_index(tx=…)`, the analyzer
61
+ checks now give the same result on SurrealDB 2.x and 3.x and on the embedded engine.
62
+ - Server pins unchanged: tested against SurrealDB 3.3.4 and 2.7.0 and on the embedded engine,
63
+ plus 3.1.5 for backward compatibility.
64
+
8
65
  ## [0.36.3] - 2026-10-24
9
66
 
10
67
  Follow-up to the review of v0.36.0 (#316): a vector field declared with `| None` keeps its
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: surreal-orm-lite
3
- Version: 0.36.3
3
+ Version: 0.36.4
4
4
  Summary: Lightweight Django-style ORM for SurrealDB using the official Python SDK. Async support with Pydantic validation.
5
5
  Project-URL: Homepage, https://github.com/EulogySnowfall/SurrealDB-ORM-lite
6
6
  Project-URL: Documentation, https://github.com/EulogySnowfall/SurrealDB-ORM-lite
@@ -6109,8 +6109,19 @@ print(await NoteV2.objects().search(text="physics").count())
6109
6109
  in the same block, fails the commit with a retryable `SurrealDbConflictError` that a retry meets again; so does a
6110
6110
  block that removes an index of a table that held records and defines it again, where the index stands as before.
6111
6111
  Nothing of such a block is committed. On an empty table both commit, as on 3.x.
6112
- - `remove_analyzer(tx=…)` reads committed state to find the indexes that use the analyzer: an index queued earlier in
6113
- a buffered block is not seen.
6112
+ - `remove_analyzer(tx=…)` (on SurrealDB 2.x and the embedded engine; 3.x checks in the transaction itself) and
6113
+ `define_analyzer(tx=…)` read committed state to find the indexes that use the analyzer. An index `remove_index(tx=…)`
6114
+ removed earlier in the block no longer counts, although committed state still holds it: once the block has removed the
6115
+ last index that uses an analyzer, it can remove the analyzer, or redefine it without `REBUILD INDEX`, on every engine.
6116
+ An index removed then defined again in the block, or removed with a raw `tx.add("REMOVE INDEX …")`, still counts
6117
+ there, and an index queued earlier in a buffered block is not seen.
6118
+ - Once the block has removed or redefined an analyzer, an index that uses it can no longer be defined in the block,
6119
+ whether the model declares the analyzer as an `Analyzer` or names it by `str`: `define_indexes(tx=…)` raises
6120
+ `SurrealDbError` before any DDL. A removed analyzer counts as missing, although committed state still holds it — the
6121
+ index would stand without it after the commit, and every write to the table would be refused. A redefined one would
6122
+ not be followed: SurrealDB would build the index with the committed definition. The refusal does not fail the block.
6123
+ To migrate an index to a new analyzer, call `define_analyzer()` without `tx=` first. `define_analyzer(tx=…)` after
6124
+ `remove_analyzer(tx=…)` of the same name defines the analyzer again, after its removal.
6114
6125
  - A search in an interactive transaction sees the transaction's writes; in a buffered one, a read raises, as every
6115
6126
  read does there.
6116
6127
  - In a transaction, a search on a field without a full-text index raises the same `SurrealDbError`, which the
@@ -6058,8 +6058,19 @@ print(await NoteV2.objects().search(text="physics").count())
6058
6058
  in the same block, fails the commit with a retryable `SurrealDbConflictError` that a retry meets again; so does a
6059
6059
  block that removes an index of a table that held records and defines it again, where the index stands as before.
6060
6060
  Nothing of such a block is committed. On an empty table both commit, as on 3.x.
6061
- - `remove_analyzer(tx=…)` reads committed state to find the indexes that use the analyzer: an index queued earlier in
6062
- a buffered block is not seen.
6061
+ - `remove_analyzer(tx=…)` (on SurrealDB 2.x and the embedded engine; 3.x checks in the transaction itself) and
6062
+ `define_analyzer(tx=…)` read committed state to find the indexes that use the analyzer. An index `remove_index(tx=…)`
6063
+ removed earlier in the block no longer counts, although committed state still holds it: once the block has removed the
6064
+ last index that uses an analyzer, it can remove the analyzer, or redefine it without `REBUILD INDEX`, on every engine.
6065
+ An index removed then defined again in the block, or removed with a raw `tx.add("REMOVE INDEX …")`, still counts
6066
+ there, and an index queued earlier in a buffered block is not seen.
6067
+ - Once the block has removed or redefined an analyzer, an index that uses it can no longer be defined in the block,
6068
+ whether the model declares the analyzer as an `Analyzer` or names it by `str`: `define_indexes(tx=…)` raises
6069
+ `SurrealDbError` before any DDL. A removed analyzer counts as missing, although committed state still holds it — the
6070
+ index would stand without it after the commit, and every write to the table would be refused. A redefined one would
6071
+ not be followed: SurrealDB would build the index with the committed definition. The refusal does not fail the block.
6072
+ To migrate an index to a new analyzer, call `define_analyzer()` without `tx=` first. `define_analyzer(tx=…)` after
6073
+ `remove_analyzer(tx=…)` of the same name defines the analyzer again, after its removal.
6063
6074
  - A search in an interactive transaction sees the transaction's writes; in a buffered one, a read raises, as every
6064
6075
  read does there.
6065
6076
  - In a transaction, a search on a field without a full-text index raises the same `SurrealDbError`, which the
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "surreal-orm-lite"
3
- version = "0.36.3"
3
+ version = "0.36.4"
4
4
  description = "Lightweight Django-style ORM for SurrealDB using the official Python SDK. Async support with Pydantic validation."
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -1,4 +1,4 @@
1
- __version__ = "0.36.3"
1
+ __version__ = "0.36.4"
2
2
 
3
3
  from ._sdk import BoundExcluded, BoundIncluded
4
4
  from .aggregations import Aggregation, Avg, Count, Max, Min, Sum
@@ -2746,7 +2746,15 @@ class SurrealDBConnectionManager:
2746
2746
 
2747
2747
  With *tx*, a new analyzer — or a redefinition no index uses — joins the transaction. A redefinition with indexes
2748
2748
  to rebuild is refused before any DDL: the servers rebuild an index with the analyzers committed before the
2749
- transaction began, so the rebuild must follow the committed definition.
2749
+ transaction began, so the rebuild must follow the committed definition. An index ``Model.remove_index()``
2750
+ removed earlier with the same *tx* is not to rebuild, although committed state still holds it: once the block
2751
+ has removed every index that uses the analyzer, the redefinition joins the transaction, without
2752
+ ``REBUILD INDEX``. An index removed then defined again in the block (``define_indexes(tx=…)``), or removed with
2753
+ a raw ``tx.add("REMOVE INDEX …")``, which the transaction does not track, still counts. The transaction notes
2754
+ the analyzer it defines or redefines: ``Model.define_indexes(tx=…)`` then refuses, before any DDL, to define in
2755
+ the block an index that uses it — SurrealDB would build it with the committed definition. An analyzer
2756
+ :meth:`remove_analyzer` removed earlier with the same *tx* counts as missing, although committed state still
2757
+ holds it: it is defined again after its removal.
2750
2758
 
2751
2759
  :param analyzer: the ``Analyzer`` to define.
2752
2760
  :param overwrite: ``True`` (default) redefines a changed analyzer (``OVERWRITE``) and rebuilds its indexes;
@@ -2790,9 +2798,16 @@ class SurrealDBConnectionManager:
2790
2798
  removed``. SurrealDB 3.x refuses it itself; on SurrealDB 2.x and the embedded engine, which would remove it and
2791
2799
  then refuse every write to that table, the ORM reads the line (``client.version()``), then ``INFO FOR DB`` and
2792
2800
  ``INFO FOR TABLE`` of every table, and refuses it before any DDL. Remove its indexes first
2793
- (``Model.remove_index()``). In a buffered transaction those reads see the committed state: an index queued
2794
- earlier in the block is not seen. No cached read is dropped: a removal succeeds only when no index uses the
2795
- analyzer.
2801
+ (``Model.remove_index()``). With *tx*, those reads see the committed state: an index ``Model.remove_index()``
2802
+ removed earlier with the same *tx* is left out, although committed state still holds it, so a block that
2803
+ removes the last index using the analyzer and then the analyzer commits, as on SurrealDB 3.x, which checks in
2804
+ the transaction; an index removed then defined again in the block, or removed with a raw
2805
+ ``tx.add("REMOVE INDEX …")``, which the transaction does not track, still counts, and an index queued earlier in
2806
+ a buffered block is not seen. The transaction notes the removal: ``Model.define_indexes(tx=…)`` then counts the
2807
+ analyzer as missing, although committed state still holds it, and refuses, before any DDL, to define in the
2808
+ block an index that uses it — the commit would leave that index without its analyzer, and every write to its
2809
+ table refused; :meth:`define_analyzer` with the same *tx* defines it again. No cached read is dropped: a
2810
+ removal succeeds only when no index uses the analyzer.
2796
2811
 
2797
2812
  :param analyzer: the analyzer's name (letters, digits, underscores; not starting with a digit).
2798
2813
  :param missing_ok: ``True`` (default) emits ``REMOVE ANALYZER IF EXISTS …``, so a missing analyzer is not an
@@ -62,8 +62,10 @@ from .schema import (
62
62
  from .schema_state import FieldState, TableState
63
63
  from .search import (
64
64
  Index,
65
+ analyzers_after_removals,
65
66
  apply_analyzers,
66
67
  apply_rebuilds,
68
+ check_analyzers_redefined_in,
67
69
  check_indexes_config,
68
70
  check_named_analyzers,
69
71
  checked_overwrite,
@@ -1332,6 +1334,14 @@ class BaseSurrealModel(BaseModel):
1332
1334
  ``SurrealDbConflictError`` that a retry meets again: nothing of the block is committed and the index stands as
1333
1335
  before; on an empty table it commits, as on 3.x.
1334
1336
 
1337
+ **An analyzer changed in the transaction** (issue #323). With *tx*, an analyzer
1338
+ ``SurrealDBConnectionManager.remove_analyzer()`` removed with the same *tx* counts as missing, although committed
1339
+ state still holds it: an index using it is refused before any DDL — an ``Analyzer`` object as one to define in a
1340
+ transaction, a name by ``str`` as one that does not exist — instead of standing without it after the commit,
1341
+ which would refuse every write to the table. An index to define (or redefine) whose analyzer
1342
+ ``define_analyzer()`` redefined with the same *tx* is refused too, whether the model declares it or names it:
1343
+ SurrealDB would build it with the committed definition. Call ``define_analyzer()`` without *tx* first.
1344
+
1335
1345
  Names are written in backticks, filters re-rendered from a closed grammar, numbers from Python numbers,
1336
1346
  comments as string literals. A refusal names the analyzer or the index and its table, never the statement:
1337
1347
  ``Can't apply index definition: 'post_title' on table 'post' -> <reason>``. Applying needs a session allowed to
@@ -1342,16 +1352,18 @@ class BaseSurrealModel(BaseModel):
1342
1352
  overwrite: ``True`` (default) redefines a changed analyzer or index — the model wins. ``False`` defines
1343
1353
  what is missing (``IF NOT EXISTS``) and leaves what exists as it is.
1344
1354
  tx: optional transaction the index statements are applied with; the reads happen before anything is
1345
- queued. An index :meth:`remove_index` removed with it counts as missing.
1355
+ queued. An index :meth:`remove_index` removed with it counts as missing, and so does an analyzer
1356
+ ``remove_analyzer()`` removed with it.
1346
1357
 
1347
1358
  Raises:
1348
1359
  TypeError: *overwrite* is not a ``bool`` — before any request.
1349
1360
  SurrealDbError: *tx* belongs to another connection — before any request; an analyzer named by ``str`` does
1350
- not exist, or *tx* is given and an analyzer must be defined or redefined, or an index must be defined
1351
- or redefined (one removed in the block included) after the transaction wrote records to the model's
1352
- table (the message names the model and the table) — before any DDL; the server refused a statement
1353
- (``Can't apply analyzer definition: …``, ``Can't apply index definition: …``, ``Can't apply index
1354
- rebuild: …``).
1361
+ not exist (one removed in the block included), or *tx* is given and an analyzer must be defined or
1362
+ redefined (likewise), or an index must be defined or redefined with an analyzer
1363
+ ``define_analyzer(tx=…)`` redefined in the block, or after the transaction wrote records to the
1364
+ model's table (one removed in the block included; the message names the model and the table) — before
1365
+ any DDL; the server refused a statement (``Can't apply analyzer definition: …``, ``Can't apply index
1366
+ definition: …``, ``Can't apply index rebuild: …``).
1355
1367
  SurrealDbAuthenticationError: the session may not read the definitions or define the analyzers and
1356
1368
  indexes.
1357
1369
 
@@ -1382,6 +1394,9 @@ class BaseSurrealModel(BaseModel):
1382
1394
  # Cheap even inside a transaction: get_client() returns the already-connected client.
1383
1395
  client = await cls._connection_manager().get_client()
1384
1396
  stored_analyzers, tables = await read_database_info(client, what="index", kind="definition", subject=f"table {table!r}")
1397
+ # Issue #323: an analyzer remove_analyzer(tx=…) removed in this transaction still stands in committed state; it is
1398
+ # missing once the transaction commits, so an index using it is refused below — never defined without it.
1399
+ stored_analyzers = analyzers_after_removals(stored_analyzers, tx)
1385
1400
  analyzers = declared_analyzers(indexes)
1386
1401
  check_named_analyzers(indexes, analyzers, stored_analyzers, table=table)
1387
1402
  define, redefine = plan_analyzers(analyzers, stored_analyzers, overwrite)
@@ -1407,6 +1422,10 @@ class BaseSurrealModel(BaseModel):
1407
1422
  keyword=keyword,
1408
1423
  redefined=redefined,
1409
1424
  )
1425
+ analyzer_of = {index.name: index.analyzer_name or "" for index in indexes}
1426
+ # Issue #323: the servers build an index with the analyzers committed before the transaction began, not with
1427
+ # one define_analyzer(tx=…) redefined in it.
1428
+ check_analyzers_redefined_in(tx, cls.__name__, table, definitions, analyzer_of)
1410
1429
  # Issue #304: SurrealDB servers do not index the records a transaction wrote before the index was defined.
1411
1430
  # Under tx the analyzers are in place (refused above otherwise), so the definitions are all that would join it.
1412
1431
  if tx is not None and definitions:
@@ -1415,7 +1434,6 @@ class BaseSurrealModel(BaseModel):
1415
1434
  raise SurrealDbError(transaction_write_refused(cls.__name__, table))
1416
1435
  # A redefined analyzer leaves every index using it with its old tokens: the table's declared indexes (above),
1417
1436
  # its other indexes (another model's, or defined by hand), and every other table's — one INFO FOR TABLE each.
1418
- analyzer_of = {index.name: index.analyzer_name or "" for index in indexes}
1419
1437
  # Issue #304: an index this call redefines is rebuilt by its DEFINE INDEX OVERWRITE; the warning names it too.
1420
1438
  redefined_indexes = [(table, name, analyzer_of[name]) for name, _ in definitions if analyzer_of[name] in redefined]
1421
1439
  own_rebuilds = [(table, name, analyzer_of[name]) for name, _ in rebuilds]
@@ -1455,14 +1473,17 @@ class BaseSurrealModel(BaseModel):
1455
1473
  index as missing, although committed state still holds it, and defines it again after the removal when its
1456
1474
  model declares it — so removing an index and defining it again in one block keeps it. On SurrealDB 2.x, on a
1457
1475
  table that held records, such a block fails the commit with a retryable ``SurrealDbConflictError`` that a retry
1458
- meets again, and nothing of it is committed. A raw ``tx.add("REMOVE INDEX …")`` is not noted.
1476
+ meets again, and nothing of it is committed. ``SurrealDBConnectionManager.remove_analyzer(tx=…)`` and
1477
+ ``define_analyzer(tx=…)`` read the note too: the removed index no longer counts as one using the analyzer (issue
1478
+ #323); once the block has removed or redefined that analyzer, :meth:`define_indexes` refuses to define the index
1479
+ again in it. A raw ``tx.add("REMOVE INDEX …")`` is not noted.
1459
1480
 
1460
1481
  Args:
1461
1482
  name: the index's name.
1462
1483
  missing_ok: ``True`` (default) emits ``REMOVE INDEX IF EXISTS …``, so a missing index is not an error;
1463
1484
  ``False`` raises ``SurrealDbNotFoundError`` for it.
1464
1485
  tx: optional transaction the statement is applied with; it notes the removal for
1465
- :meth:`define_indexes`.
1486
+ :meth:`define_indexes`, ``remove_analyzer()`` and ``define_analyzer()``.
1466
1487
 
1467
1488
  Raises:
1468
1489
  TypeError, ValueError: *name* is not an identifier, or *missing_ok* not a ``bool`` — before any request.
@@ -59,7 +59,11 @@ already read), ``apply_analyzers()`` and ``apply_rebuilds()`` apply the statemen
59
59
  warning per redefined analyzer. ``apply_analyzer()`` and ``apply_analyzer_removal()`` are the bodies of the connection
60
60
  manager's ``define_analyzer()`` and ``remove_analyzer()``; the latter refuses, below SurrealDB 3 (2.x, the embedded
61
61
  engine, which would remove it and then refuse every write to the table), an analyzer an index uses, with 3.x's own
62
- reason.
62
+ reason. With a transaction, both leave out the indexes ``remove_index(tx=…)`` removed earlier in it, which committed
63
+ state still holds (``indexes_removed_in()``, the transaction's note — issue #323), and note the analyzer they removed
64
+ or defined (``note_analyzer_change()``): ``define_analyzer(tx=…)`` and ``define_indexes(tx=…)`` count one the block
65
+ removed as missing (``analyzers_after_removals()``), and ``define_indexes(tx=…)`` refuses an index whose analyzer the
66
+ block redefined (``transaction_redefined_analyzer_refused()``).
63
67
 
64
68
  **The query side.** ``QuerySet.search()`` checks its fields and texts with ``checked_search_field()`` and words its
65
69
  refusals with ``text_refused()``, ``field_refused()``, ``too_many_fields()``, ``time_travel_refused()`` (with
@@ -73,7 +77,8 @@ annotation whose match reference ``search()`` did not give it is refused with ``
73
77
  ``search()`` with ``no_search_refused()``.
74
78
 
75
79
  The module sits low in the package: it imports neither ``model_base``, ``query_set`` nor ``connection_manager``, so
76
- each of them can import it.
80
+ each of them can import it; it imports ``transaction``, which imports none of them, for its note of the removed
81
+ indexes.
77
82
  """
78
83
 
79
84
  import logging
@@ -92,6 +97,7 @@ from .schema import _comment_text, _identifier_name, _missing_clause, _overwrite
92
97
  from .schema_state import AnalyzerState, IndexState
93
98
  from .surql_literal import _identifier, _quoted
94
99
  from .time_travel import line_major
100
+ from .transaction import Transaction
95
101
  from .utils import VALID_FIELD_PATTERN, _ddl_refusal_class, apply_ddl_statements, validate_field_name
96
102
  from .vector import (
97
103
  DEFAULTS,
@@ -1148,25 +1154,61 @@ def stored_indexes_using(
1148
1154
 
1149
1155
 
1150
1156
  async def indexes_using(
1151
- client: Any, tables: Iterable[str], analyzers: Collection[str], *, what: str, kind: str
1157
+ client: Any,
1158
+ tables: Iterable[str],
1159
+ analyzers: Collection[str],
1160
+ *,
1161
+ what: str,
1162
+ kind: str,
1163
+ skip: Callable[[str], Collection[str]] | None = None,
1152
1164
  ) -> list[IndexUse]:
1153
1165
  """The indexes of *tables* whose analyzer is among *analyzers*, as ``(table, index, analyzer)``, sorted by table
1154
1166
  then index — one ``INFO FOR TABLE`` per table, in the order of *tables*, outside any transaction (committed state).
1155
1167
 
1156
1168
  *tables* are names ``INFO FOR DB`` gave (:func:`read_database_info`), each written in backticks, escaped. An index
1157
- the parser cannot read is left out (its analyzer is unknown).
1169
+ the parser cannot read is left out (its analyzer is unknown), and so is one *skip* names for its table: *skip*
1170
+ takes a table's name, as ``INFO FOR DB`` gives it, and returns the names of its indexes to leave out —
1171
+ :func:`indexes_removed_in`'s, the indexes ``remove_index(tx=…)`` removed earlier in a transaction, which committed
1172
+ state still holds (issue #323). Every table is read all the same.
1158
1173
 
1159
1174
  :param what: what the reads are for, for the message of a refusal (``"index"``, ``"analyzer"``).
1160
1175
  :param kind: ``"definition"``, ``"removal"`` or ``"rebuild"``, likewise.
1176
+ :param skip: the indexes to leave out, by table; ``None`` (default) leaves none out.
1161
1177
  :raises SurrealDbAuthenticationError: the session may not read a table's definitions (a record user).
1162
1178
  :raises SurrealDbError: any other refusal of an ``INFO``.
1163
1179
  """
1164
1180
  found: list[IndexUse] = []
1165
1181
  for table in tables:
1166
- found.extend(stored_indexes_using(table, await _table_indexes(client, table, what=what, kind=kind), analyzers))
1182
+ stored = await _table_indexes(client, table, what=what, kind=kind)
1183
+ found.extend(stored_indexes_using(table, stored, analyzers, skip=skip(table) if skip is not None else ()))
1167
1184
  return sorted(found)
1168
1185
 
1169
1186
 
1187
+ def indexes_removed_in(tx: object) -> Callable[[str], frozenset[str]] | None:
1188
+ """The indexes ``remove_index(tx=…)`` removed in *tx* and not defined again since, by table — *tx*'s
1189
+ ``Transaction._indexes_removed`` — for :func:`indexes_using`'s *skip*; ``None`` when *tx* is not a ``Transaction``
1190
+ (no transaction given). A raw ``tx.add("REMOVE INDEX …")`` is not noted (issue #323)."""
1191
+ return tx._indexes_removed if isinstance(tx, Transaction) else None
1192
+
1193
+
1194
+ def analyzers_after_removals(stored: Mapping[str, AnalyzerState | None], tx: object) -> dict[str, AnalyzerState | None]:
1195
+ """The analyzers *stored* holds (``INFO FOR DB``'s, committed state) without those ``remove_analyzer(tx=…)``
1196
+ removed earlier in *tx* and did not define again since — *tx*'s ``Transaction._analyzers_removed`` (issue #323):
1197
+ the analyzers in place once the transaction commits. *stored* as it is when *tx* is not a ``Transaction``. Pure."""
1198
+ removed = tx._analyzers_removed() if isinstance(tx, Transaction) else frozenset()
1199
+ return {name: state for name, state in stored.items() if name not in removed}
1200
+
1201
+
1202
+ def note_analyzer_change(tx: object, name: str, *, removed: bool) -> None:
1203
+ """Note on *tx* that the analyzer *name* was removed (*removed*) or defined in it — once the statement is applied
1204
+ or queued (issue #323); nothing when *tx* is not a ``Transaction``."""
1205
+ if isinstance(tx, Transaction):
1206
+ if removed:
1207
+ tx._note_analyzer_removal(name)
1208
+ else:
1209
+ tx._note_analyzer_definition(name)
1210
+
1211
+
1170
1212
  # --- the appliers' plans --------------------------------------------------------------------------------------------
1171
1213
 
1172
1214
 
@@ -1236,6 +1278,37 @@ def transaction_analyzer_refused(model: str, name: str) -> str:
1236
1278
  )
1237
1279
 
1238
1280
 
1281
+ def transaction_redefined_analyzer_refused(model: str, index: str, table: str, analyzer: str) -> str:
1282
+ """The refusal of ``define_indexes(tx=…)`` when the index *index* of *table* uses the analyzer *analyzer*, which
1283
+ ``define_analyzer(tx=…)`` redefined earlier in the transaction (issue #323): the servers build an index with the
1284
+ analyzers committed before the transaction began."""
1285
+ return (
1286
+ f"{model}.define_indexes(tx=…) cannot define index {index!r} on table {table!r} in a transaction that has "
1287
+ f"redefined its analyzer {analyzer!r}: SurrealDB builds an index with the analyzers committed before the "
1288
+ "transaction began, so the index would not follow the new definition. Call "
1289
+ "SurrealDBConnectionManager.define_analyzer() without tx= first, then define_indexes(tx=…)."
1290
+ )
1291
+
1292
+
1293
+ def check_analyzers_redefined_in(
1294
+ tx: object, model: str, table: str, definitions: Iterable[tuple[str, str]], analyzer_of: Mapping[str, str]
1295
+ ) -> None:
1296
+ """Refuse, before any DDL, the first of *definitions* — ``(index name, statement)``, what ``define_indexes(tx=…)``
1297
+ of the model *model* would define on *table* — whose analyzer (*analyzer_of*, by index name, ``""`` for none)
1298
+ ``define_analyzer(tx=…)`` defined or redefined earlier in *tx* (``Transaction._analyzers_defined``, issue #323):
1299
+ the servers build an index with the analyzers committed before the transaction began. Nothing to refuse when *tx*
1300
+ is not a ``Transaction``.
1301
+
1302
+ :raises SurrealDbError: :func:`transaction_redefined_analyzer_refused`'s text.
1303
+ """
1304
+ if not isinstance(tx, Transaction):
1305
+ return
1306
+ defined = tx._analyzers_defined()
1307
+ for name, _ in definitions:
1308
+ if analyzer_of[name] in defined:
1309
+ raise SurrealDbError(transaction_redefined_analyzer_refused(model, name, table, analyzer_of[name]))
1310
+
1311
+
1239
1312
  def transaction_write_refused(model: str, table: str) -> str:
1240
1313
  """The refusal of ``define_indexes(tx=…)`` when the transaction has already written records to *table*, the
1241
1314
  model's (issue #304): SurrealDB servers leave those records out of the index."""
@@ -1372,20 +1445,29 @@ async def apply_analyzer(client: Any, analyzer: Analyzer, *, overwrite: bool, tx
1372
1445
  (:func:`indexes_using`), then ``DEFINE ANALYZER OVERWRITE`` and ``REBUILD INDEX`` of each index using it — SurrealDB
1373
1446
  leaves the records indexed before with their old tokens — and the warning of :func:`warn_redefined`; nothing
1374
1447
  without *overwrite*. With *tx*, a redefinition with indexes to rebuild is refused before any DDL (the rebuild must
1375
- follow the committed definition); a new analyzer, or a redefinition no index uses, joins the transaction.
1448
+ follow the committed definition); a new analyzer, or a redefinition no index uses, joins the transaction. An index
1449
+ ``remove_index(tx=…)`` removed earlier in *tx* is not to rebuild, although committed state still holds it
1450
+ (:func:`indexes_removed_in`, issue #323): a redefinition whose indexes the block all removed joins the transaction,
1451
+ without ``REBUILD INDEX``. An analyzer ``remove_analyzer(tx=…)`` removed earlier in *tx* counts as missing
1452
+ (:func:`analyzers_after_removals`): defined again after its removal, with no index read nor rebuilt. *tx* notes the
1453
+ analyzer once defined or redefined (:func:`note_analyzer_change`), for ``define_indexes(tx=…)``.
1376
1454
 
1377
1455
  :raises SurrealDbError: *tx* is given and the redefinition has indexes to rebuild; a refused read or statement.
1378
1456
  :raises SurrealDbAuthenticationError: the session may not read the definitions or define the analyzer.
1379
1457
  """
1380
1458
  subject = repr(analyzer.name)
1381
1459
  stored, tables = await read_database_info(client, what="analyzer", kind="definition", subject=subject)
1382
- define, redefine = plan_analyzers([analyzer], stored, overwrite)
1460
+ define, redefine = plan_analyzers([analyzer], analyzers_after_removals(stored, tx), overwrite)
1383
1461
  rebuilds: list[IndexUse] = []
1384
1462
  if redefine:
1385
- rebuilds = await indexes_using(client, tables, {analyzer.name}, what="analyzer", kind="definition")
1463
+ rebuilds = await indexes_using(
1464
+ client, tables, {analyzer.name}, what="analyzer", kind="definition", skip=indexes_removed_in(tx)
1465
+ )
1386
1466
  if tx is not None and rebuilds:
1387
1467
  raise SurrealDbError(analyzer_redefinition_refused(analyzer.name, rebuilds))
1388
1468
  applied = await apply_analyzers(client, tx, define, redefine, overwrite)
1469
+ if applied:
1470
+ note_analyzer_change(tx, analyzer.name, removed=False)
1389
1471
  applied += await apply_rebuilds(client, tx, rebuilds)
1390
1472
  warn_redefined(redefine, rebuilds)
1391
1473
  return applied
@@ -1398,8 +1480,12 @@ async def apply_analyzer_removal(client: Any, name: str, statement: str, *, tx:
1398
1480
  ``client.version()`` once. Below 3 (SurrealDB 2.x, the embedded engine), which accept the removal of an analyzer an
1399
1481
  index uses and then refuse every write to that table: ``INFO FOR DB``, then ``INFO FOR TABLE`` of every table
1400
1482
  (:func:`indexes_using`, committed state), and the first index using it, in table then index order, refused with
1401
- SurrealDB 3.x's own reason, before any DDL. From 3 on, the server refuses it itself, in the same words. Then the
1402
- statement, joining *tx* when given: ``Can't apply analyzer removal: '<name>' -> <reason>`` for a refusal.
1483
+ SurrealDB 3.x's own reason, before any DDL. An index ``remove_index(tx=…)`` removed earlier in *tx* is left out,
1484
+ although committed state still holds it (:func:`indexes_removed_in`, issue #323); an index queued earlier in a
1485
+ buffered *tx* is not seen. From 3 on, the server refuses it itself, in the same words, in the transaction. Then the
1486
+ statement, joining *tx* when given: ``Can't apply analyzer removal: '<name>' -> <reason>`` for a refusal. *tx*
1487
+ notes the removal once applied or queued (:func:`note_analyzer_change`): ``define_analyzer(tx=…)`` and
1488
+ ``define_indexes(tx=…)`` then count the analyzer as missing (issue #323).
1403
1489
 
1404
1490
  :raises SurrealDbError: an index uses the analyzer; a refused read or statement.
1405
1491
  :raises SurrealDbNotFoundError: ``REMOVE ANALYZER`` without ``IF EXISTS`` of a missing analyzer (outside a buffered
@@ -1408,13 +1494,14 @@ async def apply_analyzer_removal(client: Any, name: str, statement: str, *, tx:
1408
1494
  """
1409
1495
  if line_major(await client.version()) < 3:
1410
1496
  _, tables = await read_database_info(client, what="analyzer", kind="removal", subject=repr(name))
1411
- using = await indexes_using(client, tables, {name}, what="analyzer", kind="removal")
1497
+ using = await indexes_using(client, tables, {name}, what="analyzer", kind="removal", skip=indexes_removed_in(tx))
1412
1498
  if using:
1413
1499
  table, index, _ = using[0]
1414
1500
  raise SurrealDbError(analyzer_in_use_refused(name, index, table))
1415
1501
  await apply_ddl_statements(
1416
1502
  [statement], client=client, tx=tx, what="analyzer", kind="removal", classify=True, subjects=[repr(name)]
1417
1503
  )
1504
+ note_analyzer_change(tx, name, removed=True)
1418
1505
  return statement
1419
1506
 
1420
1507
 
@@ -32,8 +32,16 @@ records a transaction wrote before the index was defined. A raw ``tx.add()`` is
32
32
 
33
33
  Indexes removed (issue #312): ``remove_index(tx=…)`` notes the index it removes, by table (``_note_index_removal``).
34
34
  ``define_indexes(tx=…)`` reads committed state, where that index still stands: it counts the indexes noted for its table
35
- (``_indexes_removed``) as missing, plans them again, and forgets those it defines (``_forget_index_removals``). The
36
- commit and the cancel keep the note, like the tables written. A raw ``tx.add("REMOVE INDEX …")`` is not noted.
35
+ (``_indexes_removed``) as missing, plans them again, and forgets those it defines (``_forget_index_removals``).
36
+ ``remove_analyzer(tx=…)`` and ``define_analyzer(tx=…)`` read committed state too, and leave the noted indexes out of
37
+ those that use the analyzer (issue #323). The commit and the cancel keep the note, like the tables written. A raw
38
+ ``tx.add("REMOVE INDEX …")`` is not noted.
39
+
40
+ Analyzers changed (issue #323): ``remove_analyzer(tx=…)`` notes the analyzer it removes (``_note_analyzer_removal``),
41
+ ``define_analyzer(tx=…)`` the one it defines or redefines (``_note_analyzer_definition``); the last change of a name
42
+ wins. ``define_analyzer(tx=…)`` and ``define_indexes(tx=…)`` count a removed one as missing (``_analyzers_removed``),
43
+ and ``define_indexes(tx=…)`` refuses an index whose analyzer the block redefined (``_analyzers_defined``): SurrealDB
44
+ builds an index with the analyzers committed before the transaction began. The commit and the cancel keep the note.
37
45
 
38
46
  Errors (issue #278): the errors a failed transaction raises give the server's reason and the failed statement's
39
47
  position, without the server's excerpt of the statement; the server's own text stays reachable as their suppressed
@@ -240,6 +248,10 @@ class Transaction(ABC):
240
248
  # Issue #312: the indexes remove_index(tx=…) removed in this transaction, by table, until define_indexes(tx=…)
241
249
  # defines them again; kept until the transaction ends.
242
250
  self._removed_indexes: dict[str, set[str]] = {}
251
+ # Issue #323: the analyzers remove_analyzer(tx=…) removed and those define_analyzer(tx=…) defined or redefined
252
+ # in this transaction — the last change of a name wins; kept until the transaction ends.
253
+ self._removed_analyzers: set[str] = set()
254
+ self._defined_analyzers: set[str] = set()
243
255
  # Issue #268: the errors of the statements that failed in this transaction (interactive strategy).
244
256
  self._failures: list[BaseException] = []
245
257
 
@@ -304,8 +316,10 @@ class Transaction(ABC):
304
316
  """Note that the index *name* of *table* was removed in this transaction — issue #312.
305
317
 
306
318
  ``remove_index(tx=…)`` calls it once its ``REMOVE INDEX`` was applied (interactive) or queued (buffered).
307
- ``define_indexes(tx=…)``, which reads committed state where the index still stands, counts it as missing. Like
308
- the tables written, the commit and the cancel keep it. A raw ``tx.add()`` is not noted.
319
+ ``define_indexes(tx=…)``, which reads committed state where the index still stands, counts it as missing;
320
+ ``remove_analyzer(tx=…)`` and ``define_analyzer(tx=…)`` no longer count it among the indexes that use an
321
+ analyzer (issue #323). Like the tables written, the commit and the cancel keep it. A raw ``tx.add()`` is not
322
+ noted.
309
323
  """
310
324
  self._removed_indexes.setdefault(table, set()).add(name)
311
325
 
@@ -321,6 +335,35 @@ class Transaction(ABC):
321
335
  if removed is not None:
322
336
  removed.difference_update(names)
323
337
 
338
+ def _note_analyzer_removal(self, name: str) -> None:
339
+ """Note that the analyzer *name* was removed in this transaction — issue #323.
340
+
341
+ ``remove_analyzer(tx=…)`` calls it once its ``REMOVE ANALYZER`` was applied (interactive) or queued (buffered).
342
+ ``define_analyzer(tx=…)`` and ``define_indexes(tx=…)``, which read committed state where the analyzer still
343
+ stands, count it as missing. A definition noted before for the same name is forgotten. Like the indexes
344
+ removed, the commit and the cancel keep it. A raw ``tx.add()`` is not noted.
345
+ """
346
+ self._defined_analyzers.discard(name)
347
+ self._removed_analyzers.add(name)
348
+
349
+ def _note_analyzer_definition(self, name: str) -> None:
350
+ """Note that the analyzer *name* was defined or redefined in this transaction — issue #323.
351
+
352
+ ``define_analyzer(tx=…)`` calls it once its ``DEFINE ANALYZER`` was applied or queued. ``define_indexes(tx=…)``
353
+ refuses to define an index that uses it: SurrealDB builds an index with the analyzers committed before the
354
+ transaction began. A removal noted before for the same name is forgotten. The commit and the cancel keep it.
355
+ """
356
+ self._removed_analyzers.discard(name)
357
+ self._defined_analyzers.add(name)
358
+
359
+ def _analyzers_removed(self) -> frozenset[str]:
360
+ """The analyzers removed in this transaction and not defined again since (issue #323)."""
361
+ return frozenset(self._removed_analyzers)
362
+
363
+ def _analyzers_defined(self) -> frozenset[str]:
364
+ """The analyzers defined or redefined in this transaction and not removed since (issue #323)."""
365
+ return frozenset(self._defined_analyzers)
366
+
324
367
  @property
325
368
  @abstractmethod
326
369
  def is_interactive(self) -> bool: