@zimezone/z-command 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (282) hide show
  1. package/README.md +13 -1
  2. package/dist/commands/init.d.ts.map +1 -1
  3. package/dist/commands/init.js +34 -9
  4. package/dist/commands/init.js.map +1 -1
  5. package/package.json +8 -3
  6. package/templates.zip +0 -0
  7. package/templates/agents/accessibility-expert.agent.md +0 -56
  8. package/templates/agents/ai-engineer.agent.md +0 -61
  9. package/templates/agents/angular-architect.agent.md +0 -49
  10. package/templates/agents/api-designer.agent.md +0 -40
  11. package/templates/agents/api-documenter.agent.md +0 -161
  12. package/templates/agents/architect-review.agent.md +0 -146
  13. package/templates/agents/arm-cortex-expert.agent.md +0 -288
  14. package/templates/agents/azure-infra-engineer.agent.md +0 -57
  15. package/templates/agents/backend-architect.agent.md +0 -309
  16. package/templates/agents/backend-developer.agent.md +0 -61
  17. package/templates/agents/backend-security-coder.agent.md +0 -152
  18. package/templates/agents/bash-pro.agent.md +0 -285
  19. package/templates/agents/blockchain-developer.agent.md +0 -57
  20. package/templates/agents/build-engineer.agent.md +0 -56
  21. package/templates/agents/business-analyst.agent.md +0 -47
  22. package/templates/agents/c-pro.agent.md +0 -35
  23. package/templates/agents/c4-code.agent.md +0 -320
  24. package/templates/agents/c4-component.agent.md +0 -227
  25. package/templates/agents/c4-container.agent.md +0 -248
  26. package/templates/agents/c4-context.agent.md +0 -235
  27. package/templates/agents/cli-developer.agent.md +0 -57
  28. package/templates/agents/cloud-architect.agent.md +0 -56
  29. package/templates/agents/code-architect.agent.md +0 -63
  30. package/templates/agents/code-reviewer.agent.md +0 -49
  31. package/templates/agents/competitive-analyst.agent.md +0 -48
  32. package/templates/agents/conductor-validator.agent.md +0 -245
  33. package/templates/agents/context-manager.agent.md +0 -55
  34. package/templates/agents/cpp-pro.agent.md +0 -59
  35. package/templates/agents/csharp-developer.agent.md +0 -57
  36. package/templates/agents/csharp-pro.agent.md +0 -38
  37. package/templates/agents/customer-support.agent.md +0 -148
  38. package/templates/agents/data-engineer.agent.md +0 -55
  39. package/templates/agents/data-researcher.agent.md +0 -55
  40. package/templates/agents/data-scientist.agent.md +0 -56
  41. package/templates/agents/database-admin.agent.md +0 -142
  42. package/templates/agents/database-administrator.agent.md +0 -50
  43. package/templates/agents/database-architect.agent.md +0 -238
  44. package/templates/agents/database-optimizer.agent.md +0 -144
  45. package/templates/agents/debugger.agent.md +0 -30
  46. package/templates/agents/deployment-engineer.agent.md +0 -0
  47. package/templates/agents/devops-engineer.agent.md +0 -59
  48. package/templates/agents/devops-troubleshooter.agent.md +0 -138
  49. package/templates/agents/django-developer.agent.md +0 -50
  50. package/templates/agents/django-pro.agent.md +0 -159
  51. package/templates/agents/docs-architect.agent.md +0 -77
  52. package/templates/agents/documentation-engineer.agent.md +0 -57
  53. package/templates/agents/dotnet-architect.agent.md +0 -175
  54. package/templates/agents/dx-optimizer.agent.md +0 -63
  55. package/templates/agents/electron-pro.agent.md +0 -56
  56. package/templates/agents/elixir-pro.agent.md +0 -38
  57. package/templates/agents/embedded-systems.agent.md +0 -55
  58. package/templates/agents/error-detective.agent.md +0 -32
  59. package/templates/agents/event-sourcing-architect.agent.md +0 -42
  60. package/templates/agents/fastapi-pro.agent.md +0 -171
  61. package/templates/agents/fintech-engineer.agent.md +0 -57
  62. package/templates/agents/firmware-analyst.agent.md +0 -330
  63. package/templates/agents/flutter-expert.agent.md +0 -50
  64. package/templates/agents/frontend-developer.agent.md +0 -59
  65. package/templates/agents/frontend-security-coder.agent.md +0 -149
  66. package/templates/agents/fullstack-developer.agent.md +0 -46
  67. package/templates/agents/git-workflow-manager.agent.md +0 -57
  68. package/templates/agents/golang-pro.agent.md +0 -50
  69. package/templates/agents/graphql-architect.agent.md +0 -48
  70. package/templates/agents/haskell-pro.agent.md +0 -37
  71. package/templates/agents/hr-pro.agent.md +0 -105
  72. package/templates/agents/incident-responder.agent.md +0 -190
  73. package/templates/agents/ios-developer.agent.md +0 -198
  74. package/templates/agents/iot-engineer.agent.md +0 -56
  75. package/templates/agents/java-architect.agent.md +0 -48
  76. package/templates/agents/java-pro.agent.md +0 -156
  77. package/templates/agents/javascript-pro.agent.md +0 -35
  78. package/templates/agents/julia-pro.agent.md +0 -187
  79. package/templates/agents/kotlin-specialist.agent.md +0 -50
  80. package/templates/agents/laravel-specialist.agent.md +0 -50
  81. package/templates/agents/legacy-modernizer.agent.md +0 -56
  82. package/templates/agents/legal-advisor.agent.md +0 -49
  83. package/templates/agents/llm-architect.agent.md +0 -58
  84. package/templates/agents/malware-analyst.agent.md +0 -272
  85. package/templates/agents/mcp-developer.agent.md +0 -54
  86. package/templates/agents/mermaid-expert.agent.md +0 -39
  87. package/templates/agents/microservices-architect.agent.md +0 -47
  88. package/templates/agents/minecraft-bukkit-pro.agent.md +0 -104
  89. package/templates/agents/ml-engineer.agent.md +0 -56
  90. package/templates/agents/mlops-engineer.agent.md +0 -56
  91. package/templates/agents/mobile-developer.agent.md +0 -45
  92. package/templates/agents/mobile-security-coder.agent.md +0 -163
  93. package/templates/agents/monorepo-architect.agent.md +0 -44
  94. package/templates/agents/multi-agent-coordinator.agent.md +0 -55
  95. package/templates/agents/network-engineer.agent.md +0 -57
  96. package/templates/agents/nextjs-developer.agent.md +0 -48
  97. package/templates/agents/nlp-engineer.agent.md +0 -58
  98. package/templates/agents/observability-engineer.agent.md +0 -228
  99. package/templates/agents/payment-integration.agent.md +0 -56
  100. package/templates/agents/performance-engineer.agent.md +0 -167
  101. package/templates/agents/performance-optimizer.agent.md +0 -57
  102. package/templates/agents/php-pro.agent.md +0 -43
  103. package/templates/agents/platform-engineer.agent.md +0 -57
  104. package/templates/agents/posix-shell-pro.agent.md +0 -284
  105. package/templates/agents/postgres-pro.agent.md +0 -58
  106. package/templates/agents/product-manager.agent.md +0 -55
  107. package/templates/agents/project-manager.agent.md +0 -57
  108. package/templates/agents/prompt-engineer.agent.md +0 -58
  109. package/templates/agents/python-pro.agent.md +0 -48
  110. package/templates/agents/quant-analyst.agent.md +0 -32
  111. package/templates/agents/rails-expert.agent.md +0 -50
  112. package/templates/agents/react-specialist.agent.md +0 -49
  113. package/templates/agents/refactoring-specialist.agent.md +0 -56
  114. package/templates/agents/reference-builder.agent.md +0 -167
  115. package/templates/agents/research-analyst.agent.md +0 -63
  116. package/templates/agents/reverse-engineer.agent.md +0 -202
  117. package/templates/agents/risk-manager.agent.md +0 -41
  118. package/templates/agents/ruby-pro.agent.md +0 -35
  119. package/templates/agents/rust-pro.agent.md +0 -156
  120. package/templates/agents/sales-automator.agent.md +0 -35
  121. package/templates/agents/scala-pro.agent.md +0 -60
  122. package/templates/agents/scrum-master.agent.md +0 -54
  123. package/templates/agents/search-specialist.agent.md +0 -59
  124. package/templates/agents/security-analyst.agent.md +0 -57
  125. package/templates/agents/security-auditor.agent.md +0 -138
  126. package/templates/agents/security-engineer.agent.md +0 -57
  127. package/templates/agents/seo-authority-builder.agent.md +0 -116
  128. package/templates/agents/seo-cannibalization-detector.agent.md +0 -103
  129. package/templates/agents/seo-content-auditor.agent.md +0 -63
  130. package/templates/agents/seo-content-planner.agent.md +0 -88
  131. package/templates/agents/seo-content-refresher.agent.md +0 -98
  132. package/templates/agents/seo-content-writer.agent.md +0 -76
  133. package/templates/agents/seo-keyword-strategist.agent.md +0 -75
  134. package/templates/agents/seo-meta-optimizer.agent.md +0 -72
  135. package/templates/agents/seo-snippet-hunter.agent.md +0 -94
  136. package/templates/agents/seo-specialist.agent.md +0 -57
  137. package/templates/agents/seo-structure-architect.agent.md +0 -88
  138. package/templates/agents/service-mesh-expert.agent.md +0 -41
  139. package/templates/agents/sql-pro.agent.md +0 -146
  140. package/templates/agents/sre-engineer.agent.md +0 -58
  141. package/templates/agents/swift-expert.agent.md +0 -49
  142. package/templates/agents/task-distributor.agent.md +0 -47
  143. package/templates/agents/tdd-orchestrator.agent.md +0 -183
  144. package/templates/agents/technical-writer.agent.md +0 -48
  145. package/templates/agents/temporal-python-pro.agent.md +0 -349
  146. package/templates/agents/terraform-engineer.agent.md +0 -57
  147. package/templates/agents/terraform-specialist.agent.md +0 -137
  148. package/templates/agents/test-automator.agent.md +0 -203
  149. package/templates/agents/test-engineer.agent.md +0 -55
  150. package/templates/agents/threat-modeling-expert.agent.md +0 -44
  151. package/templates/agents/trend-analyst.agent.md +0 -47
  152. package/templates/agents/tutorial-engineer.agent.md +0 -118
  153. package/templates/agents/typescript-pro.agent.md +0 -48
  154. package/templates/agents/ui-designer.agent.md +0 -48
  155. package/templates/agents/ui-ux-designer.agent.md +0 -188
  156. package/templates/agents/ui-visual-validator.agent.md +0 -192
  157. package/templates/agents/ux-researcher.agent.md +0 -48
  158. package/templates/agents/vector-database-engineer.agent.md +0 -43
  159. package/templates/agents/vue-expert.agent.md +0 -48
  160. package/templates/agents/websocket-engineer.agent.md +0 -49
  161. package/templates/agents/workflow-orchestrator.agent.md +0 -48
  162. package/templates/skills/angular-migration/SKILL.md +0 -410
  163. package/templates/skills/api-design-principles/SKILL.md +0 -528
  164. package/templates/skills/api-design-principles/assets/api-design-checklist.md +0 -155
  165. package/templates/skills/api-design-principles/assets/rest-api-template.py +0 -182
  166. package/templates/skills/api-design-principles/references/graphql-schema-design.md +0 -583
  167. package/templates/skills/api-design-principles/references/rest-best-practices.md +0 -408
  168. package/templates/skills/architecture-decision-records/SKILL.md +0 -428
  169. package/templates/skills/architecture-patterns/SKILL.md +0 -494
  170. package/templates/skills/async-python-patterns/SKILL.md +0 -694
  171. package/templates/skills/auth-implementation-patterns/SKILL.md +0 -634
  172. package/templates/skills/changelog-automation/SKILL.md +0 -552
  173. package/templates/skills/code-review/SKILL.md +0 -62
  174. package/templates/skills/code-review-excellence/SKILL.md +0 -520
  175. package/templates/skills/competitive-landscape/SKILL.md +0 -479
  176. package/templates/skills/context-driven-development/SKILL.md +0 -385
  177. package/templates/skills/cost-optimization/SKILL.md +0 -274
  178. package/templates/skills/cqrs-implementation/SKILL.md +0 -554
  179. package/templates/skills/data-quality-frameworks/SKILL.md +0 -587
  180. package/templates/skills/data-storytelling/SKILL.md +0 -453
  181. package/templates/skills/database-migration/SKILL.md +0 -424
  182. package/templates/skills/dbt-transformation-patterns/SKILL.md +0 -561
  183. package/templates/skills/debugging-strategies/SKILL.md +0 -527
  184. package/templates/skills/defi-protocol-templates/SKILL.md +0 -454
  185. package/templates/skills/dependency-upgrade/SKILL.md +0 -409
  186. package/templates/skills/deployment-pipeline-design/SKILL.md +0 -359
  187. package/templates/skills/distributed-tracing/SKILL.md +0 -438
  188. package/templates/skills/dotnet-backend-patterns/SKILL.md +0 -815
  189. package/templates/skills/dotnet-backend-patterns/assets/repository-template.cs +0 -523
  190. package/templates/skills/dotnet-backend-patterns/assets/service-template.cs +0 -336
  191. package/templates/skills/dotnet-backend-patterns/references/dapper-patterns.md +0 -544
  192. package/templates/skills/dotnet-backend-patterns/references/ef-core-best-practices.md +0 -355
  193. package/templates/skills/e2e-testing-patterns/SKILL.md +0 -547
  194. package/templates/skills/employment-contract-templates/SKILL.md +0 -507
  195. package/templates/skills/error-handling-patterns/SKILL.md +0 -636
  196. package/templates/skills/event-store-design/SKILL.md +0 -437
  197. package/templates/skills/fastapi-templates/SKILL.md +0 -567
  198. package/templates/skills/git-advanced-workflows/SKILL.md +0 -400
  199. package/templates/skills/github-actions-templates/SKILL.md +0 -333
  200. package/templates/skills/go-concurrency-patterns/SKILL.md +0 -655
  201. package/templates/skills/grafana-dashboards/SKILL.md +0 -369
  202. package/templates/skills/helm-chart-scaffolding/SKILL.md +0 -544
  203. package/templates/skills/helm-chart-scaffolding/assets/Chart.yaml.template +0 -42
  204. package/templates/skills/helm-chart-scaffolding/assets/values.yaml.template +0 -185
  205. package/templates/skills/helm-chart-scaffolding/references/chart-structure.md +0 -500
  206. package/templates/skills/helm-chart-scaffolding/scripts/validate-chart.sh +0 -244
  207. package/templates/skills/javascript-testing-patterns/SKILL.md +0 -1025
  208. package/templates/skills/langchain-architecture/SKILL.md +0 -338
  209. package/templates/skills/llm-evaluation/SKILL.md +0 -471
  210. package/templates/skills/microservices-patterns/SKILL.md +0 -595
  211. package/templates/skills/modern-javascript-patterns/SKILL.md +0 -911
  212. package/templates/skills/monorepo-management/SKILL.md +0 -622
  213. package/templates/skills/nextjs-app-router-patterns/SKILL.md +0 -544
  214. package/templates/skills/nodejs-backend-patterns/SKILL.md +0 -1020
  215. package/templates/skills/nx-workspace-patterns/SKILL.md +0 -452
  216. package/templates/skills/openapi-spec-generation/SKILL.md +0 -1028
  217. package/templates/skills/paypal-integration/SKILL.md +0 -467
  218. package/templates/skills/pci-compliance/SKILL.md +0 -466
  219. package/templates/skills/postgresql/SKILL.md +0 -204
  220. package/templates/skills/projection-patterns/SKILL.md +0 -490
  221. package/templates/skills/prometheus-configuration/SKILL.md +0 -392
  222. package/templates/skills/prompt-engineering-patterns/SKILL.md +0 -201
  223. package/templates/skills/prompt-engineering-patterns/assets/few-shot-examples.json +0 -106
  224. package/templates/skills/prompt-engineering-patterns/assets/prompt-template-library.md +0 -246
  225. package/templates/skills/prompt-engineering-patterns/references/chain-of-thought.md +0 -399
  226. package/templates/skills/prompt-engineering-patterns/references/few-shot-learning.md +0 -369
  227. package/templates/skills/prompt-engineering-patterns/references/prompt-optimization.md +0 -414
  228. package/templates/skills/prompt-engineering-patterns/references/prompt-templates.md +0 -470
  229. package/templates/skills/prompt-engineering-patterns/references/system-prompts.md +0 -189
  230. package/templates/skills/prompt-engineering-patterns/scripts/optimize-prompt.py +0 -279
  231. package/templates/skills/python-packaging/SKILL.md +0 -870
  232. package/templates/skills/python-performance-optimization/SKILL.md +0 -869
  233. package/templates/skills/python-testing-patterns/SKILL.md +0 -907
  234. package/templates/skills/rag-implementation/SKILL.md +0 -403
  235. package/templates/skills/react-modernization/SKILL.md +0 -513
  236. package/templates/skills/react-native-architecture/SKILL.md +0 -671
  237. package/templates/skills/react-state-management/SKILL.md +0 -429
  238. package/templates/skills/risk-metrics-calculation/SKILL.md +0 -555
  239. package/templates/skills/rust-async-patterns/SKILL.md +0 -517
  240. package/templates/skills/secrets-management/SKILL.md +0 -346
  241. package/templates/skills/security-requirement-extraction/SKILL.md +0 -677
  242. package/templates/skills/security-review/SKILL.md +0 -78
  243. package/templates/skills/shellcheck-configuration/SKILL.md +0 -454
  244. package/templates/skills/similarity-search-patterns/SKILL.md +0 -558
  245. package/templates/skills/slo-implementation/SKILL.md +0 -329
  246. package/templates/skills/sql-optimization-patterns/SKILL.md +0 -493
  247. package/templates/skills/stripe-integration/SKILL.md +0 -442
  248. package/templates/skills/systematic-debugging/SKILL.md +0 -57
  249. package/templates/skills/tailwind-design-system/SKILL.md +0 -666
  250. package/templates/skills/temporal-python-testing/SKILL.md +0 -158
  251. package/templates/skills/temporal-python-testing/resources/integration-testing.md +0 -455
  252. package/templates/skills/temporal-python-testing/resources/local-setup.md +0 -553
  253. package/templates/skills/temporal-python-testing/resources/replay-testing.md +0 -462
  254. package/templates/skills/temporal-python-testing/resources/unit-testing.md +0 -328
  255. package/templates/skills/terraform-module-library/SKILL.md +0 -249
  256. package/templates/skills/terraform-module-library/references/aws-modules.md +0 -63
  257. package/templates/skills/test-driven-development/SKILL.md +0 -46
  258. package/templates/skills/threat-mitigation-mapping/SKILL.md +0 -745
  259. package/templates/skills/track-management/SKILL.md +0 -593
  260. package/templates/skills/typescript-advanced-types/SKILL.md +0 -717
  261. package/templates/skills/ui-ux-pro-max/SKILL.md +0 -352
  262. package/templates/skills/ui-ux-pro-max/data/charts.csv +0 -26
  263. package/templates/skills/ui-ux-pro-max/data/colors.csv +0 -97
  264. package/templates/skills/ui-ux-pro-max/data/icons.csv +0 -101
  265. package/templates/skills/ui-ux-pro-max/data/landing.csv +0 -31
  266. package/templates/skills/ui-ux-pro-max/data/products.csv +0 -97
  267. package/templates/skills/ui-ux-pro-max/data/prompts.csv +0 -24
  268. package/templates/skills/ui-ux-pro-max/data/react-performance.csv +0 -45
  269. package/templates/skills/ui-ux-pro-max/data/styles.csv +0 -59
  270. package/templates/skills/ui-ux-pro-max/data/typography.csv +0 -58
  271. package/templates/skills/ui-ux-pro-max/data/ui-reasoning.csv +0 -101
  272. package/templates/skills/ui-ux-pro-max/data/ux-guidelines.csv +0 -100
  273. package/templates/skills/ui-ux-pro-max/data/web-interface.csv +0 -31
  274. package/templates/skills/ui-ux-pro-max/scripts/core.py +0 -258
  275. package/templates/skills/ui-ux-pro-max/scripts/design_system.py +0 -547
  276. package/templates/skills/ui-ux-pro-max/scripts/search.py +0 -76
  277. package/templates/skills/uv-package-manager/SKILL.md +0 -831
  278. package/templates/skills/vector-index-tuning/SKILL.md +0 -521
  279. package/templates/skills/wcag-audit-patterns/SKILL.md +0 -555
  280. package/templates/skills/workflow-orchestration-patterns/SKILL.md +0 -316
  281. package/templates/skills/workflow-patterns/SKILL.md +0 -623
  282. package/templates/skills/writing-plans/SKILL.md +0 -64
