test_migration.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. from __future__ import annotations
  2. import importlib.util
  3. from pathlib import Path
  4. from alembic.config import Config
  5. from alembic.script import ScriptDirectory
  6. BACKEND_ROOT = Path(__file__).resolve().parents[2]
  7. MIGRATIONS_ROOT = BACKEND_ROOT / "migrations"
  8. REVISION_PATH = MIGRATIONS_ROOT / "versions" / "0001_initial_schema.py"
  9. RESTORE_REVISION_PATH = (
  10. MIGRATIONS_ROOT / "versions" / "0002_add_restore_audit_action.py"
  11. )
  12. Q3_REVISION_PATH = (
  13. MIGRATIONS_ROOT
  14. / "versions"
  15. / "0003_q3_business_alignment_and_content_search.py"
  16. )
  17. REORDER_REVISION_PATH = (
  18. MIGRATIONS_ROOT / "versions" / "0009_reorder_audit_action.py"
  19. )
  20. ATTACHMENT_TYPE_REVISION_PATH = (
  21. MIGRATIONS_ROOT / "versions" / "0010_attachment_type_redefine.py"
  22. )
  23. REORDER_CATEGORY_REVISION_PATH = (
  24. MIGRATIONS_ROOT
  25. / "versions"
  26. / "0011_reorder_category_audit_action.py"
  27. )
  28. ROLE_MANAGEMENT_REVISION_PATH = (
  29. MIGRATIONS_ROOT / "versions" / "0012_role_management.py"
  30. )
  31. DOCUMENT_PASSWORD_LOCK_REVISION_PATH = (
  32. MIGRATIONS_ROOT / "versions" / "0013_document_password_lock.py"
  33. )
  34. DOCUMENT_RELATIONS_REVISION_PATH = (
  35. MIGRATIONS_ROOT / "versions" / "0014_document_relations.py"
  36. )
  37. LOGIN_IP_REVISION_PATH = (
  38. MIGRATIONS_ROOT / "versions" / "0015_login_ip_restriction.py"
  39. )
  40. PORTAL_USER_PROVISION_REVISION_PATH = (
  41. MIGRATIONS_ROOT / "versions" / "0016_portal_user_provision.py"
  42. )
  43. TYPE_DISPLAY_NAME_REVISION_PATH = (
  44. MIGRATIONS_ROOT / "versions" / "0017_category_type_display_name.py"
  45. )
  46. SUB_PLAN_BINDING_REVISION_PATH = (
  47. MIGRATIONS_ROOT / "versions" / "0018_sub_plan_binding.py"
  48. )
  49. CONTENT_EXTRACT_ERROR_REVISION_PATH = (
  50. MIGRATIONS_ROOT / "versions" / "0019_content_extract_error.py"
  51. )
  52. REMOVE_ASSIGNED_SCOPE_REVISION_PATH = (
  53. MIGRATIONS_ROOT / "versions" / "0021_remove_assigned_scope.py"
  54. )
  55. def _load_revision_module():
  56. spec = importlib.util.spec_from_file_location("dms_initial_migration", REVISION_PATH)
  57. assert spec is not None and spec.loader is not None
  58. module = importlib.util.module_from_spec(spec)
  59. spec.loader.exec_module(module)
  60. return module
  61. def test_alembic_config_and_head_are_valid():
  62. config = Config(str(MIGRATIONS_ROOT / "alembic.ini"))
  63. script = ScriptDirectory.from_config(config)
  64. assert script.get_current_head() == "0021_remove_assigned_scope"
  65. def test_initial_migration_exports_upgrade_and_downgrade():
  66. module = _load_revision_module()
  67. assert callable(module.upgrade)
  68. assert callable(module.downgrade)
  69. def test_initial_migration_contains_seven_tables_and_generated_columns():
  70. source = REVISION_PATH.read_text(encoding="utf-8")
  71. for table_name in (
  72. "sys_organization",
  73. "sys_user",
  74. "doc_category",
  75. "doc_document",
  76. "doc_attachment_binding",
  77. "doc_permission",
  78. "sys_audit_log",
  79. ):
  80. assert f'"{table_name}"' in source
  81. assert source.count('sa.Computed("IF(is_deleted = 0, 1, NULL)"') == 2
  82. assert "uq_doc_attachment_binding_active" in source
  83. assert "uq_doc_permission_active" in source
  84. def test_initial_migration_has_no_cascade():
  85. source = REVISION_PATH.read_text(encoding="utf-8")
  86. assert 'ondelete="CASCADE"' not in source
  87. assert source.count('ondelete="RESTRICT"') >= 8
  88. def test_downgrade_uses_dependency_safe_order():
  89. source = REVISION_PATH.read_text(encoding="utf-8")
  90. positions = [
  91. source.index(f'op.drop_table("{name}")')
  92. for name in (
  93. "sys_audit_log",
  94. "doc_permission",
  95. "doc_attachment_binding",
  96. "doc_document",
  97. "doc_category",
  98. "sys_user",
  99. "sys_organization",
  100. )
  101. ]
  102. assert positions == sorted(positions)
  103. def test_restore_migration_is_scoped_and_reversible():
  104. source = RESTORE_REVISION_PATH.read_text(encoding="utf-8")
  105. assert 'down_revision: str | None = "0001_initial_schema"' in source
  106. assert "RESTORE_DOCUMENT" in source
  107. assert "ix_doc_document_recycle_deleted" in source
  108. assert '["is_deleted", "deleted_at", "id"]' in source
  109. assert "drop_index" in source
  110. assert source.count("create_check_constraint") == 2
  111. assert "create_table" not in source
  112. assert "add_column" not in source
  113. def test_q3_migration_is_single_scoped_revision_with_safe_downgrade():
  114. source = Q3_REVISION_PATH.read_text(encoding="utf-8")
  115. assert (
  116. 'down_revision: str | None = "0002_add_restore_audit_action"'
  117. in source
  118. )
  119. assert "content_text" in source
  120. assert "content_extract_status" in source
  121. assert "content_extracted_at" in source
  122. assert "LONGTEXT" in source
  123. assert "auth_version = auth_version + 1" in source
  124. assert "role_code = 'USER'" in source
  125. assert "role_code IN ('USER', 'ADMIN')" in source
  126. assert "RuntimeError" in source
  127. assert "create_table" not in source
  128. def test_reorder_audit_migration_is_scoped_and_reversible():
  129. source = REORDER_REVISION_PATH.read_text(encoding="utf-8")
  130. assert 'down_revision: str | None = "0008_document_sort_no"' in source
  131. assert "REORDER_DOCUMENT" in source
  132. assert "REORDER_ATTACHMENT" in source
  133. assert "audit_action_type" in source
  134. assert "RuntimeError" in source
  135. assert "create_table" not in source
  136. assert "add_column" not in source
  137. def test_attachment_type_redefine_migration_maps_legacy_values():
  138. source = ATTACHMENT_TYPE_REVISION_PATH.read_text(encoding="utf-8")
  139. assert 'down_revision: str | None = "0009_reorder_audit_action"' in source
  140. assert "SUPPORT_ACHIEVEMENT" in source
  141. assert "TRAINING_MATERIAL" in source
  142. assert "TYPICAL_CASE" in source
  143. assert "document_attachment_type" in source
  144. assert "UPDATE doc_document SET attachment_type = 'SUPPORT_ACHIEVEMENT'" in source
  145. assert "RuntimeError" in source
  146. assert "create_table" not in source
  147. assert "add_column" not in source
  148. def test_reorder_category_audit_migration_is_scoped_and_reversible():
  149. source = REORDER_CATEGORY_REVISION_PATH.read_text(encoding="utf-8")
  150. assert (
  151. 'down_revision: str | None = "0010_attachment_type_redefine"'
  152. in source
  153. )
  154. assert "REORDER_CATEGORY" in source
  155. assert "audit_action_type" in source
  156. assert "RuntimeError" in source
  157. assert "create_table" not in source
  158. assert "add_column" not in source
  159. def test_role_management_migration_is_scoped_and_reversible():
  160. source = ROLE_MANAGEMENT_REVISION_PATH.read_text(encoding="utf-8")
  161. assert (
  162. 'down_revision: str | None = "0011_reorder_category_audit_action"'
  163. in source
  164. )
  165. for table_name in ("sys_role", "sys_role_dept", "sys_role_permission"):
  166. assert f'"{table_name}"' in source
  167. assert "uq_sys_role_default_active" in source
  168. assert "IF(is_default = 1 AND is_deleted = 0, 1, NULL)" in source
  169. assert "can_config_permission" in source
  170. assert "can_bind_attachment" in source
  171. assert "can_unbind_attachment" in source
  172. assert "fk_sys_user_role_id_sys_role" in source
  173. assert "CHANGE_ROLE_PERMISSION" in source
  174. assert "'ROLE'" in source
  175. assert "RuntimeError" in source
  176. assert source.count("op.create_table") == 3
  177. # 降级按依赖序先删矩阵/部门,再删角色本体
  178. positions = [
  179. source.index(f'op.drop_table("{name}")')
  180. for name in ("sys_role_permission", "sys_role_dept", "sys_role")
  181. ]
  182. assert positions == sorted(positions)
  183. def test_document_password_lock_migration_is_scoped_and_reversible():
  184. source = DOCUMENT_PASSWORD_LOCK_REVISION_PATH.read_text(encoding="utf-8")
  185. assert (
  186. 'down_revision: str | None = "0012_role_management"' in source
  187. )
  188. assert '"password_hash"' in source
  189. for column in (
  190. "protect_view",
  191. "protect_download",
  192. "protect_edit",
  193. "protect_delete",
  194. "protect_config_permission",
  195. "protect_bind_attachment",
  196. "protect_unbind_attachment",
  197. ):
  198. assert f'"{column}"' in source
  199. assert "document_password_main_only" in source
  200. assert "(password_hash IS NULL) OR (document_type = 'MAIN')" in source
  201. assert "DOCUMENT_PASSWORD_SET" in source
  202. assert "DOCUMENT_PASSWORD_REMOVE" in source
  203. assert "RuntimeError" in source
  204. assert "create_table" not in source
  205. def test_document_relations_migration_is_scoped_and_reversible():
  206. source = DOCUMENT_RELATIONS_REVISION_PATH.read_text(encoding="utf-8")
  207. assert (
  208. 'down_revision: str | None = "0013_document_password_lock"' in source
  209. )
  210. assert 'op.create_table(\n "doc_document_relation"' in source
  211. assert '"relation_type"' in source
  212. assert "sa.String(length=64)" in source
  213. assert "uq_doc_document_relation_active" in source
  214. assert "IF(is_deleted = 0, 1, NULL)" in source
  215. assert "document_relation_no_self" in source
  216. assert "document_id <> related_document_id" in source
  217. assert source.count("ondelete=\"RESTRICT\"") == 2
  218. # 审计复用EDIT_DOCUMENT,不扩展sys_audit_log的CHECK约束
  219. assert "audit_action_type" not in source
  220. assert "RuntimeError" in source
  221. assert source.count("op.create_table") == 1
  222. assert "add_column" not in source
  223. def test_login_ip_restriction_migration_is_scoped_and_reversible():
  224. source = LOGIN_IP_REVISION_PATH.read_text(encoding="utf-8")
  225. assert (
  226. 'down_revision: str | None = "0014_document_relations"' in source
  227. )
  228. for table_name in ("sys_ip_entry", "sys_org_ip_assignment"):
  229. assert f'"{table_name}"' in source
  230. assert '"ip_value"' in source
  231. assert "uq_sys_ip_entry_ip_value_active" in source
  232. assert "uq_sys_org_ip_assignment_active" in source
  233. assert "IF(is_deleted = 0, 1, NULL)" in source
  234. assert "fk_sys_org_ip_assignment_organization_id_sys_organization" in source
  235. assert "fk_sys_org_ip_assignment_ip_entry_id_sys_ip_entry" in source
  236. assert "CREATE_IP_ENTRY" in source
  237. assert "ASSIGN_ORG_IP" in source
  238. assert '"IP_ENTRY"' in source
  239. assert "RuntimeError" in source
  240. assert source.count("op.create_table") == 2
  241. # 降级按依赖序先删分配表,再删IP条目表
  242. positions = [
  243. source.index(f'op.drop_table("{name}")')
  244. for name in ("sys_org_ip_assignment", "sys_ip_entry")
  245. ]
  246. assert positions == sorted(positions)
  247. def test_portal_user_provision_migration_is_scoped_and_reversible():
  248. source = PORTAL_USER_PROVISION_REVISION_PATH.read_text(encoding="utf-8")
  249. assert (
  250. 'down_revision: str | None = "0015_login_ip_restriction"' in source
  251. )
  252. assert '"external_id"' in source
  253. assert "sa.String(length=128)" in source
  254. assert "uq_sys_user_external_id_active" in source
  255. assert "ix_sys_user_external_active" in source
  256. assert "IF(is_deleted = 0, 1, NULL)" in source
  257. # 降级保护:存在门户用户时拒绝有损降级
  258. assert "external_id IS NOT NULL" in source
  259. assert "RuntimeError" in source
  260. assert "create_table" not in source
  261. def test_category_type_display_name_migration_is_scoped_and_idempotent():
  262. source = TYPE_DISPLAY_NAME_REVISION_PATH.read_text(encoding="utf-8")
  263. assert (
  264. 'down_revision: str | None = "0016_portal_user_provision"' in source
  265. )
  266. assert '_COLUMN = "type_display_name"' in source
  267. assert "sa.String(length=128)" in source
  268. # 幂等:按现存列集合判断,重复执行不重复加列
  269. assert "_existing_columns" in source
  270. assert "drop_column" in source
  271. assert "create_table" not in source
  272. def test_sub_plan_binding_migration_is_scoped_and_reversible():
  273. source = SUB_PLAN_BINDING_REVISION_PATH.read_text(encoding="utf-8")
  274. assert (
  275. 'down_revision: str | None = "0017_category_type_display_name"'
  276. in source
  277. )
  278. assert '"doc_sub_plan_binding"' in source
  279. assert "uq_doc_sub_plan_binding_active" in source
  280. assert "IF(is_deleted = 0, 1, NULL)" in source
  281. assert "ix_doc_sub_plan_binding_main_active_sort" in source
  282. assert "ix_doc_sub_plan_binding_sub_active" in source
  283. assert source.count('ondelete="RESTRICT"') == 2
  284. # 审计复用 BIND/UNBIND_ATTACHMENT,不扩展 sys_audit_log 的 CHECK 约束
  285. assert "audit_action_type" not in source
  286. # 降级保护:已产生挂载数据时拒绝有损降级
  287. assert "RuntimeError" in source
  288. assert source.count("op.create_table") == 1
  289. assert "add_column" not in source
  290. def test_content_extract_error_migration_is_scoped_and_idempotent():
  291. source = CONTENT_EXTRACT_ERROR_REVISION_PATH.read_text(encoding="utf-8")
  292. assert (
  293. 'down_revision: str | None = "0018_sub_plan_binding"' in source
  294. )
  295. assert '_TABLE = "doc_document"' in source
  296. assert '_COLUMN = "content_extract_error"' in source
  297. assert "sa.String(512)" in source
  298. # 幂等:按现存列集合判断,重复执行不重复加列
  299. assert "_existing_columns" in source
  300. assert "add_column" in source
  301. assert "drop_column" in source
  302. assert "create_table" not in source
  303. # 降级保护:已产生失败记录时拒绝有损降级
  304. assert "RuntimeError" in source
  305. assert "IS NOT NULL" in source
  306. def test_remove_assigned_scope_migration_is_scoped_and_idempotent():
  307. source = REMOVE_ASSIGNED_SCOPE_REVISION_PATH.read_text(encoding="utf-8")
  308. assert (
  309. 'down_revision: str | None = "0020_sub_plan_to_attachment"' in source
  310. )
  311. # 存量 ASSIGNED 行物理删除(含软删行)
  312. assert "DELETE FROM sys_role_permission WHERE dept_scope = 'ASSIGNED'" in source
  313. # CHECK 约束按名幂等换名收敛:旧删新加
  314. assert "ck_sys_role_permission_role_permission_dept_scope" in source
  315. assert "ck_sys_role_permission_dept_scope_v2" in source
  316. assert "dept_scope IN ('OWN', 'OTHER')" in source
  317. # 角色可访问部门表下线
  318. assert "DROP TABLE IF EXISTS sys_role_dept" in source
  319. # 降级:重建 sys_role_dept 空表并补种 ASSIGNED 默认全否行
  320. assert 'op.create_table(\n "sys_role_dept"' in source
  321. assert "'ASSIGNED'" in source