@maestro-ai/cli 1.0.0

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 (252) hide show
  1. package/README.md +59 -0
  2. package/content/guides/Cat/303/241logo de Stacks para Cloud Moderna.md" +119 -0
  3. package/content/guides/Cat/303/241logo de Stacks para Hospedagem Compartilhada.md" +147 -0
  4. package/content/guides/Checklist Mestre de Entrega.md +68 -0
  5. package/content/guides/Gates de Qualidade.md +209 -0
  6. package/content/guides/Guia de Adi/303/247/303/243o de Novas Funcionalidades.md" +355 -0
  7. package/content/guides/Guia de Chaos Engineering.md +267 -0
  8. package/content/guides/Guia de Debugging com IA.md +135 -0
  9. package/content/guides/Guia de Estrat/303/251gias de Cache.md" +352 -0
  10. package/content/guides/Guia de Migrations Zero-Downtime.md +311 -0
  11. package/content/guides/Guia de Multi-tenancy.md +368 -0
  12. package/content/guides/Guia de Otimiza/303/247/303/243o de Custos Cloud.md" +195 -0
  13. package/content/guides/Guia de Refatora/303/247/303/243o de C/303/263digo Legado com IA.md" +162 -0
  14. package/content/guides/Guia de SLOs e Error Budgets.md +315 -0
  15. package/content/guides/M/303/251tricas de Efici/303/252ncia do Desenvolvimento com IA.md" +93 -0
  16. package/content/guides/Rules base.md +90 -0
  17. package/content/prompts/README.md +203 -0
  18. package/content/prompts/acessibilidade/analise-acessibilidade.md +257 -0
  19. package/content/prompts/apis/design-api-rest.md +303 -0
  20. package/content/prompts/apis/idempotencia.md +254 -0
  21. package/content/prompts/apis/versionamento.md +313 -0
  22. package/content/prompts/arquitetura/arquitetura-c4-completo.md +190 -0
  23. package/content/prompts/arquitetura/clean-architecture.md +151 -0
  24. package/content/prompts/arquitetura/ddd-bounded-contexts.md +183 -0
  25. package/content/prompts/arquitetura/ddd-cqrs.md +176 -0
  26. package/content/prompts/arquitetura/modelo-dominio.md +207 -0
  27. package/content/prompts/arquitetura/multi-tenancy.md +235 -0
  28. package/content/prompts/database/migrations-zero-downtime.md +192 -0
  29. package/content/prompts/database/otimizacao-queries.md +296 -0
  30. package/content/prompts/desenvolvimento/code-review.md +301 -0
  31. package/content/prompts/desenvolvimento/gerar-servico.md +271 -0
  32. package/content/prompts/devops/docker-compose.md +336 -0
  33. package/content/prompts/devops/feature-flags.md +374 -0
  34. package/content/prompts/devops/kubernetes-deploy.md +460 -0
  35. package/content/prompts/devops/pipeline-cicd.md +358 -0
  36. package/content/prompts/devops/terraform-iac.md +502 -0
  37. package/content/prompts/escalabilidade/analise-performance.md +240 -0
  38. package/content/prompts/escalabilidade/analise-performance.txt +94 -0
  39. package/content/prompts/escalabilidade/caching.md +255 -0
  40. package/content/prompts/observabilidade/chaos-testing.md +237 -0
  41. package/content/prompts/observabilidade/estrategia-observabilidade.md +263 -0
  42. package/content/prompts/observabilidade/estrategia-observabilidade.txt +134 -0
  43. package/content/prompts/observabilidade/slos.md +215 -0
  44. package/content/prompts/produto/discovery-inicial.md +203 -0
  45. package/content/prompts/produto/discovery-inicial.txt +33 -0
  46. package/content/prompts/requisitos/refinar-requisitos.md +232 -0
  47. package/content/prompts/requisitos/refinar-requisitos.txt +40 -0
  48. package/content/prompts/seguranca/analise-seguranca.md +243 -0
  49. package/content/prompts/seguranca/pentest-checklist.md +333 -0
  50. package/content/prompts/seguranca/rate-limiting.md +356 -0
  51. package/content/prompts/seguranca/revisao-lgpd.md +227 -0
  52. package/content/prompts/seguranca/threat-modeling.md +224 -0
  53. package/content/prompts/testes/contract-testing.md +340 -0
  54. package/content/prompts/testes/gerar-testes-unitarios.md +474 -0
  55. package/content/prompts/testes/testes-e2e.md +460 -0
  56. package/content/prompts/testes/testes-integracao.md +418 -0
  57. package/content/prompts/testes/testes-performance.md +458 -0
  58. package/content/prompts/ux/gerar-ui-stitch.md +151 -0
  59. package/content/skills/api-patterns/SKILL.md +81 -0
  60. package/content/skills/api-patterns/api-style.md +42 -0
  61. package/content/skills/api-patterns/auth.md +24 -0
  62. package/content/skills/api-patterns/documentation.md +26 -0
  63. package/content/skills/api-patterns/graphql.md +41 -0
  64. package/content/skills/api-patterns/rate-limiting.md +31 -0
  65. package/content/skills/api-patterns/response.md +37 -0
  66. package/content/skills/api-patterns/rest.md +40 -0
  67. package/content/skills/api-patterns/scripts/api_validator.py +211 -0
  68. package/content/skills/api-patterns/security-testing.md +122 -0
  69. package/content/skills/api-patterns/trpc.md +41 -0
  70. package/content/skills/api-patterns/versioning.md +22 -0
  71. package/content/skills/app-builder/SKILL.md +75 -0
  72. package/content/skills/app-builder/agent-coordination.md +71 -0
  73. package/content/skills/app-builder/feature-building.md +53 -0
  74. package/content/skills/app-builder/project-detection.md +34 -0
  75. package/content/skills/app-builder/scaffolding.md +118 -0
  76. package/content/skills/app-builder/tech-stack.md +40 -0
  77. package/content/skills/app-builder/templates/SKILL.md +39 -0
  78. package/content/skills/app-builder/templates/astro-static/TEMPLATE.md +76 -0
  79. package/content/skills/app-builder/templates/chrome-extension/TEMPLATE.md +92 -0
  80. package/content/skills/app-builder/templates/cli-tool/TEMPLATE.md +88 -0
  81. package/content/skills/app-builder/templates/electron-desktop/TEMPLATE.md +88 -0
  82. package/content/skills/app-builder/templates/express-api/TEMPLATE.md +83 -0
  83. package/content/skills/app-builder/templates/flutter-app/TEMPLATE.md +90 -0
  84. package/content/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -0
  85. package/content/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +82 -0
  86. package/content/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +100 -0
  87. package/content/skills/app-builder/templates/nextjs-static/TEMPLATE.md +106 -0
  88. package/content/skills/app-builder/templates/nuxt-app/TEMPLATE.md +101 -0
  89. package/content/skills/app-builder/templates/python-fastapi/TEMPLATE.md +83 -0
  90. package/content/skills/app-builder/templates/react-native-app/TEMPLATE.md +93 -0
  91. package/content/skills/architecture/SKILL.md +55 -0
  92. package/content/skills/architecture/context-discovery.md +43 -0
  93. package/content/skills/architecture/examples.md +94 -0
  94. package/content/skills/architecture/pattern-selection.md +68 -0
  95. package/content/skills/architecture/patterns-reference.md +50 -0
  96. package/content/skills/architecture/trade-off-analysis.md +77 -0
  97. package/content/skills/bash-linux/SKILL.md +199 -0
  98. package/content/skills/behavioral-modes/SKILL.md +242 -0
  99. package/content/skills/brainstorming/SKILL.md +163 -0
  100. package/content/skills/brainstorming/dynamic-questioning.md +350 -0
  101. package/content/skills/clean-code/SKILL.md +201 -0
  102. package/content/skills/code-review-checklist/SKILL.md +109 -0
  103. package/content/skills/database-design/SKILL.md +52 -0
  104. package/content/skills/database-design/database-selection.md +43 -0
  105. package/content/skills/database-design/indexing.md +39 -0
  106. package/content/skills/database-design/migrations.md +48 -0
  107. package/content/skills/database-design/optimization.md +36 -0
  108. package/content/skills/database-design/orm-selection.md +30 -0
  109. package/content/skills/database-design/schema-design.md +56 -0
  110. package/content/skills/database-design/scripts/schema_validator.py +172 -0
  111. package/content/skills/deployment-procedures/SKILL.md +241 -0
  112. package/content/skills/doc.md +177 -0
  113. package/content/skills/documentation-templates/SKILL.md +194 -0
  114. package/content/skills/frontend-design/SKILL.md +396 -0
  115. package/content/skills/frontend-design/animation-guide.md +331 -0
  116. package/content/skills/frontend-design/color-system.md +311 -0
  117. package/content/skills/frontend-design/decision-trees.md +418 -0
  118. package/content/skills/frontend-design/motion-graphics.md +306 -0
  119. package/content/skills/frontend-design/scripts/accessibility_checker.py +183 -0
  120. package/content/skills/frontend-design/scripts/ux_audit.py +722 -0
  121. package/content/skills/frontend-design/typography-system.md +345 -0
  122. package/content/skills/frontend-design/ux-psychology.md +541 -0
  123. package/content/skills/frontend-design/visual-effects.md +383 -0
  124. package/content/skills/game-development/2d-games/SKILL.md +119 -0
  125. package/content/skills/game-development/3d-games/SKILL.md +135 -0
  126. package/content/skills/game-development/SKILL.md +167 -0
  127. package/content/skills/game-development/game-art/SKILL.md +185 -0
  128. package/content/skills/game-development/game-audio/SKILL.md +190 -0
  129. package/content/skills/game-development/game-design/SKILL.md +129 -0
  130. package/content/skills/game-development/mobile-games/SKILL.md +108 -0
  131. package/content/skills/game-development/multiplayer/SKILL.md +132 -0
  132. package/content/skills/game-development/pc-games/SKILL.md +144 -0
  133. package/content/skills/game-development/vr-ar/SKILL.md +123 -0
  134. package/content/skills/game-development/web-games/SKILL.md +150 -0
  135. package/content/skills/geo-fundamentals/SKILL.md +156 -0
  136. package/content/skills/geo-fundamentals/scripts/geo_checker.py +289 -0
  137. package/content/skills/i18n-localization/SKILL.md +154 -0
  138. package/content/skills/i18n-localization/scripts/i18n_checker.py +241 -0
  139. package/content/skills/intelligent-routing/SKILL.md +334 -0
  140. package/content/skills/lint-and-validate/SKILL.md +45 -0
  141. package/content/skills/lint-and-validate/scripts/lint_runner.py +172 -0
  142. package/content/skills/lint-and-validate/scripts/type_coverage.py +173 -0
  143. package/content/skills/mcp-builder/SKILL.md +176 -0
  144. package/content/skills/mobile-design/SKILL.md +394 -0
  145. package/content/skills/mobile-design/decision-trees.md +516 -0
  146. package/content/skills/mobile-design/mobile-backend.md +491 -0
  147. package/content/skills/mobile-design/mobile-color-system.md +420 -0
  148. package/content/skills/mobile-design/mobile-debugging.md +122 -0
  149. package/content/skills/mobile-design/mobile-design-thinking.md +357 -0
  150. package/content/skills/mobile-design/mobile-navigation.md +458 -0
  151. package/content/skills/mobile-design/mobile-performance.md +767 -0
  152. package/content/skills/mobile-design/mobile-testing.md +356 -0
  153. package/content/skills/mobile-design/mobile-typography.md +433 -0
  154. package/content/skills/mobile-design/platform-android.md +666 -0
  155. package/content/skills/mobile-design/platform-ios.md +561 -0
  156. package/content/skills/mobile-design/scripts/mobile_audit.py +670 -0
  157. package/content/skills/mobile-design/touch-psychology.md +537 -0
  158. package/content/skills/nextjs-best-practices/SKILL.md +203 -0
  159. package/content/skills/nodejs-best-practices/SKILL.md +333 -0
  160. package/content/skills/parallel-agents/SKILL.md +175 -0
  161. package/content/skills/performance-profiling/SKILL.md +143 -0
  162. package/content/skills/performance-profiling/scripts/lighthouse_audit.py +76 -0
  163. package/content/skills/plan-writing/SKILL.md +152 -0
  164. package/content/skills/powershell-windows/SKILL.md +167 -0
  165. package/content/skills/python-patterns/SKILL.md +441 -0
  166. package/content/skills/react-patterns/SKILL.md +198 -0
  167. package/content/skills/red-team-tactics/SKILL.md +199 -0
  168. package/content/skills/seo-fundamentals/SKILL.md +129 -0
  169. package/content/skills/seo-fundamentals/scripts/seo_checker.py +219 -0
  170. package/content/skills/server-management/SKILL.md +161 -0
  171. package/content/skills/systematic-debugging/SKILL.md +109 -0
  172. package/content/skills/tailwind-patterns/SKILL.md +269 -0
  173. package/content/skills/tdd-workflow/SKILL.md +149 -0
  174. package/content/skills/testing-patterns/SKILL.md +178 -0
  175. package/content/skills/testing-patterns/scripts/test_runner.py +219 -0
  176. package/content/skills/vulnerability-scanner/SKILL.md +276 -0
  177. package/content/skills/vulnerability-scanner/checklists.md +121 -0
  178. package/content/skills/vulnerability-scanner/scripts/security_scan.py +458 -0
  179. package/content/skills/webapp-testing/SKILL.md +187 -0
  180. package/content/skills/webapp-testing/scripts/playwright_runner.py +173 -0
  181. package/content/specialists/Especialista em Acessibilidade.md +266 -0
  182. package/content/specialists/Especialista em An/303/241lise de Testes.md" +434 -0
  183. package/content/specialists/Especialista em Arquitetura Avan/303/247ada.md" +358 -0
  184. package/content/specialists/Especialista em Arquitetura de Software.md +177 -0
  185. package/content/specialists/Especialista em Banco de Dados.md +260 -0
  186. package/content/specialists/Especialista em Contrato de API.md +172 -0
  187. package/content/specialists/Especialista em Dados e Analytics com IA.md +246 -0
  188. package/content/specialists/Especialista em Debugging e Troubleshooting.md +191 -0
  189. package/content/specialists/Especialista em Desenvolvimento Frontend.md +477 -0
  190. package/content/specialists/Especialista em Desenvolvimento Mobile.md +241 -0
  191. package/content/specialists/Especialista em Desenvolvimento e Vibe Coding Estruturado.md +417 -0
  192. package/content/specialists/Especialista em DevOps e Infraestrutura.md +294 -0
  193. package/content/specialists/Especialista em Documenta/303/247/303/243o T/303/251cnica.md" +227 -0
  194. package/content/specialists/Especialista em Engenharia de Requisitos com IA.md +299 -0
  195. package/content/specialists/Especialista em Explora/303/247/303/243o de Codebase.md" +179 -0
  196. package/content/specialists/Especialista em Gest/303/243o de Produto.md" +179 -0
  197. package/content/specialists/Especialista em Migra/303/247/303/243o e Moderniza/303/247/303/243o.md" +410 -0
  198. package/content/specialists/Especialista em Modelagem e Arquitetura de Dom/303/255nio com IA.md" +248 -0
  199. package/content/specialists/Especialista em Observabilidade.md +415 -0
  200. package/content/specialists/Especialista em Performance e Escalabilidade.md +373 -0
  201. package/content/specialists/Especialista em Plano de Execu/303/247/303/243o com IA.md" +341 -0
  202. package/content/specialists/Especialista em Prototipagem R/303/241pida com Google Stitch.md" +419 -0
  203. package/content/specialists/Especialista em Seguran/303/247a da Informa/303/247/303/243o.md" +508 -0
  204. package/content/specialists/Especialista em UX Design.md +453 -0
  205. package/content/specialists/INDEX.md +43 -0
  206. package/content/templates/PRD.md +165 -0
  207. package/content/templates/README.md +65 -0
  208. package/content/templates/adr.md +103 -0
  209. package/content/templates/arquitetura.md +279 -0
  210. package/content/templates/backlog.md +185 -0
  211. package/content/templates/checklist-seguranca.md +180 -0
  212. package/content/templates/contexto.md +120 -0
  213. package/content/templates/criterios-aceite.md +99 -0
  214. package/content/templates/design-banco.md +270 -0
  215. package/content/templates/design-doc.md +240 -0
  216. package/content/templates/feature.md +88 -0
  217. package/content/templates/historia-backend.md +84 -0
  218. package/content/templates/historia-frontend.md +75 -0
  219. package/content/templates/historia-usuario.md +125 -0
  220. package/content/templates/mapa-navegacao.md +133 -0
  221. package/content/templates/matriz-rastreabilidade.md +121 -0
  222. package/content/templates/modelo-dominio.md +219 -0
  223. package/content/templates/plano-testes.md +199 -0
  224. package/content/templates/prototipo-stitch.md +138 -0
  225. package/content/templates/requisitos.md +162 -0
  226. package/content/templates/slo-sli.md +197 -0
  227. package/content/workflows/README-MCP.md +363 -0
  228. package/content/workflows/brainstorm.md +113 -0
  229. package/content/workflows/create.md +59 -0
  230. package/content/workflows/debug.md +103 -0
  231. package/content/workflows/deploy.md +176 -0
  232. package/content/workflows/enhance.md +63 -0
  233. package/content/workflows/mcp-debug.md +506 -0
  234. package/content/workflows/mcp-feature.md +385 -0
  235. package/content/workflows/mcp-gate.md +413 -0
  236. package/content/workflows/mcp-next.md +388 -0
  237. package/content/workflows/mcp-refactor.md +600 -0
  238. package/content/workflows/mcp-start.md +304 -0
  239. package/content/workflows/mcp-status.md +400 -0
  240. package/content/workflows/orchestrate.md +237 -0
  241. package/content/workflows/plan.md +89 -0
  242. package/content/workflows/preview.md +81 -0
  243. package/content/workflows/status.md +86 -0
  244. package/content/workflows/test.md +144 -0
  245. package/content/workflows/ui-ux-pro-max.md +296 -0
  246. package/dist/commands/init.d.ts +6 -0
  247. package/dist/commands/init.js +138 -0
  248. package/dist/commands/update.d.ts +5 -0
  249. package/dist/commands/update.js +50 -0
  250. package/dist/index.d.ts +2 -0
  251. package/dist/index.js +21 -0
  252. package/package.json +48 -0