@@ -1,466 +0,0 @@
1
- ---
2
- name: pci-compliance
3
- description: Implement PCI DSS compliance requirements for secure handling of payment card data and payment systems. Use when securing payment processing, achieving PCI compliance, or implementing payment card security measures.
4
- ---
5
-
6
- # PCI Compliance
7
-
8
- Master PCI DSS (Payment Card Industry Data Security Standard) compliance for secure payment processing and handling of cardholder data.
9
-
10
- ## When to Use This Skill
11
-
12
- - Building payment processing systems
13
- - Handling credit card information
14
- - Implementing secure payment flows
15
- - Conducting PCI compliance audits
16
- - Reducing PCI compliance scope
17
- - Implementing tokenization and encryption
18
- - Preparing for PCI DSS assessments
19
-
20
- ## PCI DSS Requirements (12 Core Requirements)
21
-
22
- ### Build and Maintain Secure Network
23
- 1. Install and maintain firewall configuration
24
- 2. Don't use vendor-supplied defaults for passwords
25
-
26
- ### Protect Cardholder Data
27
- 3. Protect stored cardholder data
28
- 4. Encrypt transmission of cardholder data across public networks
29
-
30
- ### Maintain Vulnerability Management
31
- 5. Protect systems against malware
32
- 6. Develop and maintain secure systems and applications
33
-
34
- ### Implement Strong Access Control
35
- 7. Restrict access to cardholder data by business need-to-know
36
- 8. Identify and authenticate access to system components
37
- 9. Restrict physical access to cardholder data
38
-
39
- ### Monitor and Test Networks
40
- 10. Track and monitor all access to network resources and cardholder data
41
- 11. Regularly test security systems and processes
42
-
43
- ### Maintain Information Security Policy
44
- 12. Maintain a policy that addresses information security
45
-
46
- ## Compliance Levels
47
-
48
- **Level 1**: > 6 million transactions/year (annual ROC required)
49
- **Level 2**: 1-6 million transactions/year (annual SAQ)
50
- **Level 3**: 20,000-1 million e-commerce transactions/year
51
- **Level 4**: < 20,000 e-commerce or < 1 million total transactions
52
-
53
- ## Data Minimization (Never Store)
54
-
55
- ```python
56
- # NEVER STORE THESE
57
- PROHIBITED_DATA = {
58
- 'full_track_data': 'Magnetic stripe data',
59
- 'cvv': 'Card verification code/value',
60
- 'pin': 'PIN or PIN block'
61
- }
62
-
63
- # CAN STORE (if encrypted)
64
- ALLOWED_DATA = {
65
- 'pan': 'Primary Account Number (card number)',
66
- 'cardholder_name': 'Name on card',
67
- 'expiration_date': 'Card expiration',
68
- 'service_code': 'Service code'
69
- }
70
-
71
- class PaymentData:
72
- """Safe payment data handling."""
73
-
74
- def __init__(self):
75
- self.prohibited_fields = ['cvv', 'cvv2', 'cvc', 'pin']
76
-
77
- def sanitize_log(self, data):
78
- """Remove sensitive data from logs."""
79
- sanitized = data.copy()
80
-
81
- # Mask PAN
82
- if 'card_number' in sanitized:
83
- card = sanitized['card_number']
84
- sanitized['card_number'] = f"{card[:6]}{'*' * (len(card) - 10)}{card[-4:]}"
85
-
86
- # Remove prohibited data
87
- for field in self.prohibited_fields:
88
- sanitized.pop(field, None)
89
-
90
- return sanitized
91
-
92
- def validate_no_prohibited_storage(self, data):
93
- """Ensure no prohibited data is being stored."""
94
- for field in self.prohibited_fields:
95
- if field in data:
96
- raise SecurityError(f"Attempting to store prohibited field: {field}")
97
- ```
98
-
99
- ## Tokenization
100
-
101
- ### Using Payment Processor Tokens
102
- ```python
103
- import stripe
104
-
105
- class TokenizedPayment:
106
- """Handle payments using tokens (no card data on server)."""
107
-
108
- @staticmethod
109
- def create_payment_method_token(card_details):
110
- """Create token from card details (client-side only)."""
111
- # THIS SHOULD ONLY BE DONE CLIENT-SIDE WITH STRIPE.JS
112
- # NEVER send card details to your server
113
-
114
- """
115
- // Frontend JavaScript
116
- const stripe = Stripe('pk_...');
117
-
118
- const {token, error} = await stripe.createToken({
119
- card: {
120
- number: '4242424242424242',
121
- exp_month: 12,
122
- exp_year: 2024,
123
- cvc: '123'
124
- }
125
- });
126
-
127
- // Send token.id to server (NOT card details)
128
- """
129
- pass
130
-
131
- @staticmethod
132
- def charge_with_token(token_id, amount):
133
- """Charge using token (server-side)."""
134
- # Your server only sees the token, never the card number
135
- stripe.api_key = "sk_..."
136
-
137
- charge = stripe.Charge.create(
138
- amount=amount,
139
- currency="usd",
140
- source=token_id, # Token instead of card details
141
- description="Payment"
142
- )
143
-
144
- return charge
145
-
146
- @staticmethod
147
- def store_payment_method(customer_id, payment_method_token):
148
- """Store payment method as token for future use."""
149
- stripe.Customer.modify(
150
- customer_id,
151
- source=payment_method_token
152
- )
153
-
154
- # Store only customer_id and payment_method_id in your database
155
- # NEVER store actual card details
156
- return {
157
- 'customer_id': customer_id,
158
- 'has_payment_method': True
159
- # DO NOT store: card number, CVV, etc.
160
- }
161
- ```
162
-
163
- ### Custom Tokenization (Advanced)
164
- ```python
165
- import secrets
166
- from cryptography.fernet import Fernet
167
-
168
- class TokenVault:
169
- """Secure token vault for card data (if you must store it)."""
170
-
171
- def __init__(self, encryption_key):
172
- self.cipher = Fernet(encryption_key)
173
- self.vault = {} # In production: use encrypted database
174
-
175
- def tokenize(self, card_data):
176
- """Convert card data to token."""
177
- # Generate secure random token
178
- token = secrets.token_urlsafe(32)
179
-
180
- # Encrypt card data
181
- encrypted = self.cipher.encrypt(json.dumps(card_data).encode())
182
-
183
- # Store token -> encrypted data mapping
184
- self.vault[token] = encrypted
185
-
186
- return token
187
-
188
- def detokenize(self, token):
189
- """Retrieve card data from token."""
190
- encrypted = self.vault.get(token)
191
- if not encrypted:
192
- raise ValueError("Token not found")
193
-
194
- # Decrypt
195
- decrypted = self.cipher.decrypt(encrypted)
196
- return json.loads(decrypted.decode())
197
-
198
- def delete_token(self, token):
199
- """Remove token from vault."""
200
- self.vault.pop(token, None)
201
- ```
202
-
203
- ## Encryption
204
-
205
- ### Data at Rest
206
- ```python
207
- from cryptography.hazmat.primitives.ciphers.aead import AESGCM
208
- import os
209
-
210
- class EncryptedStorage:
211
- """Encrypt data at rest using AES-256-GCM."""
212
-
213
- def __init__(self, encryption_key):
214
- """Initialize with 256-bit key."""
215
- self.key = encryption_key # Must be 32 bytes
216
-
217
- def encrypt(self, plaintext):
218
- """Encrypt data."""
219
- # Generate random nonce
220
- nonce = os.urandom(12)
221
-
222
- # Encrypt
223
- aesgcm = AESGCM(self.key)
224
- ciphertext = aesgcm.encrypt(nonce, plaintext.encode(), None)
225
-
226
- # Return nonce + ciphertext
227
- return nonce + ciphertext
228
-
229
- def decrypt(self, encrypted_data):
230
- """Decrypt data."""
231
- # Extract nonce and ciphertext
232
- nonce = encrypted_data[:12]
233
- ciphertext = encrypted_data[12:]
234
-
235
- # Decrypt
236
- aesgcm = AESGCM(self.key)
237
- plaintext = aesgcm.decrypt(nonce, ciphertext, None)
238
-
239
- return plaintext.decode()
240
-
241
- # Usage
242
- storage = EncryptedStorage(os.urandom(32))
243
- encrypted_pan = storage.encrypt("4242424242424242")
244
- # Store encrypted_pan in database
245
- ```
246
-
247
- ### Data in Transit
248
- ```python
249
- # Always use TLS 1.2 or higher
250
- # Flask/Django example
251
- app.config['SESSION_COOKIE_SECURE'] = True # HTTPS only
252
- app.config['SESSION_COOKIE_HTTPONLY'] = True
253
- app.config['SESSION_COOKIE_SAMESITE'] = 'Strict'
254
-
255
- # Enforce HTTPS
256
- from flask_talisman import Talisman
257
- Talisman(app, force_https=True)
258
- ```
259
-
260
- ## Access Control
261
-
262
- ```python
263
- from functools import wraps
264
- from flask import session
265
-
266
- def require_pci_access(f):
267
- """Decorator to restrict access to cardholder data."""
268
- @wraps(f)
269
- def decorated_function(*args, **kwargs):
270
- user = session.get('user')
271
-
272
- # Check if user has PCI access role
273
- if not user or 'pci_access' not in user.get('roles', []):
274
- return {'error': 'Unauthorized access to cardholder data'}, 403
275
-
276
- # Log access attempt
277
- audit_log(
278
- user=user['id'],
279
- action='access_cardholder_data',
280
- resource=f.__name__
281
- )
282
-
283
- return f(*args, **kwargs)
284
-
285
- return decorated_function
286
-
287
- @app.route('/api/payment-methods')
288
- @require_pci_access
289
- def get_payment_methods():
290
- """Retrieve payment methods (restricted access)."""
291
- # Only accessible to users with pci_access role
292
- pass
293
- ```
294
-
295
- ## Audit Logging
296
-
297
- ```python
298
- import logging
299
- from datetime import datetime
300
-
301
- class PCIAuditLogger:
302
- """PCI-compliant audit logging."""
303
-
304
- def __init__(self):
305
- self.logger = logging.getLogger('pci_audit')
306
- # Configure to write to secure, append-only log
307
-
308
- def log_access(self, user_id, resource, action, result):
309
- """Log access to cardholder data."""
310
- entry = {
311
- 'timestamp': datetime.utcnow().isoformat(),
312
- 'user_id': user_id,
313
- 'resource': resource,
314
- 'action': action,
315
- 'result': result,
316
- 'ip_address': request.remote_addr
317
- }
318
-
319
- self.logger.info(json.dumps(entry))
320
-
321
- def log_authentication(self, user_id, success, method):
322
- """Log authentication attempt."""
323
- entry = {
324
- 'timestamp': datetime.utcnow().isoformat(),
325
- 'user_id': user_id,
326
- 'event': 'authentication',
327
- 'success': success,
328
- 'method': method,
329
- 'ip_address': request.remote_addr
330
- }
331
-
332
- self.logger.info(json.dumps(entry))
333
-
334
- # Usage
335
- audit = PCIAuditLogger()
336
- audit.log_access(user_id=123, resource='payment_methods', action='read', result='success')
337
- ```
338
-
339
- ## Security Best Practices
340
-
341
- ### Input Validation
342
- ```python
343
- import re
344
-
345
- def validate_card_number(card_number):
346
- """Validate card number format (Luhn algorithm)."""
347
- # Remove spaces and dashes
348
- card_number = re.sub(r'[\s-]', '', card_number)
349
-
350
- # Check if all digits
351
- if not card_number.isdigit():
352
- return False
353
-
354
- # Luhn algorithm
355
- def luhn_checksum(card_num):
356
- def digits_of(n):
357
- return [int(d) for d in str(n)]
358
-
359
- digits = digits_of(card_num)
360
- odd_digits = digits[-1::-2]
361
- even_digits = digits[-2::-2]
362
- checksum = sum(odd_digits)
363
- for d in even_digits:
364
- checksum += sum(digits_of(d * 2))
365
- return checksum % 10
366
-
367
- return luhn_checksum(card_number) == 0
368
-
369
- def sanitize_input(user_input):
370
- """Sanitize user input to prevent injection."""
371
- # Remove special characters
372
- # Validate against expected format
373
- # Escape for database queries
374
- pass
375
- ```
376
-
377
- ## PCI DSS SAQ (Self-Assessment Questionnaire)
378
-
379
- ### SAQ A (Least Requirements)
380
- - E-commerce using hosted payment page
381
- - No card data on your systems
382
- - ~20 questions
383
-
384
- ### SAQ A-EP
385
- - E-commerce with embedded payment form
386
- - Uses JavaScript to handle card data
387
- - ~180 questions
388
-
389
- ### SAQ D (Most Requirements)
390
- - Store, process, or transmit card data
391
- - Full PCI DSS requirements
392
- - ~300 questions
393
-
394
- ## Compliance Checklist
395
-
396
- ```python
397
- PCI_COMPLIANCE_CHECKLIST = {
398
- 'network_security': [
399
- 'Firewall configured and maintained',
400
- 'No vendor default passwords',
401
- 'Network segmentation implemented'
402
- ],
403
- 'data_protection': [
404
- 'No storage of CVV, track data, or PIN',
405
- 'PAN encrypted when stored',
406
- 'PAN masked when displayed',
407
- 'Encryption keys properly managed'
408
- ],
409
- 'vulnerability_management': [
410
- 'Anti-virus installed and updated',
411
- 'Secure development practices',
412
- 'Regular security patches',
413
- 'Vulnerability scanning performed'
414
- ],
415
- 'access_control': [
416
- 'Access restricted by role',
417
- 'Unique IDs for all users',
418
- 'Multi-factor authentication',
419
- 'Physical security measures'
420
- ],
421
- 'monitoring': [
422
- 'Audit logs enabled',
423
- 'Log review process',
424
- 'File integrity monitoring',
425
- 'Regular security testing'
426
- ],
427
- 'policy': [
428
- 'Security policy documented',
429
- 'Risk assessment performed',
430
- 'Security awareness training',
431
- 'Incident response plan'
432
- ]
433
- }
434
- ```
435
-
436
- ## Resources
437
-
438
- - **references/data-minimization.md**: Never store prohibited data
439
- - **references/tokenization.md**: Tokenization strategies
440
- - **references/encryption.md**: Encryption requirements
441
- - **references/access-control.md**: Role-based access
442
- - **references/audit-logging.md**: Comprehensive logging
443
- - **assets/pci-compliance-checklist.md**: Complete checklist
444
- - **assets/encrypted-storage.py**: Encryption utilities
445
- - **scripts/audit-payment-system.sh**: Compliance audit script
446
-
447
- ## Common Violations
448
-
449
- 1. **Storing CVV**: Never store card verification codes
450
- 2. **Unencrypted PAN**: Card numbers must be encrypted at rest
451
- 3. **Weak Encryption**: Use AES-256 or equivalent
452
- 4. **No Access Controls**: Restrict who can access cardholder data
453
- 5. **Missing Audit Logs**: Must log all access to payment data
454
- 6. **Insecure Transmission**: Always use TLS 1.2+
455
- 7. **Default Passwords**: Change all default credentials
456
- 8. **No Security Testing**: Regular penetration testing required
457
-
458
- ## Reducing PCI Scope
459
-
460
- 1. **Use Hosted Payments**: Stripe Checkout, PayPal, etc.
461
- 2. **Tokenization**: Replace card data with tokens
462
- 3. **Network Segmentation**: Isolate cardholder data environment
463
- 4. **Outsource**: Use PCI-compliant payment processors
464
- 5. **No Storage**: Never store full card details
465
-
466
- By minimizing systems that touch card data, you reduce compliance burden significantly.
@@ -1,204 +0,0 @@
1
- ---
2
- name: postgresql-table-design
3
- description: Design a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features
4
- ---
5
-
6
- # PostgreSQL Table Design
7
-
8
- ## Core Rules
9
-
10
- - Define a **PRIMARY KEY** for reference tables (users, orders, etc.). Not always needed for time-series/event/log data. When used, prefer `BIGINT GENERATED ALWAYS AS IDENTITY`; use `UUID` only when global uniqueness/opacity is needed.
11
- - **Normalize first (to 3NF)** to eliminate data redundancy and update anomalies; denormalize **only** for measured, high-ROI reads where join performance is proven problematic. Premature denormalization creates maintenance burden.
12
- - Add **NOT NULL** everywhere it’s semantically required; use **DEFAULT**s for common values.
13
- - Create **indexes for access paths you actually query**: PK/unique (auto), **FK columns (manual!)**, frequent filters/sorts, and join keys.
14
- - Prefer **TIMESTAMPTZ** for event time; **NUMERIC** for money; **TEXT** for strings; **BIGINT** for integer values, **DOUBLE PRECISION** for floats (or `NUMERIC` for exact decimal arithmetic).
15
-
16
- ## PostgreSQL “Gotchas”
17
-
18
- - **Identifiers**: unquoted → lowercased. Avoid quoted/mixed-case names. Convention: use `snake_case` for table/column names.
19
- - **Unique + NULLs**: UNIQUE allows multiple NULLs. Use `UNIQUE (...) NULLS NOT DISTINCT` (PG15+) to restrict to one NULL.
20
- - **FK indexes**: PostgreSQL **does not** auto-index FK columns. Add them.
21
- - **No silent coercions**: length/precision overflows error out (no truncation). Example: inserting 999 into `NUMERIC(2,0)` fails with error, unlike some databases that silently truncate or round.
22
- - **Sequences/identity have gaps** (normal; don't "fix"). Rollbacks, crashes, and concurrent transactions create gaps in ID sequences (1, 2, 5, 6...). This is expected behavior—don't try to make IDs consecutive.
23
- - **Heap storage**: no clustered PK by default (unlike SQL Server/MySQL InnoDB); `CLUSTER` is one-off reorganization, not maintained on subsequent inserts. Row order on disk is insertion order unless explicitly clustered.
24
- - **MVCC**: updates/deletes leave dead tuples; vacuum handles them—design to avoid hot wide-row churn.
25
-
26
- ## Data Types
27
-
28
- - **IDs**: `BIGINT GENERATED ALWAYS AS IDENTITY` preferred (`GENERATED BY DEFAULT` also fine); `UUID` when merging/federating/used in a distributed system or for opaque IDs. Generate with `uuidv7()` (preferred if using PG18+) or `gen_random_uuid()` (if using an older PG version).
29
- - **Integers**: prefer `BIGINT` unless storage space is critical; `INTEGER` for smaller ranges; avoid `SMALLINT` unless constrained.
30
- - **Floats**: prefer `DOUBLE PRECISION` over `REAL` unless storage space is critical. Use `NUMERIC` for exact decimal arithmetic.
31
- - **Strings**: prefer `TEXT`; if length limits needed, use `CHECK (LENGTH(col) <= n)` instead of `VARCHAR(n)`; avoid `CHAR(n)`. Use `BYTEA` for binary data. Large strings/binary (>2KB default threshold) automatically stored in TOAST with compression. TOAST storage: `PLAIN` (no TOAST), `EXTENDED` (compress + out-of-line), `EXTERNAL` (out-of-line, no compress), `MAIN` (compress, keep in-line if possible). Default `EXTENDED` usually optimal. Control with `ALTER TABLE tbl ALTER COLUMN col SET STORAGE strategy` and `ALTER TABLE tbl SET (toast_tuple_target = 4096)` for threshold. Case-insensitive: for locale/accent handling use non-deterministic collations; for plain ASCII use expression indexes on `LOWER(col)` (preferred unless column needs case-insensitive PK/FK/UNIQUE) or `CITEXT`.
32
- - **Money**: `NUMERIC(p,s)` (never float).
33
- - **Time**: `TIMESTAMPTZ` for timestamps; `DATE` for date-only; `INTERVAL` for durations. Avoid `TIMESTAMP` (without timezone). Use `now()` for transaction start time, `clock_timestamp()` for current wall-clock time.
34
- - **Booleans**: `BOOLEAN` with `NOT NULL` constraint unless tri-state values are required.
35
- - **Enums**: `CREATE TYPE ... AS ENUM` for small, stable sets (e.g. US states, days of week). For business-logic-driven and evolving values (e.g. order statuses) → use TEXT (or INT) + CHECK or lookup table.
36
- - **Arrays**: `TEXT[]`, `INTEGER[]`, etc. Use for ordered lists where you query elements. Index with **GIN** for containment (`@>`, `<@`) and overlap (`&&`) queries. Access: `arr[1]` (1-indexed), `arr[1:3]` (slicing). Good for tags, categories; avoid for relations—use junction tables instead. Literal syntax: `'{val1,val2}'` or `ARRAY[val1,val2]`.
37
- - **Range types**: `daterange`, `numrange`, `tstzrange` for intervals. Support overlap (`&&`), containment (`@>`), operators. Index with **GiST**. Good for scheduling, versioning, numeric ranges. Pick a bounds scheme and use it consistently; prefer `[)` (inclusive/exclusive) by default.
38
- - **Network types**: `INET` for IP addresses, `CIDR` for network ranges, `MACADDR` for MAC addresses. Support network operators (`<<`, `>>`, `&&`).
39
- - **Geometric types**: `POINT`, `LINE`, `POLYGON`, `CIRCLE` for 2D spatial data. Index with **GiST**. Consider **PostGIS** for advanced spatial features.
40
- - **Text search**: `TSVECTOR` for full-text search documents, `TSQUERY` for search queries. Index `tsvector` with **GIN**. Always specify language: `to_tsvector('english', col)` and `to_tsquery('english', 'query')`. Never use single-argument versions. This applies to both index expressions and queries.
41
- - **Domain types**: `CREATE DOMAIN email AS TEXT CHECK (VALUE ~ '^[^@]+@[^@]+$')` for reusable custom types with validation. Enforces constraints across tables.
42
- - **Composite types**: `CREATE TYPE address AS (street TEXT, city TEXT, zip TEXT)` for structured data within columns. Access with `(col).field` syntax.
43
- - **JSONB**: preferred over JSON; index with **GIN**. Use only for optional/semi-structured attrs. ONLY use JSON if the original ordering of the contents MUST be preserved.
44
- - **Vector types**: `vector` type by `pgvector` for vector similarity search for embeddings.
45
-
46
-
47
- ### Do not use the following data types
48
- - DO NOT use `timestamp` (without time zone); DO use `timestamptz` instead.
49
- - DO NOT use `char(n)` or `varchar(n)`; DO use `text` instead.
50
- - DO NOT use `money` type; DO use `numeric` instead.
51
- - DO NOT use `timetz` type; DO use `timestamptz` instead.
52
- - DO NOT use `timestamptz(0)` or any other precision specification; DO use `timestamptz` instead
53
- - DO NOT use `serial` type; DO use `generated always as identity` instead.
54
-
55
-
56
- ## Table Types
57
-
58
- - **Regular**: default; fully durable, logged.
59
- - **TEMPORARY**: session-scoped, auto-dropped, not logged. Faster for scratch work.
60
- - **UNLOGGED**: persistent but not crash-safe. Faster writes; good for caches/staging.
61
-
62
- ## Row-Level Security
63
-
64
- Enable with `ALTER TABLE tbl ENABLE ROW LEVEL SECURITY`. Create policies: `CREATE POLICY user_access ON orders FOR SELECT TO app_users USING (user_id = current_user_id())`. Built-in user-based access control at the row level.
65
-
66
- ## Constraints
67
-
68
- - **PK**: implicit UNIQUE + NOT NULL; creates a B-tree index.
69
- - **FK**: specify `ON DELETE/UPDATE` action (`CASCADE`, `RESTRICT`, `SET NULL`, `SET DEFAULT`). Add explicit index on referencing column—speeds up joins and prevents locking issues on parent deletes/updates. Use `DEFERRABLE INITIALLY DEFERRED` for circular FK dependencies checked at transaction end.
70
- - **UNIQUE**: creates a B-tree index; allows multiple NULLs unless `NULLS NOT DISTINCT` (PG15+). Standard behavior: `(1, NULL)` and `(1, NULL)` are allowed. With `NULLS NOT DISTINCT`: only one `(1, NULL)` allowed. Prefer `NULLS NOT DISTINCT` unless you specifically need duplicate NULLs.
71
- - **CHECK**: row-local constraints; NULL values pass the check (three-valued logic). Example: `CHECK (price > 0)` allows NULL prices. Combine with `NOT NULL` to enforce: `price NUMERIC NOT NULL CHECK (price > 0)`.
72
- - **EXCLUDE**: prevents overlapping values using operators. `EXCLUDE USING gist (room_id WITH =, booking_period WITH &&)` prevents double-booking rooms. Requires appropriate index type (often GiST).
73
-
74
- ## Indexing
75
-
76
- - **B-tree**: default for equality/range queries (`=`, `<`, `>`, `BETWEEN`, `ORDER BY`)
77
- - **Composite**: order matters—index used if equality on leftmost prefix (`WHERE a = ? AND b > ?` uses index on `(a,b)`, but `WHERE b = ?` does not). Put most selective/frequently filtered columns first.
78
- - **Covering**: `CREATE INDEX ON tbl (id) INCLUDE (name, email)` - includes non-key columns for index-only scans without visiting table.
79
- - **Partial**: for hot subsets (`WHERE status = 'active'` → `CREATE INDEX ON tbl (user_id) WHERE status = 'active'`). Any query with `status = 'active'` can use this index.
80
- - **Expression**: for computed search keys (`CREATE INDEX ON tbl (LOWER(email))`). Expression must match exactly in WHERE clause: `WHERE LOWER(email) = 'user@example.com'`.
81
- - **GIN**: JSONB containment/existence, arrays (`@>`, `?`), full-text search (`@@`)
82
- - **GiST**: ranges, geometry, exclusion constraints
83
- - **BRIN**: very large, naturally ordered data (time-series)—minimal storage overhead. Effective when row order on disk correlates with indexed column (insertion order or after `CLUSTER`).
84
-
85
- ## Partitioning
86
-
87
- - Use for very large tables (>100M rows) where queries consistently filter on partition key (often time/date).
88
- - Alternate use: use for tables where data maintenance tasks dictates e.g. data pruned or bulk replaced periodically
89
- - **RANGE**: common for time-series (`PARTITION BY RANGE (created_at)`). Create partitions: `CREATE TABLE logs_2024_01 PARTITION OF logs FOR VALUES FROM ('2024-01-01') TO ('2024-02-01')`. **TimescaleDB** automates time-based or ID-based partitioning with retention policies and compression.
90
- - **LIST**: for discrete values (`PARTITION BY LIST (region)`). Example: `FOR VALUES IN ('us-east', 'us-west')`.
91
- - **HASH**: for even distribution when no natural key (`PARTITION BY HASH (user_id)`). Creates N partitions with modulus.
92
- - **Constraint exclusion**: requires `CHECK` constraints on partitions for query planner to prune. Auto-created for declarative partitioning (PG10+).
93
- - Prefer declarative partitioning or hypertables. Do NOT use table inheritance.
94
- - **Limitations**: no global UNIQUE constraints—include partition key in PK/UNIQUE. FKs from partitioned tables not supported; use triggers.
95
-
96
- ## Special Considerations
97
-
98
- ### Update-Heavy Tables
99
-
100
- - **Separate hot/cold columns**—put frequently updated columns in separate table to minimize bloat.
101
- - **Use `fillfactor=90`** to leave space for HOT updates that avoid index maintenance.
102
- - **Avoid updating indexed columns**—prevents beneficial HOT updates.
103
- - **Partition by update patterns**—separate frequently updated rows in a different partition from stable data.
104
-
105
- ### Insert-Heavy Workloads
106
-
107
- - **Minimize indexes**—only create what you query; every index slows inserts.
108
- - **Use `COPY` or multi-row `INSERT`** instead of single-row inserts.
109
- - **UNLOGGED tables** for rebuildable staging data—much faster writes.
110
- - **Defer index creation** for bulk loads—>drop index, load data, recreate indexes.
111
- - **Partition by time/hash** to distribute load. **TimescaleDB** automates partitioning and compression of insert-heavy data.
112
- - **Use a natural key for primary key** such as a (timestamp, device_id) if enforcing global uniqueness is important many insert-heavy tables don't need a primary key at all.
113
- - If you do need a surrogate key, **Prefer `BIGINT GENERATED ALWAYS AS IDENTITY` over `UUID`**.
114
-
115
- ### Upsert-Friendly Design
116
-
117
- - **Requires UNIQUE index** on conflict target columns—`ON CONFLICT (col1, col2)` needs exact matching unique index (partial indexes don't work).
118
- - **Use `EXCLUDED.column`** to reference would-be-inserted values; only update columns that actually changed to reduce write overhead.
119
- - **`DO NOTHING` faster** than `DO UPDATE` when no actual update needed.
120
-
121
- ### Safe Schema Evolution
122
-
123
- - **Transactional DDL**: most DDL operations can run in transactions and be rolled back—`BEGIN; ALTER TABLE...; ROLLBACK;` for safe testing.
124
- - **Concurrent index creation**: `CREATE INDEX CONCURRENTLY` avoids blocking writes but can't run in transactions.
125
- - **Volatile defaults cause rewrites**: adding `NOT NULL` columns with volatile defaults (e.g., `now()`, `gen_random_uuid()`) rewrites entire table. Non-volatile defaults are fast.
126
- - **Drop constraints before columns**: `ALTER TABLE DROP CONSTRAINT` then `DROP COLUMN` to avoid dependency issues.
127
- - **Function signature changes**: `CREATE OR REPLACE` with different arguments creates overloads, not replacements. DROP old version if no overload desired.
128
-
129
- ## Generated Columns
130
-
131
- - `... GENERATED ALWAYS AS (<expr>) STORED` for computed, indexable fields. PG18+ adds `VIRTUAL` columns (computed on read, not stored).
132
-
133
- ## Extensions
134
-
135
- - **`pgcrypto`**: `crypt()` for password hashing.
136
- - **`uuid-ossp`**: alternative UUID functions; prefer `pgcrypto` for new projects.
137
- - **`pg_trgm`**: fuzzy text search with `%` operator, `similarity()` function. Index with GIN for `LIKE '%pattern%'` acceleration.
138
- - **`citext`**: case-insensitive text type. Prefer expression indexes on `LOWER(col)` unless you need case-insensitive constraints.
139
- - **`btree_gin`/`btree_gist`**: enable mixed-type indexes (e.g., GIN index on both JSONB and text columns).
140
- - **`hstore`**: key-value pairs; mostly superseded by JSONB but useful for simple string mappings.
141
- - **`timescaledb`**: essential for time-series—automated partitioning, retention, compression, continuous aggregates.
142
- - **`postgis`**: comprehensive geospatial support beyond basic geometric types—essential for location-based applications.
143
- - **`pgvector`**: vector similarity search for embeddings.
144
- - **`pgaudit`**: audit logging for all database activity.
145
-
146
- ## JSONB Guidance
147
-
148
- - Prefer `JSONB` with **GIN** index.
149
- - Default: `CREATE INDEX ON tbl USING GIN (jsonb_col);` → accelerates:
150
- - **Containment** `jsonb_col @> '{"k":"v"}'`
151
- - **Key existence** `jsonb_col ? 'k'`, **any/all keys** `?\|`, `?&`
152
- - **Path containment** on nested docs
153
- - **Disjunction** `jsonb_col @> ANY(ARRAY['{"status":"active"}', '{"status":"pending"}'])`
154
- - Heavy `@>` workloads: consider opclass `jsonb_path_ops` for smaller/faster containment-only indexes:
155
- - `CREATE INDEX ON tbl USING GIN (jsonb_col jsonb_path_ops);`
156
- - **Trade-off**: loses support for key existence (`?`, `?|`, `?&`) queries—only supports containment (`@>`)
157
- - Equality/range on a specific scalar field: extract and index with B-tree (generated column or expression):
158
- - `ALTER TABLE tbl ADD COLUMN price INT GENERATED ALWAYS AS ((jsonb_col->>'price')::INT) STORED;`
159
- - `CREATE INDEX ON tbl (price);`
160
- - Prefer queries like `WHERE price BETWEEN 100 AND 500` (uses B-tree) over `WHERE (jsonb_col->>'price')::INT BETWEEN 100 AND 500` without index.
161
- - Arrays inside JSONB: use GIN + `@>` for containment (e.g., tags). Consider `jsonb_path_ops` if only doing containment.
162
- - Keep core relations in tables; use JSONB for optional/variable attributes.
163
- - Use constraints to limit allowed JSONB values in a column e.g. `config JSONB NOT NULL CHECK(jsonb_typeof(config) = 'object')`
164
-
165
-
166
- ## Examples
167
-
168
- ### Users
169
-
170
- ```sql
171
- CREATE TABLE users (
172
- user_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
173
- email TEXT NOT NULL UNIQUE,
174
- name TEXT NOT NULL,
175
- created_at TIMESTAMPTZ NOT NULL DEFAULT now()
176
- );
177
- CREATE UNIQUE INDEX ON users (LOWER(email));
178
- CREATE INDEX ON users (created_at);
179
- ```
180
-
181
- ### Orders
182
-
183
- ```sql
184
- CREATE TABLE orders (
185
- order_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
186
- user_id BIGINT NOT NULL REFERENCES users(user_id),
187
- status TEXT NOT NULL DEFAULT 'PENDING' CHECK (status IN ('PENDING','PAID','CANCELED')),
188
- total NUMERIC(10,2) NOT NULL CHECK (total > 0),
189
- created_at TIMESTAMPTZ NOT NULL DEFAULT now()
190
- );
191
- CREATE INDEX ON orders (user_id);
192
- CREATE INDEX ON orders (created_at);
193
- ```
194
-
195
- ### JSONB
196
-
197
- ```sql
198
- CREATE TABLE profiles (
199
- user_id BIGINT PRIMARY KEY REFERENCES users(user_id),
200
- attrs JSONB NOT NULL DEFAULT '{}',
201
- theme TEXT GENERATED ALWAYS AS (attrs->>'theme') STORED
202
- );
203
- CREATE INDEX profiles_attrs_gin ON profiles USING GIN (attrs);
204
- ```