@umacloud/knowledge 1.0.1
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.
- package/00-governance/governance-capabilities.md +557 -0
- package/00-governance/knowledge-map.md +39 -0
- package/00-governance/maintenance-policy.md +76 -0
- package/00-governance/review-checklist.md +81 -0
- package/README.md +13 -0
- package/ai/01-standards/agent-development-complete.md +691 -0
- package/ai/01-standards/llm-application-complete.md +488 -0
- package/ai/01-standards/mlops-complete.md +798 -0
- package/ai/01-standards/prompt-engineering-complete.md +646 -0
- package/ai/01-standards/rag-architecture-complete.md +649 -0
- package/ai/02-playbooks/llm-evaluation-playbook.md +847 -0
- package/ai/03-checklists/ai-project-checklist.md +215 -0
- package/ai/04-antipatterns/ai-antipatterns.md +661 -0
- package/ai/05-cases/case-rag-production.md +147 -0
- package/ai/06-glossary/ai-glossary.md +162 -0
- package/ai/agent-evaluation-benchmark.md +53 -0
- package/ai/ai-agent-memory-context-management.md +41 -0
- package/ai/ai-cost-capacity-optimization-playbook.md +42 -0
- package/ai/ai-data-security-and-compliance-playbook.md +37 -0
- package/ai/ai-domain-index-and-checklist.md +40 -0
- package/ai/ai-governance-maturity-model.md +50 -0
- package/ai/ai-model-selection-and-routing-strategy.md +47 -0
- package/ai/ai-observability-and-oncall-runbook.md +52 -0
- package/ai/ai-rag-engineering-playbook.md +42 -0
- package/ai/ai-red-team-and-safety-evaluation.md +42 -0
- package/ai/ai-release-readiness-and-rollback-gate.md +42 -0
- package/ai/llm-agent-engineering-deep-dive.md +57 -0
- package/ai/prompt-and-tool-guardrails.md +52 -0
- package/api/01-standards/enterprise-api-standards.md +198 -0
- package/api/01-standards/rest-api-design-guide.md +63 -0
- package/api/02-playbooks/api-pagination-playbook.md +93 -0
- package/api/02-playbooks/graphql-production-playbook.md +176 -0
- package/api/03-checklists/api-review-checklist.md +55 -0
- package/api/04-antipatterns/api-antipatterns.md +112 -0
- package/architecture/01-standards/api-gateway-patterns.md +496 -0
- package/architecture/01-standards/cloud-native-patterns.md +644 -0
- package/architecture/01-standards/distributed-systems-patterns.md +591 -0
- package/architecture/01-standards/event-driven-architecture.md +595 -0
- package/architecture/01-standards/microservices-patterns-complete.md +968 -0
- package/architecture/01-standards/microservices-patterns.md +495 -0
- package/architecture/01-standards/system-design-interview.md +664 -0
- package/architecture/02-playbooks/microservices-patterns-playbook.md +137 -0
- package/architecture/02-playbooks/migration-playbook.md +780 -0
- package/architecture/02-playbooks/system-design-playbook.md +779 -0
- package/architecture/03-checklists/architecture-decision-checklist.md +297 -0
- package/architecture/04-antipatterns/architecture-antipatterns.md +417 -0
- package/architecture/05-cases/case-netflix-microservices.md +413 -0
- package/architecture/06-glossary/architecture-glossary.md +164 -0
- package/architecture/adr-template-and-examples.md +38 -0
- package/architecture/api-gateway-deep-dive.md +1291 -0
- package/architecture/configuration-management.md +1162 -0
- package/architecture/distributed-transactions.md +1220 -0
- package/architecture/microservices-complete.md +735 -0
- package/architecture/resilience-and-disaster-patterns.md +37 -0
- package/architecture/service-governance.md +1198 -0
- package/architecture/system-architecture-deep-dive.md +37 -0
- package/backend/01-standards/analytics-and-growth.md +65 -0
- package/backend/01-standards/api-and-error-conventions.md +120 -0
- package/backend/01-standards/application-layering-and-packaging.md +160 -0
- package/backend/01-standards/auth-implementation.md +104 -0
- package/backend/01-standards/backend-framework-idioms.md +74 -0
- package/backend/01-standards/background-jobs-and-async.md +66 -0
- package/backend/01-standards/caching-strategies-complete.md +390 -0
- package/backend/01-standards/config-and-observability.md +77 -0
- package/backend/01-standards/data-modeling-and-persistence.md +94 -0
- package/backend/01-standards/django-complete.md +1765 -0
- package/backend/01-standards/email-and-notifications.md +64 -0
- package/backend/01-standards/fastapi-complete.md +925 -0
- package/backend/01-standards/file-upload-and-storage.md +66 -0
- package/backend/01-standards/graphql-api-complete.md +416 -0
- package/backend/01-standards/llm-application-standard.md +78 -0
- package/backend/01-standards/message-queue-patterns.md +379 -0
- package/backend/01-standards/microservices-and-distributed.md +78 -0
- package/backend/01-standards/nestjs-complete.md +2167 -0
- package/backend/01-standards/payment-integration.md +80 -0
- package/backend/01-standards/rate-limiting-complete.md +451 -0
- package/backend/01-standards/realtime-and-websocket.md +65 -0
- package/backend/01-standards/search-and-filtering.md +64 -0
- package/backend/01-standards/spring-boot-complete.md +445 -0
- package/backend/02-playbooks/api-design-playbook.md +718 -0
- package/backend/02-playbooks/email-send-playbook.md +130 -0
- package/backend/02-playbooks/file-upload-s3-playbook.md +153 -0
- package/backend/02-playbooks/typescript-enterprise-playbook.md +133 -0
- package/backend/02-playbooks/websocket-realtime-playbook.md +154 -0
- package/backend/03-checklists/api-launch-checklist.md +189 -0
- package/backend/04-antipatterns/backend-antipatterns.md +1051 -0
- package/blockchain/01-standards/blockchain-basics.md +557 -0
- package/blockchain/01-standards/smart-contract-development.md +1315 -0
- package/cicd/01-standards/deployment-and-delivery-standard.md +96 -0
- package/cicd/01-standards/github-actions-complete.md +473 -0
- package/cicd/01-standards/release-and-store-submission.md +75 -0
- package/cicd/02-playbooks/cicd-pipeline-playbook.md +144 -0
- package/cicd/02-playbooks/release-management-playbook.md +605 -0
- package/cicd/03-checklists/pipeline-security-checklist.md +168 -0
- package/cicd/04-antipatterns/cicd-antipatterns.md +589 -0
- package/cicd/05-cases/case-deployment-automation.md +221 -0
- package/cicd/05-cases/case-gitops-transformation.md +212 -0
- package/cicd/06-glossary/cicd-glossary.md +114 -0
- package/cicd/cicd-blueprint-deep-dive.md +38 -0
- package/cicd/release-readiness-gate.md +37 -0
- package/cloud-native/01-standards/container-security.md +741 -0
- package/cloud-native/01-standards/kubernetes-complete.md +812 -0
- package/cloud-native/02-playbooks/api-gateway-playbook.md +155 -0
- package/cloud-native/02-playbooks/gitops-with-argocd.md +760 -0
- package/cloud-native/02-playbooks/k8s-troubleshooting-playbook.md +1942 -0
- package/cloud-native/02-playbooks/message-queue-playbook.md +129 -0
- package/cloud-native/02-playbooks/multicloud-governance.md +726 -0
- package/cloud-native/02-playbooks/serverless-patterns.md +788 -0
- package/cloud-native/02-playbooks/service-mesh-playbook.md +612 -0
- package/cloud-native/02-playbooks/terraform-iac-playbook.md +143 -0
- package/cloud-native/03-checklists/container-security-checklist.md +431 -0
- package/cloud-native/03-checklists/k8s-production-readiness-checklist.md +460 -0
- package/cloud-native/04-antipatterns/container-antipatterns.md +660 -0
- package/cloud-native/04-antipatterns/k8s-antipatterns.md +743 -0
- package/cloud-native/05-cases/case-k8s-migration.md +478 -0
- package/cloud-native/05-cases/case-k8s-scaling.md +642 -0
- package/cloud-native/05-cases/case-k8s-security-incident.md +397 -0
- package/cloud-native/06-glossary/cloud-native-glossary.md +337 -0
- package/cross-platform/01-standards/cross-platform-frameworks.md +83 -0
- package/cross-platform/01-standards/platform-selection-and-architecture.md +77 -0
- package/data/01-standards/elasticsearch-complete.md +2098 -0
- package/data/01-standards/postgresql-complete.md +1613 -0
- package/data/01-standards/redis-complete.md +1527 -0
- package/data/02-playbooks/database-optimization-playbook.md +403 -0
- package/data/02-playbooks/elasticsearch-production-playbook.md +132 -0
- package/data/03-checklists/database-launch-checklist.md +187 -0
- package/data/04-antipatterns/database-antipatterns.md +873 -0
- package/data/05-cases/case-database-migration.md +310 -0
- package/data/06-glossary/database-glossary.md +440 -0
- package/data/data-governance-and-modeling-deep-dive.md +39 -0
- package/data-engineering/01-standards/airflow-complete.md +523 -0
- package/data-engineering/01-standards/kafka-complete.md +1521 -0
- package/data-engineering/02-playbooks/spark-etl-playbook.md +496 -0
- package/data-engineering/03-checklists/pipeline-launch-checklist.md +194 -0
- package/data-engineering/04-antipatterns/data-pipeline-antipatterns.md +684 -0
- package/data-engineering/05-cases/case-real-time-pipeline.md +355 -0
- package/data-engineering/06-glossary/data-engineering-glossary.md +429 -0
- package/database/01-standards/database-schema-standards.md +147 -0
- package/database/02-playbooks/postgresql-optimization-quick.md +52 -0
- package/database/02-playbooks/postgresql-performance-optimization.md +58 -0
- package/database/02-playbooks/postgresql-production-playbook.md +146 -0
- package/database/02-playbooks/redis-caching-playbook.md +117 -0
- package/database/03-checklists/database-review-checklist.md +50 -0
- package/database/04-antipatterns/database-antipatterns.md +112 -0
- package/design/01-standards/ui-design-system-complete.md +423 -0
- package/design/02-playbooks/design-handoff-playbook.md +254 -0
- package/design/02-playbooks/design-review-playbook.md +388 -0
- package/design/03-checklists/design-review-checklist.md +246 -0
- package/design/04-antipatterns/design-antipatterns.md +378 -0
- package/design/05-cases/case-design-system-adoption.md +328 -0
- package/design/06-glossary/design-glossary.md +329 -0
- package/design/ui-full-lifecycle-cross-platform-playbook.md +571 -0
- package/design/ux-system-deep-dive.md +38 -0
- package/design-systems/00-craft-rules.md +71 -0
- package/design-systems/aesthetic-families.md +43 -0
- package/design-systems/anti-ai-slop.md +162 -0
- package/design-systems/bold-geometric.md +120 -0
- package/design-systems/brutalist-bold.md +103 -0
- package/design-systems/editorial-clean.md +109 -0
- package/design-systems/glass-aurora.md +108 -0
- package/design-systems/modern-minimal.md +145 -0
- package/design-systems/premium-luxury.md +106 -0
- package/design-systems/product-type-design-map.md +48 -0
- package/design-systems/soft-warm.md +123 -0
- package/design-systems/tech-utility.md +113 -0
- package/desktop/01-standards/desktop-app-standard.md +72 -0
- package/desktop/01-standards/desktop-design.md +71 -0
- package/development/00-governance/document-template.md +41 -0
- package/development/01-standards/api-versioning-strategies.md +432 -0
- package/development/01-standards/authentication-patterns-complete.md +479 -0
- package/development/01-standards/css-architecture-complete.md +550 -0
- package/development/01-standards/database-migration-strategies.md +484 -0
- package/development/01-standards/elasticsearch-complete.md +347 -0
- package/development/01-standards/git-complete.md +371 -0
- package/development/01-standards/golang-complete.md +1565 -0
- package/development/01-standards/graphql-complete.md +298 -0
- package/development/01-standards/javascript-bundlers-complete.md +469 -0
- package/development/01-standards/javascript-typescript-complete.md +528 -0
- package/development/01-standards/jest-complete.md +275 -0
- package/development/01-standards/linux-complete.md +234 -0
- package/development/01-standards/logging-observability-complete.md +526 -0
- package/development/01-standards/microservices-communication.md +502 -0
- package/development/01-standards/mongodb-complete.md +406 -0
- package/development/01-standards/oauth2-complete.md +285 -0
- package/development/01-standards/performance-optimization-complete.md +289 -0
- package/development/01-standards/playwright-complete.md +247 -0
- package/development/01-standards/postgresql-complete.md +456 -0
- package/development/01-standards/pytest-complete.md +340 -0
- package/development/01-standards/python-async-programming.md +902 -0
- package/development/01-standards/python-complete.md +956 -0
- package/development/01-standards/python-decorators-complete.md +799 -0
- package/development/01-standards/python-design-patterns.md +2854 -0
- package/development/01-standards/python-packaging-distribution.md +420 -0
- package/development/01-standards/python-testing-strategies.md +607 -0
- package/development/01-standards/python-web-frameworks-comparison.md +471 -0
- package/development/01-standards/redis-complete.md +317 -0
- package/development/01-standards/rest-api-complete.md +316 -0
- package/development/01-standards/rust-complete.md +578 -0
- package/development/01-standards/typescript-advanced-types.md +1513 -0
- package/development/01-standards/web-security-complete.md +292 -0
- package/development/02-playbooks/api-design-playbook.md +810 -0
- package/development/02-playbooks/database-migration-playbook.md +580 -0
- package/development/02-playbooks/debugging-playbook.md +692 -0
- package/development/02-playbooks/feature-delivery-playbook.md +430 -0
- package/development/02-playbooks/incident-hotfix-playbook.md +387 -0
- package/development/02-playbooks/performance-optimization-playbook.md +531 -0
- package/development/02-playbooks/performance-tuning-playbook.md +652 -0
- package/development/02-playbooks/refactor-playbook.md +403 -0
- package/development/02-playbooks/release-playbook.md +469 -0
- package/development/03-checklists/architecture-review-checklist.md +168 -0
- package/development/03-checklists/data-migration-checklist.md +157 -0
- package/development/03-checklists/oncall-handover-checklist.md +173 -0
- package/development/03-checklists/pr-checklist.md +158 -0
- package/development/03-checklists/production-readiness-checklist.md +190 -0
- package/development/03-checklists/release-readiness-checklist.md +154 -0
- package/development/03-checklists/security-review-checklist.md +182 -0
- package/development/04-antipatterns/api-antipatterns.md +657 -0
- package/development/04-antipatterns/architecture-antipatterns.md +686 -0
- package/development/04-antipatterns/backend-antipatterns.md +648 -0
- package/development/04-antipatterns/cicd-antipatterns.md +540 -0
- package/development/04-antipatterns/code-smell-antipatterns.md +571 -0
- package/development/04-antipatterns/data-antipatterns.md +658 -0
- package/development/04-antipatterns/database-antipatterns.md +578 -0
- package/development/04-antipatterns/frontend-antipatterns.md +635 -0
- package/development/04-antipatterns/reliability-antipatterns.md +700 -0
- package/development/04-antipatterns/security-antipatterns.md +747 -0
- package/development/05-cases/case-api-version-migration.md +428 -0
- package/development/05-cases/case-authorization-hardening.md +383 -0
- package/development/05-cases/case-bluegreen-rollback.md +466 -0
- package/development/05-cases/case-cache-snowball-protection.md +485 -0
- package/development/05-cases/case-ci-cd-pipeline.md +544 -0
- package/development/05-cases/case-database-scaling.md +500 -0
- package/development/05-cases/case-db-hotspot-optimization.md +487 -0
- package/development/05-cases/case-incident-mttr-reduction.md +563 -0
- package/development/05-cases/case-microservice-migration.md +375 -0
- package/development/05-cases/case-performance-optimization.md +406 -0
- package/development/05-cases/case-security-incident-response.md +345 -0
- package/development/06-glossary/full-stack-glossary.md +166 -0
- package/development/09-maturity/quarterly-audit-template.md +35 -0
- package/development/11-ui-excellence/ui-aesthetic-system.md +41 -0
- package/development/11-ui-excellence/ui-engineering-excellence.md +435 -0
- package/development/12-scenarios/development-scenarios-guide.md +565 -0
- package/development/13-implementation-assets/implementation-toolkit.md +282 -0
- package/development/13-implementation-assets/knowledge-gates-execution.md +43 -0
- package/development/14-full-lifecycle/software-lifecycle-gates.md +511 -0
- package/development/15-lifecycle-templates/project-templates-collection.md +791 -0
- package/development/api-contract-and-versioning-guide.md +36 -0
- package/development/api-governance-complete.md +43 -0
- package/development/backend-engineering-complete.md +43 -0
- package/development/code-review-quality-complete.md +43 -0
- package/development/concurrency-reliability-complete.md +43 -0
- package/development/database-engineering-complete.md +43 -0
- package/development/engineering-effectiveness-complete.md +43 -0
- package/development/engineering-standards-deep-dive.md +38 -0
- package/development/frontend-engineering-complete.md +43 -0
- package/development/performance-capacity-complete.md +43 -0
- package/development/refactor-migration-complete.md +42 -0
- package/development/refactoring-and-techdebt-playbook.md +37 -0
- package/development/security-in-development-complete.md +43 -0
- package/devops/01-standards/cicd-pipeline-complete.md +262 -0
- package/devops/01-standards/docker-complete.md +1490 -0
- package/devops/01-standards/github-actions-complete.md +337 -0
- package/devops/01-standards/kubernetes-complete.md +638 -0
- package/devops/01-standards/terraform-complete.md +2117 -0
- package/devops/02-playbooks/docker-compose-playbook.md +233 -0
- package/devops/02-playbooks/docker-k8s-production-playbook.md +186 -0
- package/devops/02-playbooks/docker-production-playbook.md +952 -0
- package/edge-iot/01-standards/edge-iot-complete.md +473 -0
- package/experts/architect/api-design.md +178 -0
- package/experts/architect/methodology.md +124 -0
- package/experts/architect/security.md +75 -0
- package/experts/backend-lead/methodology.md +216 -0
- package/experts/devops/methodology.md +160 -0
- package/experts/frontend-lead/methodology.md +178 -0
- package/experts/product-manager/industry/ecommerce.md +43 -0
- package/experts/product-manager/industry/saas.md +40 -0
- package/experts/product-manager/methodology.md +97 -0
- package/experts/qa-lead/methodology.md +123 -0
- package/experts/qa-lead/test-strategy.md +128 -0
- package/experts/uiux-designer/methodology.md +125 -0
- package/frontend/01-standards/accessibility-complete.md +532 -0
- package/frontend/01-standards/accessibility-standard.md +74 -0
- package/frontend/01-standards/admin-dashboard-and-crud.md +72 -0
- package/frontend/01-standards/design-tokens-complete.md +444 -0
- package/frontend/01-standards/forms-and-validation.md +77 -0
- package/frontend/01-standards/frontend-architecture-and-layering.md +119 -0
- package/frontend/01-standards/i18n-and-localization.md +65 -0
- package/frontend/01-standards/nextjs-complete.md +451 -0
- package/frontend/01-standards/react-complete.md +713 -0
- package/frontend/01-standards/react-hooks-complete-guide.md +1100 -0
- package/frontend/01-standards/react-hooks-complete.md +1171 -0
- package/frontend/01-standards/seo-and-web-vitals.md +77 -0
- package/frontend/01-standards/state-management-complete.md +444 -0
- package/frontend/01-standards/vue-complete.md +499 -0
- package/frontend/01-standards/vue3-complete.md +2002 -0
- package/frontend/01-standards/web-framework-best-practices.md +64 -0
- package/frontend/01-standards/web-performance-complete.md +495 -0
- package/frontend/02-playbooks/accessibility-a11y-playbook.md +161 -0
- package/frontend/02-playbooks/frontend-performance-playbook.md +707 -0
- package/frontend/02-playbooks/i18n-internationalization-playbook.md +120 -0
- package/frontend/02-playbooks/performance-optimization-playbook.md +163 -0
- package/frontend/02-playbooks/react-nextjs-production-playbook.md +167 -0
- package/frontend/02-playbooks/react-state-management-playbook.md +173 -0
- package/frontend/03-checklists/component-quality-checklist.md +166 -0
- package/frontend/03-checklists/frontend-launch-checklist.md +299 -0
- package/frontend/04-antipatterns/frontend-antipatterns.md +886 -0
- package/frontend/05-cases/case-performance-optimization.md +274 -0
- package/harmony/01-standards/harmonyos-arkts-standard.md +75 -0
- package/harmony/01-standards/harmonyos-design.md +65 -0
- package/high-quality-engineering-playbook.md +54 -0
- package/incident/01-standards/incident-response-complete.md +303 -0
- package/incident/02-playbooks/chaos-engineering-playbook.md +883 -0
- package/incident/02-playbooks/postmortem-playbook.md +398 -0
- package/incident/03-checklists/incident-readiness-checklist.md +181 -0
- package/incident/04-antipatterns/incident-antipatterns.md +490 -0
- package/incident/05-cases/case-cascade-failure.md +176 -0
- package/incident/06-glossary/incident-glossary.md +114 -0
- package/incident/postmortem-and-response-deep-dive.md +39 -0
- package/industries/ecommerce/ecommerce-complete.md +631 -0
- package/industries/education/education-complete.md +555 -0
- package/industries/fintech/fintech-complete.md +501 -0
- package/industries/gaming/gaming-complete.md +587 -0
- package/industries/healthcare/healthcare-complete.md +452 -0
- package/low-code/01-standards/low-code-complete.md +944 -0
- package/miniprogram/01-standards/ai-common-mistakes.md +61 -0
- package/miniprogram/01-standards/miniprogram-custom-navbar-capsule.md +77 -0
- package/miniprogram/01-standards/miniprogram-design.md +61 -0
- package/miniprogram/01-standards/miniprogram-standard.md +81 -0
- package/mobile/01-standards/android-material-design.md +70 -0
- package/mobile/01-standards/flutter-complete.md +384 -0
- package/mobile/01-standards/ios-design-hig.md +78 -0
- package/mobile/01-standards/mobile-app-standard.md +85 -0
- package/mobile/01-standards/react-native-complete.md +352 -0
- package/mobile/02-playbooks/mobile-cross-platform-playbook.md +175 -0
- package/mobile/02-playbooks/mobile-performance.md +473 -0
- package/mobile/03-checklists/mobile-release-checklist.md +234 -0
- package/mobile/04-antipatterns/mobile-antipatterns.md +798 -0
- package/mobile/05-cases/case-app-performance.md +500 -0
- package/mobile/05-cases/case-app-startup-optimization.md +218 -0
- package/mobile/06-glossary/mobile-glossary.md +484 -0
- package/observability/01-standards/observability-standards.md +103 -0
- package/observability/02-playbooks/prometheus-grafana-playbook.md +135 -0
- package/observability/02-playbooks/structured-logging-playbook.md +73 -0
- package/observability/03-checklists/observability-checklist.md +54 -0
- package/observability/04-antipatterns/observability-antipatterns.md +106 -0
- package/operations/01-standards/prometheus-monitoring-complete.md +1578 -0
- package/operations/02-playbooks/capacity-planning-playbook.md +620 -0
- package/operations/03-checklists/production-launch-checklist.md +365 -0
- package/operations/04-antipatterns/operations-antipatterns.md +664 -0
- package/operations/05-cases/case-sre-practices.md +581 -0
- package/operations/06-glossary/operations-glossary.md +120 -0
- package/operations/aiops-anomaly-detection.md +758 -0
- package/operations/capacity-planning.md +1061 -0
- package/operations/chaos-engineering.md +659 -0
- package/operations/incident-command-system.md +38 -0
- package/operations/observability-complete.md +442 -0
- package/operations/slo-sli-playbook.md +517 -0
- package/operations/sre-operations-deep-dive.md +39 -0
- package/package.json +8 -0
- package/performance/01-standards/performance-and-scalability.md +80 -0
- package/performance/01-standards/performance-standards.md +156 -0
- package/performance/02-playbooks/query-optimization-playbook.md +103 -0
- package/performance/03-checklists/performance-checklist.md +56 -0
- package/performance/04-antipatterns/performance-antipatterns.md +146 -0
- package/product/01-standards/product-management-complete.md +285 -0
- package/product/02-playbooks/feature-launch-playbook.md +207 -0
- package/product/02-playbooks/user-research-playbook.md +532 -0
- package/product/03-checklists/feature-launch-checklist.md +275 -0
- package/product/04-antipatterns/product-antipatterns.md +355 -0
- package/product/05-cases/case-mvp-to-scale.md +384 -0
- package/product/06-glossary/product-glossary.md +462 -0
- package/product/feature-prioritization-framework.md +40 -0
- package/product/kpi-and-metric-tree.md +37 -0
- package/product/product-discovery-and-prd-deep-dive.md +41 -0
- package/quantum/01-standards/quantum-complete.md +1186 -0
- package/security/01-standards/api-security-complete.md +511 -0
- package/security/01-standards/container-runtime-security.md +574 -0
- package/security/01-standards/data-protection-gdpr.md +543 -0
- package/security/01-standards/owasp-top10-complete.md +1890 -0
- package/security/01-standards/secure-coding-baseline.md +90 -0
- package/security/01-standards/supply-chain-security.md +441 -0
- package/security/01-standards/web-security-checklist.md +108 -0
- package/security/01-standards/zero-trust-architecture.md +521 -0
- package/security/02-playbooks/auth-sso-playbook.md +166 -0
- package/security/02-playbooks/incident-response-security-playbook.md +588 -0
- package/security/02-playbooks/owasp-api-security-playbook.md +129 -0
- package/security/02-playbooks/payment-integration-playbook.md +119 -0
- package/security/02-playbooks/penetration-testing-playbook.md +517 -0
- package/security/03-checklists/security-audit-checklist.md +356 -0
- package/security/04-antipatterns/security-coding-antipatterns.md +580 -0
- package/security/05-cases/case-log4shell-incident.md +537 -0
- package/security/05-cases/case-major-breaches.md +468 -0
- package/security/06-glossary/security-glossary.md +212 -0
- package/security/compliance-automation.md +993 -0
- package/security/container-security.md +680 -0
- package/security/devsecops-complete.md +426 -0
- package/security/sast-dast-sca.md +775 -0
- package/security/secrets-management.md +594 -0
- package/security/security-architecture-deep-dive.md +37 -0
- package/security/threat-modeling-stride-playbook.md +40 -0
- package/seed-templates/auth-system.md +59 -0
- package/seed-templates/blog-content.md +94 -0
- package/seed-templates/dashboard.md +89 -0
- package/seed-templates/docs-site.md +73 -0
- package/seed-templates/e-commerce.md +50 -0
- package/seed-templates/saas-landing.md +92 -0
- package/seed-templates/settings-page.md +51 -0
- package/testing/01-standards/test-strategy-and-layering.md +83 -0
- package/testing/01-standards/testing-strategy-complete.md +422 -0
- package/testing/01-standards/unit-testing-best-practices.md +118 -0
- package/testing/02-playbooks/e2e-testing-playbook.md +988 -0
- package/testing/02-playbooks/testing-strategy-playbook.md +126 -0
- package/testing/03-checklists/test-strategy-checklist.md +208 -0
- package/testing/04-antipatterns/testing-antipatterns.md +718 -0
- package/testing/05-cases/case-testing-transformation.md +300 -0
- package/testing/06-glossary/testing-glossary.md +110 -0
- package/testing/risk-based-test-matrix.md +36 -0
- package/testing/testing-strategy-deep-dive.md +37 -0
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: database-antipatterns
|
|
3
|
+
title: 数据库反模式指南
|
|
4
|
+
domain: development
|
|
5
|
+
category: 04-antipatterns
|
|
6
|
+
difficulty: intermediate
|
|
7
|
+
tags: [antipatterns, database, delete, development, index, pagination, problem, query]
|
|
8
|
+
quality_score: 70
|
|
9
|
+
last_updated: 2026-06-15
|
|
10
|
+
---
|
|
11
|
+
# 数据库反模式指南
|
|
12
|
+
|
|
13
|
+
> 适用范围:PostgreSQL / MySQL / MongoDB / Redis
|
|
14
|
+
> 约束级别:SHALL(必须在 Code Review 和 SQL Review 阶段拦截)
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 1. N+1 查询(N+1 Query Problem)
|
|
19
|
+
|
|
20
|
+
### 描述
|
|
21
|
+
先查询主表获得 N 条记录,然后对每条记录单独查询关联表,导致总共执行 N+1 次数据库查询。在列表页场景下,N 可能是数百甚至数千,直接导致接口响应时间线性增长。
|
|
22
|
+
|
|
23
|
+
### 错误示例
|
|
24
|
+
```python
|
|
25
|
+
# Django ORM -- 典型 N+1
|
|
26
|
+
def get_orders_with_user(request):
|
|
27
|
+
orders = Order.objects.all()[:100] # 1 次查询
|
|
28
|
+
result = []
|
|
29
|
+
for order in orders:
|
|
30
|
+
# 每次循环触发 1 次查询,共 100 次
|
|
31
|
+
result.append({
|
|
32
|
+
"order_id": order.id,
|
|
33
|
+
"user_name": order.user.name, # SELECT * FROM users WHERE id = ?
|
|
34
|
+
"user_email": order.user.email,
|
|
35
|
+
})
|
|
36
|
+
return JsonResponse(result, safe=False)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
// Sequelize -- 典型 N+1
|
|
41
|
+
async function getPostsWithComments() {
|
|
42
|
+
const posts = await Post.findAll({ limit: 50 }); // 1 次查询
|
|
43
|
+
for (const post of posts) {
|
|
44
|
+
// 每次循环触发 1 次查询,共 50 次
|
|
45
|
+
post.comments = await Comment.findAll({
|
|
46
|
+
where: { postId: post.id },
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return posts;
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 正确示例
|
|
54
|
+
```python
|
|
55
|
+
# Django -- 使用 select_related / prefetch_related
|
|
56
|
+
def get_orders_with_user(request):
|
|
57
|
+
orders = Order.objects.select_related("user").all()[:100] # 1 次 JOIN 查询
|
|
58
|
+
result = [
|
|
59
|
+
{
|
|
60
|
+
"order_id": order.id,
|
|
61
|
+
"user_name": order.user.name,
|
|
62
|
+
"user_email": order.user.email,
|
|
63
|
+
}
|
|
64
|
+
for order in orders
|
|
65
|
+
]
|
|
66
|
+
return JsonResponse(result, safe=False)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
# SQLAlchemy -- 使用 joinedload
|
|
71
|
+
def get_orders_with_user(session: Session) -> list[Order]:
|
|
72
|
+
return (
|
|
73
|
+
session.query(Order)
|
|
74
|
+
.options(joinedload(Order.user))
|
|
75
|
+
.limit(100)
|
|
76
|
+
.all()
|
|
77
|
+
)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```javascript
|
|
81
|
+
// Sequelize -- 使用 eager loading
|
|
82
|
+
async function getPostsWithComments() {
|
|
83
|
+
return Post.findAll({
|
|
84
|
+
limit: 50,
|
|
85
|
+
include: [{ model: Comment, as: "comments" }],
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### 检测方法
|
|
91
|
+
- Django Debug Toolbar 的 SQL 面板:单个请求查询数 > 10 即需警觉。
|
|
92
|
+
- `nplusone` 库(Django/SQLAlchemy):自动检测 N+1 并抛出异常。
|
|
93
|
+
- 数据库慢查询日志:相同模板的 SQL 在短时间内执行多次。
|
|
94
|
+
- APM 工具(Datadog / New Relic):查看单个请求的 DB 调用次数。
|
|
95
|
+
|
|
96
|
+
### 修复步骤
|
|
97
|
+
1. 开启 ORM 的 SQL 日志,统计单次请求的查询数量。
|
|
98
|
+
2. 对外键关联使用 `select_related`(一对一/多对一)或 `prefetch_related`(一对多/多对多)。
|
|
99
|
+
3. 对于非 ORM 场景,使用 `WHERE id IN (...)` 批量查询替代循环单查。
|
|
100
|
+
4. 添加集成测试断言查询次数(`assertNumQueries` in Django)。
|
|
101
|
+
|
|
102
|
+
### Agent Checklist
|
|
103
|
+
- [ ] 列表接口查询次数 <= 5
|
|
104
|
+
- [ ] 所有外键访问使用 `select_related` 或 `prefetch_related`
|
|
105
|
+
- [ ] 循环中无数据库查询
|
|
106
|
+
- [ ] 集成测试包含查询次数断言
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## 2. 缺少索引(Missing Index)
|
|
111
|
+
|
|
112
|
+
### 描述
|
|
113
|
+
WHERE、JOIN、ORDER BY 使用的列未建立索引,导致全表扫描。在数据量从千级增长到百万级时,查询时间从毫秒级劣化到秒级。
|
|
114
|
+
|
|
115
|
+
### 错误示例
|
|
116
|
+
```sql
|
|
117
|
+
-- 表结构:无索引
|
|
118
|
+
CREATE TABLE orders (
|
|
119
|
+
id SERIAL PRIMARY KEY,
|
|
120
|
+
user_id INTEGER,
|
|
121
|
+
status VARCHAR(20),
|
|
122
|
+
created_at TIMESTAMP,
|
|
123
|
+
total_amount DECIMAL(10, 2)
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
-- 以下查询全部触发全表扫描
|
|
127
|
+
SELECT * FROM orders WHERE user_id = 12345;
|
|
128
|
+
SELECT * FROM orders WHERE status = 'pending' ORDER BY created_at DESC;
|
|
129
|
+
SELECT * FROM orders WHERE created_at BETWEEN '2024-01-01' AND '2024-01-31';
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### 正确示例
|
|
133
|
+
```sql
|
|
134
|
+
-- 为高频查询模式建立索引
|
|
135
|
+
CREATE INDEX idx_orders_user_id ON orders(user_id);
|
|
136
|
+
CREATE INDEX idx_orders_status_created ON orders(status, created_at DESC);
|
|
137
|
+
CREATE INDEX idx_orders_created_at ON orders(created_at);
|
|
138
|
+
|
|
139
|
+
-- 使用覆盖索引避免回表
|
|
140
|
+
CREATE INDEX idx_orders_user_summary ON orders(user_id)
|
|
141
|
+
INCLUDE (status, total_amount, created_at);
|
|
142
|
+
|
|
143
|
+
-- 使用部分索引减少索引体积
|
|
144
|
+
CREATE INDEX idx_orders_pending ON orders(created_at DESC)
|
|
145
|
+
WHERE status = 'pending';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### 检测方法
|
|
149
|
+
- `EXPLAIN ANALYZE` 输出中出现 `Seq Scan` 且 `rows` > 1000。
|
|
150
|
+
- PostgreSQL:`pg_stat_user_tables` 的 `seq_scan` 计数持续增长。
|
|
151
|
+
- MySQL:`SHOW INDEX FROM table_name` 检查是否覆盖高频查询列。
|
|
152
|
+
- 慢查询日志:执行时间 > 100ms 的 SQL 逐条分析执行计划。
|
|
153
|
+
|
|
154
|
+
### 修复步骤
|
|
155
|
+
1. 收集慢查询日志,列出 Top 20 慢 SQL。
|
|
156
|
+
2. 对每条慢 SQL 执行 `EXPLAIN ANALYZE`,识别全表扫描。
|
|
157
|
+
3. 根据查询模式创建合适的索引(单列 / 复合 / 部分 / 覆盖)。
|
|
158
|
+
4. 创建索引后重新执行 `EXPLAIN ANALYZE` 确认已使用索引。
|
|
159
|
+
5. 监控索引使用率,删除从未使用的冗余索引。
|
|
160
|
+
|
|
161
|
+
### Agent Checklist
|
|
162
|
+
- [ ] 所有 WHERE 条件列有索引(或复合索引的前缀)
|
|
163
|
+
- [ ] JOIN 的外键列有索引
|
|
164
|
+
- [ ] ORDER BY 列包含在索引中
|
|
165
|
+
- [ ] 无未使用的冗余索引
|
|
166
|
+
- [ ] 大表(> 100 万行)的高频查询使用覆盖索引
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## 3. SELECT *(过度获取)
|
|
171
|
+
|
|
172
|
+
### 描述
|
|
173
|
+
查询时使用 `SELECT *` 获取所有列,即使只需要其中 2-3 列。导致网络传输量增大、内存占用增加、无法使用覆盖索引,且表结构变更时可能引入意外的列。
|
|
174
|
+
|
|
175
|
+
### 错误示例
|
|
176
|
+
```python
|
|
177
|
+
# 只需要用户名和邮箱,却获取了所有列(包括大文本、二进制字段)
|
|
178
|
+
def get_user_list():
|
|
179
|
+
cursor.execute("SELECT * FROM users") # 包含 avatar_blob、bio_text 等大字段
|
|
180
|
+
return cursor.fetchall()
|
|
181
|
+
|
|
182
|
+
# ORM 中同样的问题
|
|
183
|
+
users = User.objects.all() # 加载了所有字段
|
|
184
|
+
names = [u.name for u in users] # 只用了 name
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### 正确示例
|
|
188
|
+
```python
|
|
189
|
+
# 明确指定所需列
|
|
190
|
+
def get_user_list():
|
|
191
|
+
cursor.execute("SELECT id, name, email FROM users")
|
|
192
|
+
return cursor.fetchall()
|
|
193
|
+
|
|
194
|
+
# ORM 中使用 values / only
|
|
195
|
+
users = User.objects.values("id", "name", "email")
|
|
196
|
+
|
|
197
|
+
# SQLAlchemy 使用 load_only
|
|
198
|
+
users = session.query(User).options(load_only(User.id, User.name, User.email)).all()
|
|
199
|
+
|
|
200
|
+
# 对于大字段,使用 defer 延迟加载
|
|
201
|
+
users = User.objects.defer("avatar_blob", "bio_text").all()
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### 检测方法
|
|
205
|
+
- SQL Review 中搜索 `SELECT *` 或 `SELECT table.*`。
|
|
206
|
+
- ORM 查询日志中查找未使用 `only()` / `values()` / `load_only()` 的查询。
|
|
207
|
+
- 使用 `sqlfluff` lint 工具自动检测 `SELECT *`。
|
|
208
|
+
|
|
209
|
+
### 修复步骤
|
|
210
|
+
1. 审查所有 `SELECT *` 查询,确定实际需要的列。
|
|
211
|
+
2. 替换为明确的列名列表。
|
|
212
|
+
3. 对包含 BLOB / TEXT 大字段的表,设置 ORM 默认 defer。
|
|
213
|
+
4. 在 CI 中加入 `sqlfluff` 检查,禁止 `SELECT *` 进入主分支。
|
|
214
|
+
|
|
215
|
+
### Agent Checklist
|
|
216
|
+
- [ ] 无 `SELECT *` 查询
|
|
217
|
+
- [ ] ORM 查询使用 `only()` / `values()` / `load_only()`
|
|
218
|
+
- [ ] 大字段使用 `defer()` 延迟加载
|
|
219
|
+
- [ ] CI 包含 SQL lint 规则
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## 4. 过度范式化(Over-Normalization)
|
|
224
|
+
|
|
225
|
+
### 描述
|
|
226
|
+
将数据拆分到过多的表中以追求完美的范式化,导致简单的读取操作需要 JOIN 5-10 张表,查询复杂且性能低下。在读多写少的场景下,适度反范式化是合理的。
|
|
227
|
+
|
|
228
|
+
### 错误示例
|
|
229
|
+
```sql
|
|
230
|
+
-- 过度拆分:一个用户资料需要 JOIN 6 张表
|
|
231
|
+
SELECT u.id, un.first_name, un.last_name, ue.email,
|
|
232
|
+
up.phone, ua.street, ua.city, uc.country_name
|
|
233
|
+
FROM users u
|
|
234
|
+
JOIN user_names un ON u.name_id = un.id
|
|
235
|
+
JOIN user_emails ue ON u.email_id = ue.id
|
|
236
|
+
JOIN user_phones up ON u.phone_id = up.id
|
|
237
|
+
JOIN user_addresses ua ON u.address_id = ua.id
|
|
238
|
+
JOIN countries uc ON ua.country_id = uc.id;
|
|
239
|
+
|
|
240
|
+
-- 甚至连状态都拆成了单独的表
|
|
241
|
+
SELECT os.status_name
|
|
242
|
+
FROM orders o
|
|
243
|
+
JOIN order_statuses os ON o.status_id = os.id
|
|
244
|
+
WHERE o.id = 123;
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### 正确示例
|
|
248
|
+
```sql
|
|
249
|
+
-- 适度反范式化:将高频读取的字段内联
|
|
250
|
+
CREATE TABLE users (
|
|
251
|
+
id SERIAL PRIMARY KEY,
|
|
252
|
+
first_name VARCHAR(50) NOT NULL,
|
|
253
|
+
last_name VARCHAR(50) NOT NULL,
|
|
254
|
+
email VARCHAR(255) NOT NULL UNIQUE,
|
|
255
|
+
phone VARCHAR(20),
|
|
256
|
+
street VARCHAR(200),
|
|
257
|
+
city VARCHAR(100),
|
|
258
|
+
country_code CHAR(2) NOT NULL -- ISO 代码,不需要 JOIN 国家表
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
-- 使用枚举而非外键表
|
|
262
|
+
CREATE TYPE order_status AS ENUM ('pending', 'paid', 'shipped', 'delivered', 'cancelled');
|
|
263
|
+
|
|
264
|
+
CREATE TABLE orders (
|
|
265
|
+
id SERIAL PRIMARY KEY,
|
|
266
|
+
user_id INTEGER REFERENCES users(id),
|
|
267
|
+
status order_status NOT NULL DEFAULT 'pending'
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
-- 对于需要分析的场景,使用物化视图
|
|
271
|
+
CREATE MATERIALIZED VIEW user_order_summary AS
|
|
272
|
+
SELECT u.id, u.first_name, u.last_name, COUNT(o.id) AS order_count,
|
|
273
|
+
SUM(o.total) AS total_spent
|
|
274
|
+
FROM users u LEFT JOIN orders o ON u.id = o.user_id
|
|
275
|
+
GROUP BY u.id, u.first_name, u.last_name;
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### 检测方法
|
|
279
|
+
- 单个查询 JOIN 超过 3 张表。
|
|
280
|
+
- 存在只包含 `id` + `name` 两列的"字典表"且数据量 < 100 条。
|
|
281
|
+
- `EXPLAIN ANALYZE` 显示多层 Nested Loop Join 导致性能退化。
|
|
282
|
+
|
|
283
|
+
### 修复步骤
|
|
284
|
+
1. 分析查询日志,找出 JOIN 数量最多的 Top 10 查询。
|
|
285
|
+
2. 评估哪些 JOIN 表是"字典表"(数据量小、变更频率低)。
|
|
286
|
+
3. 将字典表的值内联为主表的枚举列或 VARCHAR 列。
|
|
287
|
+
4. 对高频聚合查询使用物化视图。
|
|
288
|
+
5. 使用数据库迁移脚本执行反范式化,确保数据一致性。
|
|
289
|
+
|
|
290
|
+
### Agent Checklist
|
|
291
|
+
- [ ] 单个查询 JOIN <= 3 张表
|
|
292
|
+
- [ ] 数据量 < 100 的字典表考虑用枚举替代
|
|
293
|
+
- [ ] 读多写少的场景允许适度冗余
|
|
294
|
+
- [ ] 聚合查询使用物化视图或缓存
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
## 5. 不用事务(Missing Transactions)
|
|
299
|
+
|
|
300
|
+
### 描述
|
|
301
|
+
涉及多表写入的业务操作未使用事务,导致中途失败时数据处于不一致的中间状态。例如扣款成功但订单创建失败,用户余额减少但没有对应订单。
|
|
302
|
+
|
|
303
|
+
### 错误示例
|
|
304
|
+
```python
|
|
305
|
+
def transfer_money(from_id, to_id, amount):
|
|
306
|
+
# 无事务:如果第二步失败,钱已经从 from 账户扣除但未到 to 账户
|
|
307
|
+
db.execute(
|
|
308
|
+
"UPDATE accounts SET balance = balance - %s WHERE id = %s",
|
|
309
|
+
(amount, from_id)
|
|
310
|
+
)
|
|
311
|
+
# 如果这里抛异常,钱就消失了
|
|
312
|
+
db.execute(
|
|
313
|
+
"UPDATE accounts SET balance = balance + %s WHERE id = %s",
|
|
314
|
+
(amount, to_id)
|
|
315
|
+
)
|
|
316
|
+
db.execute(
|
|
317
|
+
"INSERT INTO transfers (from_id, to_id, amount) VALUES (%s, %s, %s)",
|
|
318
|
+
(from_id, to_id, amount)
|
|
319
|
+
)
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### 正确示例
|
|
323
|
+
```python
|
|
324
|
+
def transfer_money(from_id: int, to_id: int, amount: Decimal) -> Transfer:
|
|
325
|
+
with db.transaction() as tx:
|
|
326
|
+
# 加行锁防止并发问题
|
|
327
|
+
from_account = tx.execute(
|
|
328
|
+
"SELECT * FROM accounts WHERE id = %s FOR UPDATE", (from_id,)
|
|
329
|
+
).fetchone()
|
|
330
|
+
|
|
331
|
+
if from_account["balance"] < amount:
|
|
332
|
+
raise InsufficientBalanceError(from_id, amount)
|
|
333
|
+
|
|
334
|
+
tx.execute(
|
|
335
|
+
"UPDATE accounts SET balance = balance - %s WHERE id = %s",
|
|
336
|
+
(amount, from_id),
|
|
337
|
+
)
|
|
338
|
+
tx.execute(
|
|
339
|
+
"UPDATE accounts SET balance = balance + %s WHERE id = %s",
|
|
340
|
+
(amount, to_id),
|
|
341
|
+
)
|
|
342
|
+
transfer = tx.execute(
|
|
343
|
+
"INSERT INTO transfers (from_id, to_id, amount, status) "
|
|
344
|
+
"VALUES (%s, %s, %s, 'completed') RETURNING *",
|
|
345
|
+
(from_id, to_id, amount),
|
|
346
|
+
).fetchone()
|
|
347
|
+
|
|
348
|
+
return Transfer(**transfer)
|
|
349
|
+
# 事务自动 commit;异常时自动 rollback
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
```python
|
|
353
|
+
# Django ORM
|
|
354
|
+
from django.db import transaction
|
|
355
|
+
|
|
356
|
+
@transaction.atomic
|
|
357
|
+
def transfer_money(from_id: int, to_id: int, amount: Decimal) -> Transfer:
|
|
358
|
+
from_account = Account.objects.select_for_update().get(id=from_id)
|
|
359
|
+
to_account = Account.objects.select_for_update().get(id=to_id)
|
|
360
|
+
|
|
361
|
+
if from_account.balance < amount:
|
|
362
|
+
raise InsufficientBalanceError(from_id, amount)
|
|
363
|
+
|
|
364
|
+
from_account.balance -= amount
|
|
365
|
+
from_account.save()
|
|
366
|
+
to_account.balance += amount
|
|
367
|
+
to_account.save()
|
|
368
|
+
|
|
369
|
+
return Transfer.objects.create(
|
|
370
|
+
from_account=from_account, to_account=to_account, amount=amount
|
|
371
|
+
)
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### 检测方法
|
|
375
|
+
- 搜索代码中连续多条 `INSERT` / `UPDATE` / `DELETE` 且无事务包裹。
|
|
376
|
+
- ORM 中连续多个 `.save()` 调用不在 `transaction.atomic` 内。
|
|
377
|
+
- Code Review 中检查涉及资金、库存、状态流转的代码路径。
|
|
378
|
+
|
|
379
|
+
### 修复步骤
|
|
380
|
+
1. 梳理所有涉及多表写入的业务操作。
|
|
381
|
+
2. 为每个操作添加事务包裹(`BEGIN ... COMMIT / ROLLBACK`)。
|
|
382
|
+
3. 对需要防止并发写入的场景添加行锁(`SELECT ... FOR UPDATE`)。
|
|
383
|
+
4. 编写测试模拟中途失败场景,验证事务回滚正确。
|
|
384
|
+
|
|
385
|
+
### Agent Checklist
|
|
386
|
+
- [ ] 多表写入操作包裹在事务中
|
|
387
|
+
- [ ] 资金/库存操作使用 `SELECT ... FOR UPDATE` 行锁
|
|
388
|
+
- [ ] 事务异常时自动 rollback
|
|
389
|
+
- [ ] 有中途失败场景的回滚测试
|
|
390
|
+
|
|
391
|
+
---
|
|
392
|
+
|
|
393
|
+
## 6. 硬删除(Hard Delete)
|
|
394
|
+
|
|
395
|
+
### 描述
|
|
396
|
+
直接使用 `DELETE` 物理删除数据,导致无法审计、无法恢复误删数据、外键约束可能级联删除关联数据。在合规场景下(金融、医疗),硬删除可能违反法规要求。
|
|
397
|
+
|
|
398
|
+
### 错误示例
|
|
399
|
+
```python
|
|
400
|
+
def delete_user(user_id):
|
|
401
|
+
# 物理删除:数据永久丢失,关联数据可能级联删除
|
|
402
|
+
db.execute("DELETE FROM user_addresses WHERE user_id = %s", (user_id,))
|
|
403
|
+
db.execute("DELETE FROM user_orders WHERE user_id = %s", (user_id,))
|
|
404
|
+
db.execute("DELETE FROM users WHERE id = %s", (user_id,))
|
|
405
|
+
|
|
406
|
+
# ORM 中同样的问题
|
|
407
|
+
user = User.objects.get(id=user_id)
|
|
408
|
+
user.delete() # 级联删除所有关联数据
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
### 正确示例
|
|
412
|
+
```python
|
|
413
|
+
# 软删除模型
|
|
414
|
+
class SoftDeleteMixin:
|
|
415
|
+
deleted_at = Column(DateTime, nullable=True, index=True)
|
|
416
|
+
deleted_by = Column(Integer, nullable=True)
|
|
417
|
+
|
|
418
|
+
@hybrid_property
|
|
419
|
+
def is_deleted(self):
|
|
420
|
+
return self.deleted_at is not None
|
|
421
|
+
|
|
422
|
+
class User(Base, SoftDeleteMixin):
|
|
423
|
+
__tablename__ = "users"
|
|
424
|
+
id = Column(Integer, primary_key=True)
|
|
425
|
+
name = Column(String(100))
|
|
426
|
+
email = Column(String(255))
|
|
427
|
+
|
|
428
|
+
# 软删除操作
|
|
429
|
+
def soft_delete_user(user_id: int, operator_id: int) -> None:
|
|
430
|
+
with db.transaction() as tx:
|
|
431
|
+
tx.execute(
|
|
432
|
+
"UPDATE users SET deleted_at = NOW(), deleted_by = %s WHERE id = %s",
|
|
433
|
+
(operator_id, user_id),
|
|
434
|
+
)
|
|
435
|
+
# 记录审计日志
|
|
436
|
+
tx.execute(
|
|
437
|
+
"INSERT INTO audit_log (entity, entity_id, action, operator_id) "
|
|
438
|
+
"VALUES ('user', %s, 'soft_delete', %s)",
|
|
439
|
+
(user_id, operator_id),
|
|
440
|
+
)
|
|
441
|
+
|
|
442
|
+
# 查询时自动过滤已删除数据
|
|
443
|
+
def get_active_users():
|
|
444
|
+
return db.execute("SELECT * FROM users WHERE deleted_at IS NULL").fetchall()
|
|
445
|
+
|
|
446
|
+
# Django 软删除 Manager
|
|
447
|
+
class ActiveManager(models.Manager):
|
|
448
|
+
def get_queryset(self):
|
|
449
|
+
return super().get_queryset().filter(deleted_at__isnull=True)
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
### 检测方法
|
|
453
|
+
- 搜索代码中的 `DELETE FROM` 和 `.delete()` 调用。
|
|
454
|
+
- 检查表结构是否包含 `deleted_at` / `is_deleted` 列。
|
|
455
|
+
- 数据库审计日志中是否记录了删除操作。
|
|
456
|
+
|
|
457
|
+
### 修复步骤
|
|
458
|
+
1. 为需要软删除的表添加 `deleted_at`、`deleted_by` 列。
|
|
459
|
+
2. 创建软删除 Mixin / 基类,统一软删除逻辑。
|
|
460
|
+
3. 修改所有查询,添加 `WHERE deleted_at IS NULL` 条件(或使用自定义 Manager)。
|
|
461
|
+
4. 将 `DELETE` 语句改为 `UPDATE ... SET deleted_at = NOW()`。
|
|
462
|
+
5. 添加定期清理任务,对超过保留期的软删除数据进行物理删除。
|
|
463
|
+
|
|
464
|
+
### Agent Checklist
|
|
465
|
+
- [ ] 业务表使用软删除(`deleted_at` 列)
|
|
466
|
+
- [ ] 无直接 `DELETE FROM` 语句(除定期清理任务)
|
|
467
|
+
- [ ] 查询默认过滤已删除数据
|
|
468
|
+
- [ ] 删除操作记录审计日志
|
|
469
|
+
|
|
470
|
+
---
|
|
471
|
+
|
|
472
|
+
## 7. 无分页(Missing Pagination)
|
|
473
|
+
|
|
474
|
+
### 描述
|
|
475
|
+
查询接口不限制返回数量,一次性返回全部数据。在数据量增长后导致内存溢出、响应超时、网络带宽耗尽。
|
|
476
|
+
|
|
477
|
+
### 错误示例
|
|
478
|
+
```python
|
|
479
|
+
# 返回所有订单 -- 数据量增长后直接 OOM
|
|
480
|
+
@app.get("/orders")
|
|
481
|
+
def list_orders():
|
|
482
|
+
orders = Order.objects.all() # 可能有几百万条
|
|
483
|
+
return {"orders": [serialize(o) for o in orders]}
|
|
484
|
+
|
|
485
|
+
# API 无分页参数
|
|
486
|
+
@app.get("/users")
|
|
487
|
+
def list_users(status: str = None):
|
|
488
|
+
query = "SELECT * FROM users"
|
|
489
|
+
if status:
|
|
490
|
+
query += f" WHERE status = '{status}'" # 还有 SQL 注入风险
|
|
491
|
+
return db.execute(query).fetchall()
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
### 正确示例
|
|
495
|
+
```python
|
|
496
|
+
from fastapi import Query
|
|
497
|
+
|
|
498
|
+
@app.get("/orders")
|
|
499
|
+
def list_orders(
|
|
500
|
+
page: int = Query(1, ge=1, description="页码"),
|
|
501
|
+
page_size: int = Query(20, ge=1, le=100, description="每页数量"),
|
|
502
|
+
status: str | None = Query(None, description="订单状态过滤"),
|
|
503
|
+
):
|
|
504
|
+
query = Order.objects.all()
|
|
505
|
+
if status:
|
|
506
|
+
query = query.filter(status=status)
|
|
507
|
+
|
|
508
|
+
total = query.count()
|
|
509
|
+
offset = (page - 1) * page_size
|
|
510
|
+
orders = query.order_by("-created_at")[offset : offset + page_size]
|
|
511
|
+
|
|
512
|
+
return {
|
|
513
|
+
"data": [serialize(o) for o in orders],
|
|
514
|
+
"pagination": {
|
|
515
|
+
"page": page,
|
|
516
|
+
"page_size": page_size,
|
|
517
|
+
"total": total,
|
|
518
|
+
"total_pages": (total + page_size - 1) // page_size,
|
|
519
|
+
},
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
# 对于大数据量,使用游标分页(keyset pagination)
|
|
523
|
+
@app.get("/orders/cursor")
|
|
524
|
+
def list_orders_cursor(
|
|
525
|
+
after: str | None = Query(None, description="上一页最后一条的游标"),
|
|
526
|
+
limit: int = Query(20, ge=1, le=100),
|
|
527
|
+
):
|
|
528
|
+
query = Order.objects.all().order_by("-created_at")
|
|
529
|
+
if after:
|
|
530
|
+
cursor_date = decode_cursor(after)
|
|
531
|
+
query = query.filter(created_at__lt=cursor_date)
|
|
532
|
+
orders = list(query[:limit + 1])
|
|
533
|
+
|
|
534
|
+
has_next = len(orders) > limit
|
|
535
|
+
orders = orders[:limit]
|
|
536
|
+
next_cursor = encode_cursor(orders[-1].created_at) if has_next else None
|
|
537
|
+
|
|
538
|
+
return {
|
|
539
|
+
"data": [serialize(o) for o in orders],
|
|
540
|
+
"next_cursor": next_cursor,
|
|
541
|
+
"has_next": has_next,
|
|
542
|
+
}
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
### 检测方法
|
|
546
|
+
- API 接口无 `page` / `limit` / `cursor` 参数。
|
|
547
|
+
- ORM 查询无 `LIMIT` 子句。
|
|
548
|
+
- 响应 JSON 中无分页元数据(`total`、`page`、`next_cursor`)。
|
|
549
|
+
- 负载测试:随数据量增长,响应时间线性增加。
|
|
550
|
+
|
|
551
|
+
### 修复步骤
|
|
552
|
+
1. 为所有列表接口添加分页参数(`page` + `page_size` 或 `cursor` + `limit`)。
|
|
553
|
+
2. 设置 `page_size` 上限(通常 100),防止客户端请求过大。
|
|
554
|
+
3. 返回分页元数据(总数、总页数、下一页游标)。
|
|
555
|
+
4. 对超过 10 万条数据的表,使用游标分页替代偏移量分页。
|
|
556
|
+
5. 添加集成测试验证分页逻辑正确性。
|
|
557
|
+
|
|
558
|
+
### Agent Checklist
|
|
559
|
+
- [ ] 所有列表接口包含分页参数
|
|
560
|
+
- [ ] `page_size` 有上限(<= 100)
|
|
561
|
+
- [ ] 响应包含分页元数据
|
|
562
|
+
- [ ] 大数据量场景使用游标分页
|
|
563
|
+
- [ ] ORM 查询包含 `LIMIT` 子句
|
|
564
|
+
|
|
565
|
+
---
|
|
566
|
+
|
|
567
|
+
## 全局 Agent Checklist
|
|
568
|
+
|
|
569
|
+
| 检查项 | 阈值 | 工具 |
|
|
570
|
+
|--------|------|------|
|
|
571
|
+
| 列表接口查询次数 | <= 5 | Django Debug Toolbar / APM |
|
|
572
|
+
| N+1 查询 | 0 个 | `nplusone` / SQL 日志 |
|
|
573
|
+
| 缺失索引 | 0 个 | `EXPLAIN ANALYZE` / `pg_stat` |
|
|
574
|
+
| `SELECT *` 使用 | 0 处 | `sqlfluff` / Code Review |
|
|
575
|
+
| 单查询 JOIN 数 | <= 3 | `EXPLAIN ANALYZE` |
|
|
576
|
+
| 无事务多表写入 | 0 处 | Code Review |
|
|
577
|
+
| 硬删除语句 | 0 条 | Code Review / `grep DELETE` |
|
|
578
|
+
| 无分页列表接口 | 0 个 | API 文档审查 |
|