@@ -0,0 +1,311 @@
1
+ # Guia de Migrations Zero-Downtime
2
+
3
+ > **Prioridade**: 🟠 ALTA
4
+ > **Aplicável a**: Projetos Nível 2 (Médio) e Nível 3 (Complexo)
5
+
6
+ ---
7
+
8
+ ## O Problema
9
+
10
+ Migrations tradicionais podem causar:
11
+ - **Downtime** durante alterações de schema
12
+ - **Erros** em deploys blue-green (versão antiga vs nova)
13
+ - **Locks** em tabelas grandes (minutos a horas)
14
+ - **Rollback impossível** após aplicar destructive changes
15
+
16
+ ---
17
+
18
+ ## Princípios Fundamentais
19
+
20
+ ### 1. Backward Compatibility
21
+
22
+ Toda migration deve ser compatível com a versão **anterior** do código por pelo menos um ciclo de deploy.
23
+
24
+ ```
25
+ Deploy N: Código v1 + Schema v1
26
+ Deploy N+1: Código v2 + Schema v2 (compatível com v1)
27
+ Deploy N+2: Código v2 + Schema v3 (pode remover compatibilidade)
28
+ ```
29
+
30
+ ### 2. Expand-Contract Pattern
31
+
32
+ Nunca faça alterações destrutivas diretamente.
33
+
34
+ ```mermaid
35
+ graph LR
36
+ A[Estado Inicial] -->|1. Expand| B[Adiciona novo]
37
+ B -->|2. Migrate| C[Transfere dados]
38
+ C -->|3. Contract| D[Remove antigo]
39
+ ```
40
+
41
+ ### 3. Small, Incremental Changes
42
+
43
+ - Uma alteração por migration
44
+ - Migrations devem ser rápidas (< 1 segundo em tabelas grandes)
45
+ - Rollback sempre possível
46
+
47
+ ---
48
+
49
+ ## Padrões de Migration
50
+
51
+ ### Adicionar Coluna (Seguro ✅)
52
+
53
+ ```sql
54
+ -- Step 1: Adicionar coluna nullable ou com default
55
+ ALTER TABLE users ADD COLUMN phone VARCHAR(20);
56
+
57
+ -- Step 2: (Código) Deploy versão que escreve na nova coluna
58
+ -- Step 3: (Job) Preencher dados antigos se necessário
59
+ -- Step 4: (Código) Deploy versão que lê da nova coluna
60
+ -- Step 5: (Se necessário) Tornar NOT NULL
61
+ ALTER TABLE users ALTER COLUMN phone SET NOT NULL;
62
+ ```
63
+
64
+ ### Renomear Coluna (⚠️ Requer Cuidado)
65
+
66
+ **❌ NUNCA FAÇA:**
67
+ ```sql
68
+ ALTER TABLE users RENAME COLUMN name TO full_name;
69
+ ```
70
+
71
+ **✅ FAÇA EM 3 DEPLOYS:**
72
+
73
+ ```sql
74
+ -- Deploy 1: Adicionar nova coluna
75
+ ALTER TABLE users ADD COLUMN full_name VARCHAR(255);
76
+
77
+ -- Deploy 1: Trigger para sincronizar (opcional, ou fazer no código)
78
+ CREATE TRIGGER sync_name_columns
79
+ BEFORE INSERT OR UPDATE ON users
80
+ FOR EACH ROW
81
+ EXECUTE FUNCTION sync_name_to_full_name();
82
+ ```
83
+
84
+ ```python
85
+ # Deploy 2: Código escreve em ambas, lê da nova
86
+ def update_user(user_id, name):
87
+ db.execute("""
88
+ UPDATE users
89
+ SET name = %s, full_name = %s
90
+ WHERE id = %s
91
+ """, name, name, user_id)
92
+
93
+ def get_user(user_id):
94
+ # Lê da nova coluna
95
+ return db.query("SELECT full_name FROM users WHERE id = %s", user_id)
96
+ ```
97
+
98
+ ```sql
99
+ -- Deploy 3: Remover coluna antiga
100
+ ALTER TABLE users DROP COLUMN name;
101
+ ```
102
+
103
+ ### Remover Coluna (⚠️ 2 Deploys)
104
+
105
+ ```python
106
+ # Deploy 1: Código para de usar a coluna
107
+ # (mas schema ainda tem a coluna)
108
+ ```
109
+
110
+ ```sql
111
+ -- Deploy 2: Remover coluna (depois que código antigo não está mais ativo)
112
+ ALTER TABLE users DROP COLUMN legacy_field;
113
+ ```
114
+
115
+ ### Adicionar Índice (⚠️ Pode Bloquear)
116
+
117
+ **❌ Bloqueia tabela:**
118
+ ```sql
119
+ CREATE INDEX idx_users_email ON users(email);
120
+ ```
121
+
122
+ **✅ Não bloqueia (PostgreSQL):**
123
+ ```sql
124
+ CREATE INDEX CONCURRENTLY idx_users_email ON users(email);
125
+ ```
126
+
127
+ **✅ Não bloqueia (MySQL 8+):**
128
+ ```sql
129
+ ALTER TABLE users ADD INDEX idx_email (email), ALGORITHM=INPLACE, LOCK=NONE;
130
+ ```
131
+
132
+ ### Alterar Tipo de Coluna (⚠️ Complexo)
133
+
134
+ Use o Expand-Contract:
135
+
136
+ ```sql
137
+ -- Step 1: Adiciona nova coluna
138
+ ALTER TABLE orders ADD COLUMN amount_cents BIGINT;
139
+
140
+ -- Step 2: Trigger ou código para sincronizar
141
+ -- Step 3: Backfill job
142
+ UPDATE orders SET amount_cents = amount * 100 WHERE amount_cents IS NULL;
143
+
144
+ -- Step 4: Código usa nova coluna
145
+ -- Step 5: Remove coluna antiga
146
+ ALTER TABLE orders DROP COLUMN amount;
147
+ ```
148
+
149
+ ---
150
+
151
+ ## Migrations em Tabelas Grandes
152
+
153
+ Para tabelas com milhões de registros:
154
+
155
+ ### Técnica 1: Batch Processing
156
+
157
+ ```python
158
+ def backfill_in_batches(batch_size=1000):
159
+ while True:
160
+ affected = db.execute("""
161
+ UPDATE users
162
+ SET new_column = compute_value(old_column)
163
+ WHERE new_column IS NULL
164
+ LIMIT %s
165
+ """, batch_size)
166
+
167
+ if affected == 0:
168
+ break
169
+
170
+ time.sleep(0.1) # Evita sobrecarga
171
+ ```
172
+
173
+ ### Técnica 2: pt-online-schema-change (MySQL)
174
+
175
+ ```bash
176
+ pt-online-schema-change --execute \
177
+ --alter "ADD COLUMN phone VARCHAR(20)" \
178
+ D=mydb,t=users
179
+ ```
180
+
181
+ ### Técnica 3: gh-ost (MySQL)
182
+
183
+ ```bash
184
+ gh-ost \
185
+ --database=mydb \
186
+ --table=users \
187
+ --alter="ADD COLUMN phone VARCHAR(20)" \
188
+ --execute
189
+ ```
190
+
191
+ ### Técnica 4: pgroll (PostgreSQL)
192
+
193
+ ```yaml
194
+ # migration.yaml
195
+ operations:
196
+ - add_column:
197
+ table: users
198
+ column:
199
+ name: phone
200
+ type: varchar(20)
201
+ nullable: true
202
+ ```
203
+
204
+ ---
205
+
206
+ ## Feature Flags para Schema
207
+
208
+ Controle quais versões do código usam qual schema:
209
+
210
+ ```python
211
+ # config/feature_flags.py
212
+ SCHEMA_V2_ENABLED = os.getenv('SCHEMA_V2_ENABLED', 'false') == 'true'
213
+
214
+ # models/user.py
215
+ class User:
216
+ def get_display_name(self):
217
+ if SCHEMA_V2_ENABLED:
218
+ return self.full_name
219
+ return self.name
220
+ ```
221
+
222
+ ---
223
+
224
+ ## Rollback Strategies
225
+
226
+ ### Migrations Reversíveis
227
+
228
+ ```python
229
+ # Django
230
+ class Migration(migrations.Migration):
231
+ operations = [
232
+ migrations.AddField(
233
+ model_name='user',
234
+ name='phone',
235
+ field=models.CharField(max_length=20, null=True),
236
+ ),
237
+ ]
238
+
239
+ def reverse(self):
240
+ # Explícito: remover o campo
241
+ pass
242
+ ```
243
+
244
+ ### Sempre Tenha Backup
245
+
246
+ ```bash
247
+ # Antes de migrations em produção
248
+ pg_dump -Fc mydb > backup_before_migration_$(date +%Y%m%d).dump
249
+
250
+ # Verificar que backup é restaurável
251
+ pg_restore --list backup.dump
252
+ ```
253
+
254
+ ---
255
+
256
+ ## Checklist de Migration
257
+
258
+ ### Antes da Migration
259
+
260
+ - [ ] Migration testada em ambiente de staging
261
+ - [ ] Tempo de execução medido em dataset similar ao prod
262
+ - [ ] Plano de rollback documentado
263
+ - [ ] Backup recente verificado
264
+ - [ ] Horário de baixo tráfego escolhido (se necessário)
265
+
266
+ ### Durante a Migration
267
+
268
+ - [ ] Monitorar locks e tempo de execução
269
+ - [ ] Monitorar CPU/memória do banco
270
+ - [ ] Logs de aplicação para erros
271
+ - [ ] Comunicação com time se demorar
272
+
273
+ ### Após a Migration
274
+
275
+ - [ ] Verificar integridade dos dados
276
+ - [ ] Remover código/colunas legadas no próximo deploy
277
+ - [ ] Atualizar documentação de schema
278
+
279
+ ---
280
+
281
+ ## Ferramentas por Stack
282
+
283
+ | Stack | Ferramenta | Recursos |
284
+ |-------|-----------|----------|
285
+ | **PostgreSQL** | pgroll, Flyway | Online DDL, versioning |
286
+ | **MySQL** | gh-ost, pt-osc | Zero-downtime ALTERs |
287
+ | **Rails** | strong_migrations | Bloqueia migrations perigosas |
288
+ | **Django** | django-pg-zero-downtime-migrations | Checks automáticos |
289
+ | **Node.js** | TypeORM, Prisma | Migrations declarativas |
290
+ | **Java** | Flyway, Liquibase | Enterprise-ready |
291
+
292
+ ---
293
+
294
+ ## Anti-Patterns
295
+
296
+ | ❌ Não Faça | ✅ Faça Assim |
297
+ |-------------|---------------|
298
+ | Renomear coluna diretamente | Expand-Contract em 3 deploys |
299
+ | Alterar tipo diretamente | Nova coluna + migrar dados |
300
+ | DROP COLUMN com código usando | Remover uso no código primeiro |
301
+ | CREATE INDEX sem CONCURRENTLY | Usar CONCURRENTLY ou online DDL |
302
+ | Múltiplas alterações em uma migration | Uma alteração por migration |
303
+
304
+ ---
305
+
306
+ ## Referências
307
+
308
+ - [Stripe - Online Migrations at Scale](https://stripe.com/blog/online-migrations)
309
+ - [GitHub - gh-ost](https://github.com/github/gh-ost)
310
+ - [PostgreSQL - ALTER TABLE](https://www.postgresql.org/docs/current/sql-altertable.html)
311
+ - [Strong Migrations (Rails)](https://github.com/ankane/strong_migrations)
@@ -0,0 +1,368 @@
1
+ # Guia de Multi-tenancy
2
+
3
+ > **Prioridade**: 🟡 MÉDIA
4
+ > **Aplicável a**: SaaS, plataformas com múltiplos clientes/organizações
5
+
6
+ ---
7
+
8
+ ## O que é Multi-tenancy?
9
+
10
+ Multi-tenancy é uma arquitetura onde uma única instância de software serve múltiplos clientes (tenants) com isolamento de dados.
11
+
12
+ ```
13
+ ┌─────────────────────────────────────────────────────────┐
14
+ │ Aplicação SaaS │
15
+ ├─────────────────────────────────────────────────────────┤
16
+ │ Tenant A │ Tenant B │ Tenant C │ Tenant D │
17
+ │ (Empresa X)│ (Empresa Y) │ (Startup Z)│ (Corp W) │
18
+ └─────────────────────────────────────────────────────────┘
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Modelos de Isolamento
24
+
25
+ ### 1. Database per Tenant
26
+
27
+ Cada tenant tem seu próprio banco de dados.
28
+
29
+ ```mermaid
30
+ graph TD
31
+ App[Aplicação] --> DB1[(Tenant A DB)]
32
+ App --> DB2[(Tenant B DB)]
33
+ App --> DB3[(Tenant C DB)]
34
+ ```
35
+
36
+ | Prós | Contras |
37
+ |------|---------|
38
+ | Isolamento máximo | Maior custo de infra |
39
+ | Fácil backups por tenant | Complexidade operacional |
40
+ | Boa para compliance | Migrations em muitos DBs |
41
+ | Customização por tenant | Não escala para muitos tenants |
42
+
43
+ **Usar quando**: Enterprise customers, compliance rígido (HIPAA, PCI), <100 tenants
44
+
45
+ ### 2. Schema per Tenant
46
+
47
+ Um banco, mas schemas separados por tenant.
48
+
49
+ ```mermaid
50
+ graph TD
51
+ App[Aplicação] --> DB[(Database)]
52
+ DB --> S1[schema_tenant_a]
53
+ DB --> S2[schema_tenant_b]
54
+ DB --> S3[schema_tenant_c]
55
+ ```
56
+
57
+ | Prós | Contras |
58
+ |------|---------|
59
+ | Bom isolamento | Migrations por schema |
60
+ | Mais barato que DB separado | Limite de schemas |
61
+ | Fácil backup por tenant | Consultas cross-tenant complexas |
62
+
63
+ **Usar quando**: Poucas centenas de tenants, isolamento importante
64
+
65
+ ### 3. Row-Level Security (Shared Database)
66
+
67
+ Todos os tenants na mesma tabela, filtrados por tenant_id.
68
+
69
+ ```sql
70
+ -- Todas as tabelas têm tenant_id
71
+ CREATE TABLE orders (
72
+ id UUID PRIMARY KEY,
73
+ tenant_id UUID NOT NULL,
74
+ customer_name VARCHAR(255),
75
+ ...
76
+ );
77
+
78
+ CREATE INDEX idx_orders_tenant ON orders(tenant_id);
79
+ ```
80
+
81
+ | Prós | Contras |
82
+ |------|---------|
83
+ | Escala para milhares de tenants | Risco de vazamento de dados |
84
+ | Operação simples | Queries precisam sempre filtrar |
85
+ | Migrations únicas | Backup por tenant é complexo |
86
+ | Menor custo | Menos isolamento de performance |
87
+
88
+ **Usar quando**: Muitos tenants pequenos (1000+), dados similares, startups
89
+
90
+ ### 4. Híbrido
91
+
92
+ Tenants pequenos compartilham, grandes têm DB próprio.
93
+
94
+ ```mermaid
95
+ graph TD
96
+ App[Aplicação] --> DB1[(Shared DB)]
97
+ App --> DB2[(Enterprise A DB)]
98
+ App --> DB3[(Enterprise B DB)]
99
+ DB1 --> T1[tenant_1..99]
100
+ ```
101
+
102
+ **Usar quando**: Freemium + Enterprise, mix de necessidades
103
+
104
+ ---
105
+
106
+ ## Implementação: Row-Level Security
107
+
108
+ ### PostgreSQL RLS
109
+
110
+ ```sql
111
+ -- Habilitar RLS
112
+ ALTER TABLE orders ENABLE ROW LEVEL SECURITY;
113
+
114
+ -- Policy: usuários só veem dados do seu tenant
115
+ CREATE POLICY tenant_isolation ON orders
116
+ FOR ALL
117
+ USING (tenant_id = current_setting('app.current_tenant')::uuid);
118
+
119
+ -- No código, antes de cada requisição:
120
+ -- SET app.current_tenant = 'tenant-uuid';
121
+ ```
122
+
123
+ ### Middleware de Tenant (Node.js)
124
+
125
+ ```typescript
126
+ // middleware/tenantContext.ts
127
+ import { AsyncLocalStorage } from 'async_hooks';
128
+
129
+ interface TenantContext {
130
+ tenantId: string;
131
+ tenantConfig?: TenantConfig;
132
+ }
133
+
134
+ export const tenantStorage = new AsyncLocalStorage<TenantContext>();
135
+
136
+ export function tenantMiddleware(req, res, next) {
137
+ // Extrair tenant de: subdomain, header, JWT, etc.
138
+ const tenantId = extractTenantId(req);
139
+
140
+ if (!tenantId) {
141
+ return res.status(400).json({ error: 'Tenant not identified' });
142
+ }
143
+
144
+ tenantStorage.run({ tenantId }, () => {
145
+ // Setar no Prisma/TypeORM para RLS
146
+ db.$executeRaw`SET app.current_tenant = ${tenantId}`;
147
+ next();
148
+ });
149
+ }
150
+
151
+ function extractTenantId(req): string | null {
152
+ // Opção 1: Subdomain (tenant1.app.com)
153
+ const subdomain = req.hostname.split('.')[0];
154
+ if (subdomain !== 'www' && subdomain !== 'app') {
155
+ return getTenantIdBySubdomain(subdomain);
156
+ }
157
+
158
+ // Opção 2: Header
159
+ if (req.headers['x-tenant-id']) {
160
+ return req.headers['x-tenant-id'];
161
+ }
162
+
163
+ // Opção 3: JWT claim
164
+ if (req.user?.tenantId) {
165
+ return req.user.tenantId;
166
+ }
167
+
168
+ return null;
169
+ }
170
+
171
+ // Helper para acessar tenant em qualquer lugar
172
+ export function getCurrentTenant(): TenantContext {
173
+ const context = tenantStorage.getStore();
174
+ if (!context) throw new Error('No tenant context');
175
+ return context;
176
+ }
177
+ ```
178
+
179
+ ### Repository Pattern com Tenant Scope
180
+
181
+ ```typescript
182
+ // repositories/BaseRepository.ts
183
+ import { getCurrentTenant } from '../middleware/tenantContext';
184
+
185
+ export abstract class BaseRepository<T> {
186
+ protected abstract tableName: string;
187
+
188
+ async findAll(): Promise<T[]> {
189
+ const { tenantId } = getCurrentTenant();
190
+ return db.query(
191
+ `SELECT * FROM ${this.tableName} WHERE tenant_id = $1`,
192
+ [tenantId]
193
+ );
194
+ }
195
+
196
+ async findById(id: string): Promise<T | null> {
197
+ const { tenantId } = getCurrentTenant();
198
+ return db.queryOne(
199
+ `SELECT * FROM ${this.tableName} WHERE id = $1 AND tenant_id = $2`,
200
+ [id, tenantId]
201
+ );
202
+ }
203
+
204
+ async create(data: Omit<T, 'id' | 'tenant_id'>): Promise<T> {
205
+ const { tenantId } = getCurrentTenant();
206
+ return db.insert(this.tableName, { ...data, tenant_id: tenantId });
207
+ }
208
+ }
209
+
210
+ // Uso
211
+ class OrderRepository extends BaseRepository<Order> {
212
+ tableName = 'orders';
213
+
214
+ async findByStatus(status: string): Promise<Order[]> {
215
+ const { tenantId } = getCurrentTenant();
216
+ return db.query(
217
+ `SELECT * FROM orders WHERE status = $1 AND tenant_id = $2`,
218
+ [status, tenantId]
219
+ );
220
+ }
221
+ }
222
+ ```
223
+
224
+ ---
225
+
226
+ ## Identificação de Tenant
227
+
228
+ | Método | Exemplo | Prós | Contras |
229
+ |--------|---------|------|---------|
230
+ | **Subdomain** | `acme.app.com` | Óbvio para usuário | DNS/SSL por tenant |
231
+ | **Path** | `app.com/acme/...` | Simples | Polui URLs |
232
+ | **Header** | `X-Tenant-ID` | Limpo | Só para APIs |
233
+ | **JWT Claim** | `{ tenantId: '...' }` | Seguro, autenticado | Precisa de auth |
234
+ | **Query Param** | `?tenant=acme` | Fácil testar | Fácil vazar |
235
+
236
+ **Recomendação**: Subdomain para apps, Header/JWT para APIs.
237
+
238
+ ---
239
+
240
+ ## Customização por Tenant
241
+
242
+ ### Configurações
243
+
244
+ ```typescript
245
+ interface TenantConfig {
246
+ id: string;
247
+ name: string;
248
+ subdomain: string;
249
+ plan: 'free' | 'pro' | 'enterprise';
250
+ features: {
251
+ maxUsers: number;
252
+ customBranding: boolean;
253
+ ssoEnabled: boolean;
254
+ apiAccess: boolean;
255
+ };
256
+ branding?: {
257
+ logo: string;
258
+ primaryColor: string;
259
+ };
260
+ }
261
+
262
+ // Cache de config por tenant
263
+ const tenantConfigCache = new Map<string, TenantConfig>();
264
+
265
+ async function getTenantConfig(tenantId: string): Promise<TenantConfig> {
266
+ if (tenantConfigCache.has(tenantId)) {
267
+ return tenantConfigCache.get(tenantId)!;
268
+ }
269
+
270
+ const config = await db.tenants.findById(tenantId);
271
+ tenantConfigCache.set(tenantId, config);
272
+ return config;
273
+ }
274
+ ```
275
+
276
+ ### Feature Flags por Tenant
277
+
278
+ ```typescript
279
+ async function hasFeature(feature: string): Promise<boolean> {
280
+ const { tenantId } = getCurrentTenant();
281
+ const config = await getTenantConfig(tenantId);
282
+ return config.features[feature] === true;
283
+ }
284
+
285
+ // Uso
286
+ if (await hasFeature('ssoEnabled')) {
287
+ // Mostrar opções de SSO
288
+ }
289
+ ```
290
+
291
+ ---
292
+
293
+ ## Segurança
294
+
295
+ ### Checklist de Isolamento
296
+
297
+ - [ ] Todas as queries filtram por tenant_id
298
+ - [ ] Índices incluem tenant_id
299
+ - [ ] Uploads/Storage segregados por tenant
300
+ - [ ] Logs incluem tenant para auditoria
301
+ - [ ] Rate limiting por tenant
302
+ - [ ] Testes verificam isolamento
303
+
304
+ ### Testes de Isolamento
305
+
306
+ ```typescript
307
+ describe('Tenant Isolation', () => {
308
+ it('should not access data from other tenants', async () => {
309
+ // Setup: criar dados em dois tenants
310
+ await asTenant('tenant-a', async () => {
311
+ await orderRepo.create({ product: 'A' });
312
+ });
313
+
314
+ await asTenant('tenant-b', async () => {
315
+ await orderRepo.create({ product: 'B' });
316
+ });
317
+
318
+ // Verificar isolamento
319
+ await asTenant('tenant-a', async () => {
320
+ const orders = await orderRepo.findAll();
321
+ expect(orders.every(o => o.tenant_id === 'tenant-a')).toBe(true);
322
+ expect(orders.find(o => o.product === 'B')).toBeUndefined();
323
+ });
324
+ });
325
+ });
326
+ ```
327
+
328
+ ---
329
+
330
+ ## Migrations Multi-tenant
331
+
332
+ ### Para Shared Database
333
+
334
+ ```sql
335
+ -- Migration normal - aplica para todos
336
+ ALTER TABLE orders ADD COLUMN priority INT DEFAULT 0;
337
+ ```
338
+
339
+ ### Para Schema per Tenant
340
+
341
+ ```bash
342
+ # Script para aplicar migration em todos os schemas
343
+ for schema in $(psql -t -c "SELECT schema_name FROM information_schema.schemata WHERE schema_name LIKE 'tenant_%'"); do
344
+ psql -c "SET search_path TO $schema; -- migration SQL here;"
345
+ done
346
+ ```
347
+
348
+ ---
349
+
350
+ ## Checklist de Implementação
351
+
352
+ - [ ] Modelo de isolamento escolhido e documentado
353
+ - [ ] Middleware de identificação de tenant
354
+ - [ ] Todas as queries incluem tenant_id
355
+ - [ ] Testes de isolamento automatizados
356
+ - [ ] Config/features por tenant
357
+ - [ ] Rate limiting por tenant
358
+ - [ ] Logs e métricas por tenant
359
+ - [ ] Backup/restore por tenant (se necessário)
360
+ - [ ] Provisioning de novo tenant automatizado
361
+
362
+ ---
363
+
364
+ ## Referências
365
+
366
+ - [Multi-tenancy Patterns (Microsoft)](https://docs.microsoft.com/en-us/azure/architecture/guide/multitenant/overview)
367
+ - [PostgreSQL Row-Level Security](https://www.postgresql.org/docs/current/ddl-rowsecurity.html)
368
+ - [Multi-tenant SaaS Patterns (AWS)](https://aws.amazon.com/solutions/implementations/saas-tenant-isolation/)