storepy 5.3.2__tar.gz → 5.3.3__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.
- {storepy-5.3.2/src/storepy.egg-info → storepy-5.3.3}/PKG-INFO +2 -2
- {storepy-5.3.2 → storepy-5.3.3}/pyproject.toml +2 -2
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/__init__.py +9 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/__init__.py +11 -1
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/mutation.py +3 -2
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/write.py +13 -1
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/datasource.py +14 -2
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/ddl.py +81 -4
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/metadef.py +90 -2
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/migrate.py +82 -11
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/schema.py +33 -3
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/workflow.py +24 -11
- {storepy-5.3.2 → storepy-5.3.3/src/storepy.egg-info}/PKG-INFO +2 -2
- {storepy-5.3.2 → storepy-5.3.3}/src/storepy.egg-info/requires.txt +1 -1
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_host_contract.py +55 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_metadef.py +32 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_migrate.py +136 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_raw_sql_and_ddl.py +1 -1
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_session.py +83 -1
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_workflow.py +42 -2
- {storepy-5.3.2 → storepy-5.3.3}/LICENSE +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/README.md +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/setup.cfg +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/adapters/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/adapters/http.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/ask.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/ask_knowledge.md +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/bootstrap.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/cache/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/cache/context.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/cache/provider.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/cache/redis_io.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/capabilities.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/core.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/claim.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/exec.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/id.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/query.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/crud/triggers.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/degraded.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/executors/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/executors/_values.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/executors/mongo.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/executors/mysql.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/executors/postgres.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/executors/sqlite.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/feedback.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/introspect/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/introspect/mysql.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/introspect/postgres.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/introspect/sqlite.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/llm.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/load.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/local/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/local/handle.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/local/store.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/memory/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/naming.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/permission.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/reindex.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/resource/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/resource/providers/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/resource/providers/local.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/resource/providers/s3.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/scheduler/__init__.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/scope.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/secure.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/sync.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/vector_index.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/py_store/vector_scan.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/storepy.egg-info/SOURCES.txt +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/storepy.egg-info/dependency_links.txt +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/src/storepy.egg-info/top_level.txt +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_access_keys.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_adapters.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_ask.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_cache.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_cache_link.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_claim_concurrency.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_compute_override.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_coverage_margin.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_federation_e2e.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_feedback_flush.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_field_name_type.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_fns_binding.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_host_paths.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_inline_agg.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_load.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_local_store.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_memory.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_migration.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_mongo_executor.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_mongo_session.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_multi_datasource.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_parity_deny.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_parity_local.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_parity_local_memory.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_parity_scope.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_policy_isolation.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_profile.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_py_store.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_rbac_host.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_real_backends_e2e.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_require_context.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_resource.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_resource_e2e.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_resource_pools.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_resource_units.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_scenario_course_platform.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_scenario_manager_transaction.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_scheduler.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_sql_commit_and_registry.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_triggers.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_vector_conformance.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_vector_index.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_vector_scan.py +0 -0
- {storepy-5.3.2 → storepy-5.3.3}/tests/test_vector_units.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: storepy
|
|
3
|
-
Version: 5.3.
|
|
3
|
+
Version: 5.3.3
|
|
4
4
|
Summary: Multi-backend data layer for Python asyncio (MongoDB, MySQL, SQLite, PostgreSQL, local disk): pure JSON schemas, GQL tree queries compiled to a single native query, GROUP BY/HAVING aggregation, computed columns, soft-delete and role-based access control
|
|
5
5
|
Author-email: 丁滔 <coen_ddt@qq.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -24,7 +24,7 @@ Requires-Python: >=3.10
|
|
|
24
24
|
Description-Content-Type: text/markdown
|
|
25
25
|
License-File: LICENSE
|
|
26
26
|
Requires-Dist: pymongo>=4.9
|
|
27
|
-
Requires-Dist: rust-store-py<6.0.0,>=5.3.
|
|
27
|
+
Requires-Dist: rust-store-py<6.0.0,>=5.3.3
|
|
28
28
|
Provides-Extra: mysql
|
|
29
29
|
Requires-Dist: asyncmy>=0.2.9; extra == "mysql"
|
|
30
30
|
Provides-Extra: postgres
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "storepy"
|
|
7
|
-
version = "5.3.
|
|
7
|
+
version = "5.3.3"
|
|
8
8
|
description = "Multi-backend data layer for Python asyncio (MongoDB, MySQL, SQLite, PostgreSQL, local disk): pure JSON schemas, GQL tree queries compiled to a single native query, GROUP BY/HAVING aggregation, computed columns, soft-delete and role-based access control"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -26,7 +26,7 @@ authors = [
|
|
|
26
26
|
]
|
|
27
27
|
dependencies = [
|
|
28
28
|
"pymongo>=4.9",
|
|
29
|
-
"rust-store-py>=5.3.
|
|
29
|
+
"rust-store-py>=5.3.3,<6.0.0",
|
|
30
30
|
]
|
|
31
31
|
classifiers = [
|
|
32
32
|
"Development Status :: 4 - Beta",
|
|
@@ -147,6 +147,11 @@ class Store:
|
|
|
147
147
|
route_override: dict | None = None) -> int:
|
|
148
148
|
return await crud.count(schema_name, filter, route_override)
|
|
149
149
|
|
|
150
|
+
async def apply_write_computes(self, schema_name: str,
|
|
151
|
+
doc: dict[str, Any]) -> dict[str, Any]:
|
|
152
|
+
"""写后仅叠同步 ``fn`` 计算列(入参须为 ``returnDocument: AFTER`` 落库文档;不补默认值)"""
|
|
153
|
+
return await crud.apply_write_computes(schema_name, doc)
|
|
154
|
+
|
|
150
155
|
async def mutation(self, schema_name: str, data: dict | list[dict],
|
|
151
156
|
route_override: dict | None = None) -> Any:
|
|
152
157
|
return await crud.mutation(schema_name, data, route_override)
|
|
@@ -443,6 +448,10 @@ class Store:
|
|
|
443
448
|
# ── 其余 API 显式绑定(staticmethod:避免实例化后 self 注入)──
|
|
444
449
|
# Schema 管理
|
|
445
450
|
register = staticmethod(schema.register)
|
|
451
|
+
# 批量注册门面(插件 bootstrap 接入):items = [{'defn', 'location'?}],原子语义由 core 定
|
|
452
|
+
register_batch = staticmethod(schema.register_batch)
|
|
453
|
+
# 注销门面(补偿整份 attempted 清单;未知 schema 幂等成功)
|
|
454
|
+
unregister = staticmethod(schema.unregister)
|
|
446
455
|
# 运行期目录装载(读 store.config.json → 收集定义 → core 纯规划 → 带定位批量注册)
|
|
447
456
|
load_defs = staticmethod(load.load_defs)
|
|
448
457
|
# 公开回调注入与启动期缺实现校验(对齐 nodejs-store store.set_fn / assert_fns_covered)
|
|
@@ -11,11 +11,21 @@ from .claim import claim, compare_and_set, renew_lease
|
|
|
11
11
|
from .exec import ProfileViolation, _get_db, set_connections, set_db
|
|
12
12
|
from .mutation import mutation, upsert
|
|
13
13
|
from .query import query, query_federated, query_one, query_with_count
|
|
14
|
-
from .write import
|
|
14
|
+
from .write import (
|
|
15
|
+
apply_write_computes,
|
|
16
|
+
count,
|
|
17
|
+
exists,
|
|
18
|
+
insert,
|
|
19
|
+
insert_many,
|
|
20
|
+
remove,
|
|
21
|
+
update,
|
|
22
|
+
update_many,
|
|
23
|
+
)
|
|
15
24
|
|
|
16
25
|
__all__ = [
|
|
17
26
|
'ProfileViolation',
|
|
18
27
|
'_get_db',
|
|
28
|
+
'apply_write_computes',
|
|
19
29
|
'claim',
|
|
20
30
|
'compare_and_set',
|
|
21
31
|
'count',
|
|
@@ -13,6 +13,7 @@ from .exec import (
|
|
|
13
13
|
run_atomic,
|
|
14
14
|
)
|
|
15
15
|
from .id import _generate_id, _new_id_pool
|
|
16
|
+
from .write import apply_write_computes
|
|
16
17
|
|
|
17
18
|
|
|
18
19
|
async def _mutation_one(schema_name, data, now, route_override=None):
|
|
@@ -36,7 +37,7 @@ async def _mutation_one(schema_name, data, now, route_override=None):
|
|
|
36
37
|
|
|
37
38
|
if not root_result:
|
|
38
39
|
return None
|
|
39
|
-
return
|
|
40
|
+
return await apply_write_computes(schema_name, root_result)
|
|
40
41
|
|
|
41
42
|
# 单一 SQL 源 → 步骤序列整体事务化(同连接同事务,任一步失败整体回滚);
|
|
42
43
|
# Mongo 源 / 跨源步骤按原样顺序执行(非原子边界见 README「事务边界」)
|
|
@@ -79,4 +80,4 @@ async def upsert(schema_name, condition, data, options=None, route_override=None
|
|
|
79
80
|
schema_name, condition, data, options, _now_for(schema_name),
|
|
80
81
|
_generate_id(s) if s['idPrefix'] else '', _ctx(), route_override))
|
|
81
82
|
result = await _exec(plan['command'])
|
|
82
|
-
return
|
|
83
|
+
return await apply_write_computes(schema_name, result) if result else None
|
|
@@ -114,11 +114,23 @@ async def update(schema_name, condition, data, options=None, route_override=None
|
|
|
114
114
|
if out.get('triggers') and result:
|
|
115
115
|
await run_triggers(out['triggers'], root=result, before=before, now=now, ctx=ctx,
|
|
116
116
|
executed=set())
|
|
117
|
-
return
|
|
117
|
+
return await apply_write_computes(schema_name, result) if result else None
|
|
118
118
|
|
|
119
119
|
return await run_atomic(sources, _do)
|
|
120
120
|
|
|
121
121
|
|
|
122
|
+
async def apply_write_computes(schema_name, doc):
|
|
123
|
+
"""写路径结果回喂:只叠 **同步计算列**(``fn``),**不补任何默认值**。
|
|
124
|
+
|
|
125
|
+
入参必须是 ``returnDocument: AFTER`` 的落库文档。默认值只在 insert 物化一次,
|
|
126
|
+
重复填充会让「落库 / returns / 读回」出现不同取值(D1)。无 ``fn`` 声明原样返回;
|
|
127
|
+
声明 ``fn: true`` 却未注册实现,由 core 显式报错(不静默跳过)。
|
|
128
|
+
"""
|
|
129
|
+
if not doc:
|
|
130
|
+
return doc
|
|
131
|
+
return _call(lambda: get_core().apply_write_computes(schema_name, doc))
|
|
132
|
+
|
|
133
|
+
|
|
122
134
|
async def _exec_with_pre(command):
|
|
123
135
|
"""执行带 ``preCommand`` 的命令(阶段1:mutation 关系谓词归一)。
|
|
124
136
|
|
|
@@ -57,7 +57,7 @@ _CLAIMED_IDS = '___claimed_ids___'
|
|
|
57
57
|
|
|
58
58
|
def _render_claimed_ids(ids) -> str:
|
|
59
59
|
"""_id 列表 → SQL IN 字面量(单引号转义;_id 为框架生成值,转义为防御)"""
|
|
60
|
-
return '
|
|
60
|
+
return ','.join("'" + str(v).replace("'", "''") + "'" for v in ids)
|
|
61
61
|
|
|
62
62
|
|
|
63
63
|
_MISSING = object()
|
|
@@ -633,6 +633,18 @@ class NativeCommandError(RuntimeError):
|
|
|
633
633
|
"""原生 Mongo 命令入口的显式错误(非 Mongo 源 / 非 Mongo 形态)"""
|
|
634
634
|
|
|
635
635
|
|
|
636
|
+
def pipeline_writes(pipeline):
|
|
637
|
+
"""聚合管道是否为写管道:任一阶段含 ``$merge`` / ``$out``(Mongo 的写出口)。
|
|
638
|
+
|
|
639
|
+
写管道必须按写路径解析连接(主库 / 事务连接)——按读路径解析会在主从分离或
|
|
640
|
+
只读连接上报错或落到错误节点(A3)。
|
|
641
|
+
"""
|
|
642
|
+
for stage in pipeline or []:
|
|
643
|
+
if isinstance(stage, Mapping) and ('$merge' in stage or '$out' in stage):
|
|
644
|
+
return True
|
|
645
|
+
return False
|
|
646
|
+
|
|
647
|
+
|
|
636
648
|
async def execute_native(source, collection, pipeline, options=None):
|
|
637
649
|
"""
|
|
638
650
|
在指定 Mongo 源上执行原生聚合管道(Host 层逃生口,对标 SQL 侧 ``execute_raw``)
|
|
@@ -649,7 +661,7 @@ async def execute_native(source, collection, pipeline, options=None):
|
|
|
649
661
|
MongoClient 形态须经 schema 声明 database(``mongo_db`` 既有校验,缺名即报错);
|
|
650
662
|
- 返回 ``{'rows': list}``。
|
|
651
663
|
"""
|
|
652
|
-
conn = await resolve_connection(source, is_write=
|
|
664
|
+
conn = await resolve_connection(source, is_write=pipeline_writes(pipeline))
|
|
653
665
|
if is_embedded(conn):
|
|
654
666
|
# 嵌入式载体(local / memory;描述符或事务视图):db = handle;session 已绑定在 handle 上,不另传
|
|
655
667
|
db = conn['handle']
|
|
@@ -216,6 +216,45 @@ def _owner_field(defn):
|
|
|
216
216
|
return None if name == '_id' else name
|
|
217
217
|
|
|
218
218
|
|
|
219
|
+
def implicit_columns(defn, backend):
|
|
220
|
+
"""宿主自动补齐的内建列清单(**DDL 生成与迁移补列共用的唯一来源**)。
|
|
221
|
+
|
|
222
|
+
返回 ``[(逻辑名, sql_type)]``,顺序:timestamps → 归属键(owner 行控隐含)。
|
|
223
|
+
``__present`` 与声明字段不在此列(分别由 ``_columns`` / 调用方处理)。
|
|
224
|
+
timestamps 判定与 DDL 同口径:**未声明即建**(``is not False``)。
|
|
225
|
+
"""
|
|
226
|
+
i = _idx(backend)
|
|
227
|
+
out = []
|
|
228
|
+
if defn.get('timestamps') is not False:
|
|
229
|
+
for ts in _TIMESTAMP_FIELDS:
|
|
230
|
+
out.append((ts, _TYPES['number'][i]))
|
|
231
|
+
owner = _owner_field(defn)
|
|
232
|
+
if owner is not None:
|
|
233
|
+
out.append((owner, _TYPES['string'][i]))
|
|
234
|
+
return out
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def builtin_field_names(defn, backend):
|
|
238
|
+
"""内建物理列名清单(迁移剔除用;与 ``implicit_columns`` 同源)。
|
|
239
|
+
|
|
240
|
+
「内建」= 宿主按 schema 声明自动建、但用户未显式声明的列:``__present`` 哨兵 +
|
|
241
|
+
timestamps + 归属键。用户显式声明了同名字段时**不剔除**(该列已参与字段 diff,
|
|
242
|
+
误剔会被当成「新增列」重复 ADD COLUMN)。同时返回物理名与逻辑名,兼容显式传入
|
|
243
|
+
camelCase 形式的 old_defs。
|
|
244
|
+
"""
|
|
245
|
+
declared = defn.get('fields') or {}
|
|
246
|
+
declared_phys = {_pname(backend, k) for k in declared}
|
|
247
|
+
names = ['__present']
|
|
248
|
+
for logical, _sql_type in implicit_columns(defn, backend):
|
|
249
|
+
phys = _pname(backend, logical)
|
|
250
|
+
if phys in declared_phys or logical in declared:
|
|
251
|
+
continue
|
|
252
|
+
names.append(phys)
|
|
253
|
+
if phys != logical:
|
|
254
|
+
names.append(logical)
|
|
255
|
+
return names
|
|
256
|
+
|
|
257
|
+
|
|
219
258
|
def _columns(defn, backend):
|
|
220
259
|
"""返回 [(name, sql_type, pk, auto)],顺序:声明的字段(标量 / object·array JSON 列)→
|
|
221
260
|
timestamps → 归属键(owner 行控隐含)→ __present;name 为该后端物理列名(snake_case)。"""
|
|
@@ -422,11 +461,46 @@ def _archive_defn_of(defn):
|
|
|
422
461
|
arch['name'] = defn['name'] + 'Deleted'
|
|
423
462
|
arch['collection'] = (defn.get('collection') or defn['name']) + '_deleted'
|
|
424
463
|
arch['fields'] = fields
|
|
425
|
-
|
|
464
|
+
# 归档表列 = 主表列 + deletedAt。不得改 timestamps:归档文档由 core 按主表 schema
|
|
465
|
+
# 生成(含 created_at / updated_at),置 False 会让归档 INSERT 报
|
|
466
|
+
# ``no column named created_at``(默认 timestamps 的表 remove 直接失败)。
|
|
426
467
|
arch['_isArchive'] = True
|
|
427
468
|
return arch
|
|
428
469
|
|
|
429
470
|
|
|
471
|
+
def archive_ddl(backend, defn):
|
|
472
|
+
"""归档表建表语句(列/索引与主表同源):供迁移补建「主表已存在但 ``_deleted`` 缺失」的老库。
|
|
473
|
+
|
|
474
|
+
``archive=False`` 无归档表 → 返回空列表(与 addTable 分支同口径)。
|
|
475
|
+
"""
|
|
476
|
+
if defn.get('archive') is False:
|
|
477
|
+
return []
|
|
478
|
+
arch = _archive_defn_of(defn)
|
|
479
|
+
return [_create_table(arch, backend), *_index_stmts(arch, backend)]
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
def implicit_column_ddl(backend, defn, existing):
|
|
483
|
+
"""补齐缺失内建列的 ALTER 语句(供迁移修复旧版归档表)。
|
|
484
|
+
|
|
485
|
+
旧版 ``_archive_defn_of`` 固定 ``timestamps=False``,历史上建出的
|
|
486
|
+
``<table>_deleted`` 缺 ``created_at`` / ``updated_at``(归档 INSERT 报
|
|
487
|
+
``no column named created_at``)。内建列不在 diff_defs 的比较范围(只比声明字段),
|
|
488
|
+
故单独补齐。
|
|
489
|
+
"""
|
|
490
|
+
i = _idx(backend)
|
|
491
|
+
table = _pname(backend, defn.get('collection') or defn.get('name'))
|
|
492
|
+
out = []
|
|
493
|
+
if '__present' not in existing:
|
|
494
|
+
out.append(f'ALTER TABLE {_q(backend, table)} ADD COLUMN '
|
|
495
|
+
f'{_q(backend, "__present")} {_PRESENT_TYPE[i]}')
|
|
496
|
+
for name, sql_type in implicit_columns(defn, backend):
|
|
497
|
+
phys = _pname(backend, name)
|
|
498
|
+
if phys not in existing:
|
|
499
|
+
out.append(f'ALTER TABLE {_q(backend, table)} ADD COLUMN '
|
|
500
|
+
f'{_q(backend, phys)} {sql_type}')
|
|
501
|
+
return out
|
|
502
|
+
|
|
503
|
+
|
|
430
504
|
def diff_defs(old_defn, new_defn):
|
|
431
505
|
"""新旧 schema def 对比 → {'changes': [...], 'errors': [...]}(后端无关,纯函数)。
|
|
432
506
|
|
|
@@ -460,7 +534,6 @@ def diff_defs(old_defn, new_defn):
|
|
|
460
534
|
continue
|
|
461
535
|
changes.append({
|
|
462
536
|
'op': 'addColumn', 'name': name,
|
|
463
|
-
'sqlType': None, # generate 侧按 backend 解析(保持 diff 后端无关)
|
|
464
537
|
'field': nf if isinstance(nf, dict) else {'type': nf},
|
|
465
538
|
})
|
|
466
539
|
continue
|
|
@@ -492,6 +565,10 @@ def diff_defs(old_defn, new_defn):
|
|
|
492
565
|
errors.append(f'删除字段 "{name}" 不支持(破坏性变更;请显式走数据迁移脚本)')
|
|
493
566
|
|
|
494
567
|
# ── 索引:new 有 old 无 → addIndex ──
|
|
568
|
+
# 相等判定用稳定序列化(键序无关 + JS 数字语义),对齐 node diffDefs 的 _stableStringify:
|
|
569
|
+
# 直接 dict 相等(py `1 == True`)与 node `JSON.stringify` 键序敏感会得出不同结论(P9)。
|
|
570
|
+
from .metadef import stable_stringify as _canon
|
|
571
|
+
|
|
495
572
|
def _index_keys(defn):
|
|
496
573
|
out = []
|
|
497
574
|
for idx in defn.get('indexes') or []:
|
|
@@ -499,9 +576,9 @@ def diff_defs(old_defn, new_defn):
|
|
|
499
576
|
out.append(idx)
|
|
500
577
|
return out
|
|
501
578
|
|
|
502
|
-
old_idx = _index_keys(old_defn)
|
|
579
|
+
old_idx = {_canon(i) for i in _index_keys(old_defn)}
|
|
503
580
|
for idx in _index_keys(new_defn):
|
|
504
|
-
if idx not in old_idx:
|
|
581
|
+
if _canon(idx) not in old_idx:
|
|
505
582
|
changes.append({'op': 'addIndex', 'index': idx})
|
|
506
583
|
|
|
507
584
|
# collection 改名 = 建新表丢旧表(破坏性)→ Err
|
|
@@ -160,9 +160,97 @@ def _assert_serializable(value, path=''):
|
|
|
160
160
|
_assert_serializable(v, f'{path}.{i}' if path else str(i))
|
|
161
161
|
|
|
162
162
|
|
|
163
|
+
#: IEEE-754 双精度可无损表示的最大整数(``2^53 - 1``);超出后 Python ``int`` 与 JS
|
|
164
|
+
#: ``number`` 不再同值,必须按 double 归一才能双端同 hash
|
|
165
|
+
_MAX_SAFE_INT = 2 ** 53 - 1
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _shortest_digits(value):
|
|
169
|
+
"""正有限 float → ``(digits, n)``:``value == digits × 10^(n - len(digits))``。
|
|
170
|
+
|
|
171
|
+
``repr`` 给出最短往返十进制位(与 JS ``String`` 同源同长),此处只拆位并去掉尾零;
|
|
172
|
+
``n`` 是「小数点在 digits 中的位置 + 1」的 JS 记法(ECMAScript ToString 的 ``n``)。
|
|
173
|
+
"""
|
|
174
|
+
mantissa, _, exp_s = repr(value).partition('e')
|
|
175
|
+
exp = int(exp_s) if exp_s else 0
|
|
176
|
+
ip, _, fp = mantissa.partition('.')
|
|
177
|
+
digits = (ip + fp).lstrip('0').rstrip('0')
|
|
178
|
+
if ip.strip('0'):
|
|
179
|
+
e10 = len(ip) - 1
|
|
180
|
+
else:
|
|
181
|
+
e10 = -(fp.index(fp.lstrip('0')[0]) + 1) if fp.strip('0') else 0
|
|
182
|
+
return digits, e10 + 1 + exp
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _format_digits(digits, n):
|
|
186
|
+
"""按 ECMAScript 排版规则输出(仅指数符号归一为无 ``+``、无补零)"""
|
|
187
|
+
k = len(digits)
|
|
188
|
+
if k <= n <= 21:
|
|
189
|
+
return digits + '0' * (n - k)
|
|
190
|
+
if 0 < n <= 21:
|
|
191
|
+
return digits[:n] + '.' + digits[n:]
|
|
192
|
+
if -6 < n <= 0:
|
|
193
|
+
return '0.' + '0' * (-n) + digits
|
|
194
|
+
return digits[0] + ('.' + digits[1:] if k > 1 else '') + 'e' + str(n - 1)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _canonical_number(value):
|
|
198
|
+
"""数字 → 规范字面量(**本项目的规范格式,不是 JS 语义**)。
|
|
199
|
+
|
|
200
|
+
两宿主逐字节一致是硬约束(同一定义跨端必须同 hash,否则 migrate 幂等闸失效)。规则:
|
|
201
|
+
|
|
202
|
+
1. NaN / ±Infinity → ``null``(同 ``JSON.stringify``);``-0`` → ``0``;
|
|
203
|
+
2. ``|v| <= 2^53 - 1`` 的整数原样输出;超出的 Python ``int`` 先按 double 归一
|
|
204
|
+
(JS 侧本就是 double,不归一则大整数两端不同值);
|
|
205
|
+
3. 有限 double 取**最短往返十进制位**(py ``repr`` / node ``String`` 同源),按
|
|
206
|
+
ECMAScript 的排版阈值输出:``1e-6 <= |v| < 1e21`` 用普通小数,其余用科学记数法;
|
|
207
|
+
4. 指数固定为**最简形式**(无补零、无 ``+``):``1e-7`` / ``1e21``。
|
|
208
|
+
因此 py 旧输出的 ``1e-07`` 与 ``1e+21`` 不再出现——这正是本轮修复的跨端分叉。
|
|
209
|
+
"""
|
|
210
|
+
if isinstance(value, int):
|
|
211
|
+
if abs(value) <= _MAX_SAFE_INT:
|
|
212
|
+
return str(value)
|
|
213
|
+
try:
|
|
214
|
+
value = float(value)
|
|
215
|
+
except OverflowError: # 超出 double 范围:JS 侧为 Infinity → null
|
|
216
|
+
return 'null'
|
|
217
|
+
if value != value or value in (float('inf'), float('-inf')):
|
|
218
|
+
return 'null'
|
|
219
|
+
if value == 0:
|
|
220
|
+
return '0'
|
|
221
|
+
sign = '-' if value < 0 else ''
|
|
222
|
+
return sign + _format_digits(*_shortest_digits(abs(value)))
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _encode_json(value):
|
|
226
|
+
"""JSON 编码(键序无关 + 规范数字)——对齐 node ``_stableStringify`` 逐字节"""
|
|
227
|
+
if value is None:
|
|
228
|
+
return 'null'
|
|
229
|
+
if value is True:
|
|
230
|
+
return 'true'
|
|
231
|
+
if value is False:
|
|
232
|
+
return 'false'
|
|
233
|
+
if isinstance(value, (int, float)):
|
|
234
|
+
return _canonical_number(value)
|
|
235
|
+
if isinstance(value, str):
|
|
236
|
+
return json.dumps(value, ensure_ascii=False)
|
|
237
|
+
if isinstance(value, (list, tuple)):
|
|
238
|
+
return '[' + ','.join(_encode_json(v) for v in value) + ']'
|
|
239
|
+
if isinstance(value, dict):
|
|
240
|
+
return '{' + ','.join(
|
|
241
|
+
json.dumps(str(k), ensure_ascii=False) + ':' + _encode_json(v)
|
|
242
|
+
for k, v in sorted(value.items(), key=lambda kv: kv[0])) + '}'
|
|
243
|
+
raise TypeError(f'Object of type {type(value).__name__} is not JSON serializable')
|
|
244
|
+
|
|
245
|
+
|
|
163
246
|
def stable_stringify(value):
|
|
164
|
-
"""稳定序列化(键序无关):与 node ``_stableStringify``
|
|
165
|
-
|
|
247
|
+
"""稳定序列化(键序无关):与 node ``_stableStringify`` 逐字节等价。
|
|
248
|
+
|
|
249
|
+
数字走 :func:`_canonical_number`(项目规范格式,非 JS 语义),否则非整数浮点在
|
|
250
|
+
py(``1e-07``)与 node(``1e-7``)得到不同字符串 → 同一定义跨端 hash 分叉 →
|
|
251
|
+
migrate 幂等闸失效、每次跨端重跑追加空版本行。跨端对拍语料见
|
|
252
|
+
``tests/fixtures/stable-stringify-parity.json``。"""
|
|
253
|
+
return _encode_json(value)
|
|
166
254
|
|
|
167
255
|
|
|
168
256
|
def same_defn(a, b):
|
|
@@ -29,6 +29,7 @@ from . import crud as _crud
|
|
|
29
29
|
from . import ddl as _ddl
|
|
30
30
|
from . import introspect as _introspect
|
|
31
31
|
from .datasource import execute_raw, run_in_transaction
|
|
32
|
+
from .feedback import emit as _emit_feedback
|
|
32
33
|
from .metadef import (
|
|
33
34
|
_MIGRATION,
|
|
34
35
|
MetaDefError,
|
|
@@ -41,6 +42,10 @@ from .schema import core as _core
|
|
|
41
42
|
_UNSUPPORTED_HINT = '白名单外变更请分两版走:先加新列并回填,确认切换后再于下一版删除旧列'
|
|
42
43
|
|
|
43
44
|
_NUMBERS = {'int', 'long', 'float', 'double', 'number'}
|
|
45
|
+
#: 内省反推 ``number`` 的物理族:SQLite ``INTEGER`` 承载 int/long/float/double/date/datetime/bool;
|
|
46
|
+
#: MySQL ``BIGINT`` 承载 long/number/datetime/date,``TINYINT(1)``(bool)也反推为 number。
|
|
47
|
+
#: old 侧反推为 number 时,new 声明落在同族内不得判「非放宽变更」(D3)。
|
|
48
|
+
_NUMBER_FAMILY = _NUMBERS | {'date', 'datetime', 'bool', 'boolean'}
|
|
44
49
|
|
|
45
50
|
|
|
46
51
|
class MigrationUnsupported(MetaDefError):
|
|
@@ -62,15 +67,18 @@ def _hash_of(defn):
|
|
|
62
67
|
return hashlib.sha1(stable_stringify(defn).encode('utf-8')).hexdigest()
|
|
63
68
|
|
|
64
69
|
|
|
65
|
-
def _strip_builtin_fields(old_defn, new_defn):
|
|
66
|
-
"""内建物理列剔除:introspect→schema_from_rows 的 defn 带 ``__present`` 哨兵与
|
|
67
|
-
|
|
68
|
-
diff 误报「删除字段不支持」。
|
|
70
|
+
def _strip_builtin_fields(backend, old_defn, new_defn):
|
|
71
|
+
"""内建物理列剔除:introspect→schema_from_rows 的 defn 带 ``__present`` 哨兵与
|
|
72
|
+
宿主自动补齐的 timestamps / 归属键物理列(snake_case;兼容 camelCase)——不剔则
|
|
73
|
+
diff 误报「删除字段不支持」。
|
|
74
|
+
|
|
75
|
+
剔除清单**不在本文件自设**,一律取 ``_ddl.builtin_field_names``(与 DDL 生成共用
|
|
76
|
+
同一份内建列定义):此前用 ``new_defn['timestamps']`` 真值判断 + 固定两列,与 DDL
|
|
77
|
+
的 ``is not False`` 口径分叉(D1),且漏掉归属键(D2)。
|
|
78
|
+
"""
|
|
69
79
|
if not old_defn or not isinstance(old_defn, dict):
|
|
70
80
|
return old_defn
|
|
71
|
-
drop = ['__present']
|
|
72
|
-
if new_defn and new_defn.get('timestamps'):
|
|
73
|
-
drop += ['created_at', 'updated_at', 'createdAt', 'updatedAt']
|
|
81
|
+
drop = _ddl.builtin_field_names(new_defn, backend) if new_defn else ['__present']
|
|
74
82
|
fields = dict(old_defn.get('fields') or {})
|
|
75
83
|
for k in drop:
|
|
76
84
|
fields.pop(k, None)
|
|
@@ -108,16 +116,16 @@ def _physical_defn(backend, defn):
|
|
|
108
116
|
|
|
109
117
|
def _align_types(backend, old_defn, phys_new):
|
|
110
118
|
"""反推粒度对齐:introspect 反推类型粒度可粗于用户声明(SQLite 动态类型 → ``number``,
|
|
111
|
-
承载 int/long/float/double 全域)——old 侧字段为 ``number`` 时把 new
|
|
112
|
-
``number`` 比对(物理层确无差异;MySQL/PG
|
|
113
|
-
|
|
119
|
+
承载 int/long/float/double/date/datetime/bool 全域)——old 侧字段为 ``number`` 时把 new 侧
|
|
120
|
+
同名同族字段归一为 ``number`` 比对(物理层确无差异;MySQL/PG 反推保真不受影响)。
|
|
121
|
+
仅同名两侧都有时归一;新加列保留用户类型(DDL 仍按声明产列型)。"""
|
|
114
122
|
old_fields = (old_defn or {}).get('fields') or {}
|
|
115
123
|
fields = dict(phys_new.get('fields') or {})
|
|
116
124
|
for k, v in fields.items():
|
|
117
125
|
o = old_fields.get(k)
|
|
118
126
|
ot = (o.get('type') if isinstance(o, dict) else o) if o is not None else None
|
|
119
127
|
nt = v.get('type') if isinstance(v, dict) else v
|
|
120
|
-
if ot == 'number' and nt in
|
|
128
|
+
if ot == 'number' and nt in _NUMBER_FAMILY:
|
|
121
129
|
fields[k] = {**v, 'type': 'number'} if isinstance(v, dict) else 'number'
|
|
122
130
|
return {**phys_new, 'fields': fields}
|
|
123
131
|
|
|
@@ -173,7 +181,53 @@ async def migrate(*, backend, source, defs, old_defs=None, driver=None,
|
|
|
173
181
|
skipped = []
|
|
174
182
|
for name, defn in defs.items():
|
|
175
183
|
old_defn = _strip_builtin_fields(
|
|
184
|
+
backend,
|
|
176
185
|
old.get(name) or (old.get(defn.get('collection')) if defn.get('collection') else None), defn)
|
|
186
|
+
# ⓪ 内建列自动补齐(仅 introspect 模式):camelCase 时代建的表常缺
|
|
187
|
+
# ``created_by`` / ``created_at`` 等内建列。先按当前 DDL 自动补列(老库照样迁得过去),
|
|
188
|
+
# 同时发出可定位告警;**只有补列失败才中断**(保留真报错,不吞成静默通过)。
|
|
189
|
+
if probe is not None and old_defs is None:
|
|
190
|
+
table = _ddl._pname(backend, defn.get('collection') or defn.get('name'))
|
|
191
|
+
if any(t.get('name') == table for t in probe.get('tables') or []):
|
|
192
|
+
have = {c.get('name') for c in probe.get('columns') or [] if c.get('table') == table}
|
|
193
|
+
missing = sorted({_ddl._pname(backend, n)
|
|
194
|
+
for n in _ddl.builtin_field_names(defn, backend)} - have)
|
|
195
|
+
if missing:
|
|
196
|
+
try:
|
|
197
|
+
for stmt in _ddl.implicit_column_ddl(backend, defn, have):
|
|
198
|
+
await execute_raw(source, stmt, None, True)
|
|
199
|
+
except Exception as e:
|
|
200
|
+
reason = (f'物理表 {table} 缺内建列 [{", ".join(missing)}],自动补列失败:{e};'
|
|
201
|
+
'请按当前 DDL 手工补列或重建表后重跑迁移')
|
|
202
|
+
skipped.append({'name': name, 'reason': reason})
|
|
203
|
+
raise MigrationUnsupported(
|
|
204
|
+
f'MIGRATION_UNSUPPORTED: {reason}({_UNSUPPORTED_HINT})', skipped) from e
|
|
205
|
+
_emit_feedback({
|
|
206
|
+
'type': 'warning',
|
|
207
|
+
'code': 'migrationAutoAddBuiltinColumns',
|
|
208
|
+
'layer': 'migrate',
|
|
209
|
+
'message': (f'物理表 {table} 缺内建列 [{", ".join(missing)}],'
|
|
210
|
+
'已按当前 DDL 自动补列后继续迁移'),
|
|
211
|
+
'hint': '新补列的存量行为 NULL;归属键列需按业务回填后 creator 行控才可见',
|
|
212
|
+
})
|
|
213
|
+
have |= set(missing)
|
|
214
|
+
# ⓪b 归属键回填完整性(仅 introspect 模式):列已存在但值为 NULL ⇒ creator
|
|
215
|
+
# 行控下这些行对非 bypass 用户不可见。迁移层不猜业务归属,放行但必须告警
|
|
216
|
+
# (允许通过、禁止静默失守)。
|
|
217
|
+
owner = _ddl._owner_field(defn)
|
|
218
|
+
if owner is not None:
|
|
219
|
+
col = _ddl._pname(backend, owner)
|
|
220
|
+
hit = await execute_raw(
|
|
221
|
+
source, f'SELECT 1 FROM {table} WHERE {col} IS NULL LIMIT 1', None, False)
|
|
222
|
+
if hit.get('rows'):
|
|
223
|
+
_emit_feedback({
|
|
224
|
+
'type': 'warning',
|
|
225
|
+
'code': 'migrationOwnerKeyNullRows',
|
|
226
|
+
'layer': 'migrate',
|
|
227
|
+
'message': (f'物理表 {table} 的归属键 {col} 存在 NULL 行:'
|
|
228
|
+
'creator 行控下这些行对非 bypass 用户不可见'),
|
|
229
|
+
'hint': '请按业务回填归属键;迁移已继续,未静默拦截',
|
|
230
|
+
})
|
|
177
231
|
# ⑤ 幂等闸(先于 diff):hash 相同 → skipped unchanged,不 diff(反推粒度不齐防误报)
|
|
178
232
|
hash_ = _hash_of(defn)
|
|
179
233
|
seg = {'tenant': '' if tenant is None else tenant,
|
|
@@ -193,6 +247,23 @@ async def migrate(*, backend, source, defs, old_defs=None, driver=None,
|
|
|
193
247
|
f'MIGRATION_UNSUPPORTED: {";".join(plan["errors"])}({_UNSUPPORTED_HINT})', skipped)
|
|
194
248
|
# ④ 方言 SQL(纯文本)
|
|
195
249
|
stmts = _ddl.generate_migration(backend, old_defn, phys_new)
|
|
250
|
+
# ⓪c 归档表补建(仅 introspect 模式):主表已存在时 addTable 分支不触发,老库常缺
|
|
251
|
+
# ``<table>_deleted`` 附表 → remove 的归档写入报 no such table(删除功能不可用)。
|
|
252
|
+
# 此处按当前 def 补建;archive=False 无归档表,跳过。
|
|
253
|
+
if probe is not None and old_defs is None and defn.get('archive') is not False:
|
|
254
|
+
arch_defn = _ddl._archive_defn_of(defn)
|
|
255
|
+
main_table = _ddl._pname(backend, defn.get('collection') or defn.get('name'))
|
|
256
|
+
arch_table = _ddl._pname(backend, arch_defn.get('collection') or arch_defn.get('name'))
|
|
257
|
+
existing = {t.get('name') for t in probe.get('tables') or []}
|
|
258
|
+
if main_table in existing:
|
|
259
|
+
if arch_table not in existing:
|
|
260
|
+
stmts = _ddl.archive_ddl(backend, defn) + stmts
|
|
261
|
+
else:
|
|
262
|
+
# 内建列补齐(旧版归档表缺 created_at/updated_at/__present;声明字段差异
|
|
263
|
+
# 由 generate_migration 主表分支同步处理,此处不重复 diff)
|
|
264
|
+
arch_have = {c.get('name') for c in probe.get('columns') or []
|
|
265
|
+
if c.get('table') == arch_table}
|
|
266
|
+
stmts = _ddl.implicit_column_ddl(backend, arch_defn, arch_have) + stmts
|
|
196
267
|
# version = 该 name 历史最大 version+1(复用 metadef next_version 语义)
|
|
197
268
|
version = (max(r['version'] for r in rows) if rows else 0) + 1
|
|
198
269
|
# ⑥ SQL 与版本行同事务:失败整体回滚、无孤儿版本行
|
|
@@ -91,9 +91,17 @@ def _core_fn_key(key, comp):
|
|
|
91
91
|
# 内部标记:表示「该键需剔除」(对齐 JS JSON round-trip 中函数型 default 被移除)
|
|
92
92
|
_DROP = object()
|
|
93
93
|
|
|
94
|
+
# 落点键:由 `_loc_of` 显式注入 Location,不进 core 定义(01 起定义零落点)
|
|
95
|
+
_LOCATION_KEYS = ('database', 'schema')
|
|
96
|
+
|
|
94
97
|
|
|
95
98
|
def _to_core_defn(defn):
|
|
96
|
-
"""生成可跨 FFI 的 schema 定义:fn/asyncFn → True 占位;函数型值剔除
|
|
99
|
+
"""生成可跨 FFI 的 schema 定义:fn/asyncFn → True 占位;函数型值剔除
|
|
100
|
+
|
|
101
|
+
落点键(``database``/``schema``)不属 core 白名单(01 起「定义零落点」):
|
|
102
|
+
由 :func:`_loc_of` 显式注入 Location 后从定义中剔除,否则 core 注册期会以
|
|
103
|
+
「含未知顶层键」拒绝(既有 defn 兼容由本层承担,core 不再放行)。
|
|
104
|
+
"""
|
|
97
105
|
|
|
98
106
|
def walk(value, key=None):
|
|
99
107
|
if callable(value):
|
|
@@ -115,7 +123,10 @@ def _to_core_defn(defn):
|
|
|
115
123
|
return out_arr
|
|
116
124
|
return value
|
|
117
125
|
|
|
118
|
-
|
|
126
|
+
core_defn = walk(defn)
|
|
127
|
+
for k in _LOCATION_KEYS:
|
|
128
|
+
core_defn.pop(k, None)
|
|
129
|
+
return core_defn
|
|
119
130
|
|
|
120
131
|
|
|
121
132
|
def _loc_of(defn):
|
|
@@ -235,7 +246,8 @@ def register_batch(items, ctx=None):
|
|
|
235
246
|
落点**不进 defn**(定义文件零落点)。
|
|
236
247
|
"""
|
|
237
248
|
get_core().register_batch(
|
|
238
|
-
[{'defn': _to_core_defn(it['defn']),
|
|
249
|
+
[{'defn': _to_core_defn(it['defn']),
|
|
250
|
+
'location': it.get('location') or _loc_of(it['defn'])} for it in items],
|
|
239
251
|
ctx,
|
|
240
252
|
)
|
|
241
253
|
for it in items:
|
|
@@ -244,6 +256,24 @@ def register_batch(items, ctx=None):
|
|
|
244
256
|
_mirror(defn)
|
|
245
257
|
|
|
246
258
|
|
|
259
|
+
def unregister(name):
|
|
260
|
+
"""注销一个 schema(core 幂等:未知 schema 返回成功,回滚清单可整份重放)。
|
|
261
|
+
|
|
262
|
+
判决唯一在 core(``Registry::unregister``):主项与其归档派生物
|
|
263
|
+
``<Name>Deleted`` 一并清理,版本号不保留。Host 镜像同步移除同名项与
|
|
264
|
+
派生项,使 ``has`` / ``list`` 与 core 一致。
|
|
265
|
+
"""
|
|
266
|
+
core = get_core()
|
|
267
|
+
fn = getattr(core, 'unregister', None)
|
|
268
|
+
if fn is None:
|
|
269
|
+
raise RuntimeError(
|
|
270
|
+
'当前 rust-store-py 绑定未导出 Registry.unregister(core 源码已实现,'
|
|
271
|
+
'需重建绑定后生效);无法执行注销,未做任何 Host 镜像改动')
|
|
272
|
+
fn(name)
|
|
273
|
+
_schemas.pop(name, None)
|
|
274
|
+
_schemas.pop(f'{name}Deleted', None)
|
|
275
|
+
|
|
276
|
+
|
|
247
277
|
def get(name):
|
|
248
278
|
"""按名称获取 Host 侧元数据"""
|
|
249
279
|
s = _schemas.get(name)
|