@weirdfingers/baseboards 0.2.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 (139) hide show
  1. package/README.md +191 -0
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.js +887 -0
  4. package/dist/index.js.map +1 -0
  5. package/package.json +64 -0
  6. package/templates/README.md +120 -0
  7. package/templates/api/.env.example +62 -0
  8. package/templates/api/Dockerfile +32 -0
  9. package/templates/api/README.md +132 -0
  10. package/templates/api/alembic/env.py +106 -0
  11. package/templates/api/alembic/script.py.mako +28 -0
  12. package/templates/api/alembic/versions/20250101_000000_initial_schema.py +448 -0
  13. package/templates/api/alembic/versions/20251022_174729_remove_provider_name_from_generations.py +71 -0
  14. package/templates/api/alembic/versions/20251023_165852_switch_to_declarative_base_and_mapping.py +411 -0
  15. package/templates/api/alembic/versions/2025925_62735_add_seed_data_for_default_tenant.py +85 -0
  16. package/templates/api/alembic.ini +36 -0
  17. package/templates/api/config/generators.yaml +25 -0
  18. package/templates/api/config/storage_config.yaml +26 -0
  19. package/templates/api/docs/ADDING_GENERATORS.md +409 -0
  20. package/templates/api/docs/GENERATORS_API.md +502 -0
  21. package/templates/api/docs/MIGRATIONS.md +472 -0
  22. package/templates/api/docs/storage_providers.md +337 -0
  23. package/templates/api/pyproject.toml +165 -0
  24. package/templates/api/src/boards/__init__.py +10 -0
  25. package/templates/api/src/boards/api/app.py +171 -0
  26. package/templates/api/src/boards/api/auth.py +75 -0
  27. package/templates/api/src/boards/api/endpoints/__init__.py +3 -0
  28. package/templates/api/src/boards/api/endpoints/jobs.py +76 -0
  29. package/templates/api/src/boards/api/endpoints/setup.py +505 -0
  30. package/templates/api/src/boards/api/endpoints/sse.py +129 -0
  31. package/templates/api/src/boards/api/endpoints/storage.py +74 -0
  32. package/templates/api/src/boards/api/endpoints/tenant_registration.py +296 -0
  33. package/templates/api/src/boards/api/endpoints/webhooks.py +13 -0
  34. package/templates/api/src/boards/auth/__init__.py +15 -0
  35. package/templates/api/src/boards/auth/adapters/__init__.py +20 -0
  36. package/templates/api/src/boards/auth/adapters/auth0.py +220 -0
  37. package/templates/api/src/boards/auth/adapters/base.py +73 -0
  38. package/templates/api/src/boards/auth/adapters/clerk.py +172 -0
  39. package/templates/api/src/boards/auth/adapters/jwt.py +122 -0
  40. package/templates/api/src/boards/auth/adapters/none.py +102 -0
  41. package/templates/api/src/boards/auth/adapters/oidc.py +284 -0
  42. package/templates/api/src/boards/auth/adapters/supabase.py +110 -0
  43. package/templates/api/src/boards/auth/context.py +35 -0
  44. package/templates/api/src/boards/auth/factory.py +115 -0
  45. package/templates/api/src/boards/auth/middleware.py +221 -0
  46. package/templates/api/src/boards/auth/provisioning.py +129 -0
  47. package/templates/api/src/boards/auth/tenant_extraction.py +278 -0
  48. package/templates/api/src/boards/cli.py +354 -0
  49. package/templates/api/src/boards/config.py +116 -0
  50. package/templates/api/src/boards/database/__init__.py +7 -0
  51. package/templates/api/src/boards/database/cli.py +110 -0
  52. package/templates/api/src/boards/database/connection.py +252 -0
  53. package/templates/api/src/boards/database/models.py +19 -0
  54. package/templates/api/src/boards/database/seed_data.py +182 -0
  55. package/templates/api/src/boards/dbmodels/__init__.py +455 -0
  56. package/templates/api/src/boards/generators/__init__.py +57 -0
  57. package/templates/api/src/boards/generators/artifacts.py +53 -0
  58. package/templates/api/src/boards/generators/base.py +140 -0
  59. package/templates/api/src/boards/generators/implementations/__init__.py +12 -0
  60. package/templates/api/src/boards/generators/implementations/audio/__init__.py +3 -0
  61. package/templates/api/src/boards/generators/implementations/audio/whisper.py +66 -0
  62. package/templates/api/src/boards/generators/implementations/image/__init__.py +3 -0
  63. package/templates/api/src/boards/generators/implementations/image/dalle3.py +93 -0
  64. package/templates/api/src/boards/generators/implementations/image/flux_pro.py +85 -0
  65. package/templates/api/src/boards/generators/implementations/video/__init__.py +3 -0
  66. package/templates/api/src/boards/generators/implementations/video/lipsync.py +70 -0
  67. package/templates/api/src/boards/generators/loader.py +253 -0
  68. package/templates/api/src/boards/generators/registry.py +114 -0
  69. package/templates/api/src/boards/generators/resolution.py +515 -0
  70. package/templates/api/src/boards/generators/testmods/class_gen.py +34 -0
  71. package/templates/api/src/boards/generators/testmods/import_side_effect.py +35 -0
  72. package/templates/api/src/boards/graphql/__init__.py +7 -0
  73. package/templates/api/src/boards/graphql/access_control.py +136 -0
  74. package/templates/api/src/boards/graphql/mutations/root.py +136 -0
  75. package/templates/api/src/boards/graphql/queries/root.py +116 -0
  76. package/templates/api/src/boards/graphql/resolvers/__init__.py +8 -0
  77. package/templates/api/src/boards/graphql/resolvers/auth.py +12 -0
  78. package/templates/api/src/boards/graphql/resolvers/board.py +1055 -0
  79. package/templates/api/src/boards/graphql/resolvers/generation.py +889 -0
  80. package/templates/api/src/boards/graphql/resolvers/generator.py +50 -0
  81. package/templates/api/src/boards/graphql/resolvers/user.py +25 -0
  82. package/templates/api/src/boards/graphql/schema.py +81 -0
  83. package/templates/api/src/boards/graphql/types/board.py +102 -0
  84. package/templates/api/src/boards/graphql/types/generation.py +130 -0
  85. package/templates/api/src/boards/graphql/types/generator.py +17 -0
  86. package/templates/api/src/boards/graphql/types/user.py +47 -0
  87. package/templates/api/src/boards/jobs/repository.py +104 -0
  88. package/templates/api/src/boards/logging.py +195 -0
  89. package/templates/api/src/boards/middleware.py +339 -0
  90. package/templates/api/src/boards/progress/__init__.py +4 -0
  91. package/templates/api/src/boards/progress/models.py +25 -0
  92. package/templates/api/src/boards/progress/publisher.py +64 -0
  93. package/templates/api/src/boards/py.typed +0 -0
  94. package/templates/api/src/boards/redis_pool.py +118 -0
  95. package/templates/api/src/boards/storage/__init__.py +52 -0
  96. package/templates/api/src/boards/storage/base.py +363 -0
  97. package/templates/api/src/boards/storage/config.py +187 -0
  98. package/templates/api/src/boards/storage/factory.py +278 -0
  99. package/templates/api/src/boards/storage/implementations/__init__.py +27 -0
  100. package/templates/api/src/boards/storage/implementations/gcs.py +340 -0
  101. package/templates/api/src/boards/storage/implementations/local.py +201 -0
  102. package/templates/api/src/boards/storage/implementations/s3.py +294 -0
  103. package/templates/api/src/boards/storage/implementations/supabase.py +218 -0
  104. package/templates/api/src/boards/tenant_isolation.py +446 -0
  105. package/templates/api/src/boards/validation.py +262 -0
  106. package/templates/api/src/boards/workers/__init__.py +1 -0
  107. package/templates/api/src/boards/workers/actors.py +201 -0
  108. package/templates/api/src/boards/workers/cli.py +125 -0
  109. package/templates/api/src/boards/workers/context.py +188 -0
  110. package/templates/api/src/boards/workers/middleware.py +58 -0
  111. package/templates/api/src/py.typed +0 -0
  112. package/templates/compose.dev.yaml +39 -0
  113. package/templates/compose.yaml +109 -0
  114. package/templates/docker/env.example +23 -0
  115. package/templates/web/.env.example +28 -0
  116. package/templates/web/Dockerfile +51 -0
  117. package/templates/web/components.json +22 -0
  118. package/templates/web/imageLoader.js +18 -0
  119. package/templates/web/next-env.d.ts +5 -0
  120. package/templates/web/next.config.js +36 -0
  121. package/templates/web/package.json +37 -0
  122. package/templates/web/postcss.config.mjs +7 -0
  123. package/templates/web/public/favicon.ico +0 -0
  124. package/templates/web/src/app/boards/[boardId]/page.tsx +232 -0
  125. package/templates/web/src/app/globals.css +120 -0
  126. package/templates/web/src/app/layout.tsx +21 -0
  127. package/templates/web/src/app/page.tsx +35 -0
  128. package/templates/web/src/app/providers.tsx +18 -0
  129. package/templates/web/src/components/boards/ArtifactInputSlots.tsx +142 -0
  130. package/templates/web/src/components/boards/ArtifactPreview.tsx +125 -0
  131. package/templates/web/src/components/boards/GenerationGrid.tsx +45 -0
  132. package/templates/web/src/components/boards/GenerationInput.tsx +251 -0
  133. package/templates/web/src/components/boards/GeneratorSelector.tsx +89 -0
  134. package/templates/web/src/components/header.tsx +30 -0
  135. package/templates/web/src/components/ui/button.tsx +58 -0
  136. package/templates/web/src/components/ui/card.tsx +92 -0
  137. package/templates/web/src/components/ui/navigation-menu.tsx +168 -0
  138. package/templates/web/src/lib/utils.ts +6 -0
  139. package/templates/web/tsconfig.json +47 -0
@@ -0,0 +1,354 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ Main CLI entry point for Boards backend server.
4
+ """
5
+
6
+ import os
7
+ import sys
8
+
9
+ import click
10
+ import uvicorn
11
+
12
+ from boards import __version__
13
+ from boards.logging import configure_logging, get_logger
14
+
15
+ logger = get_logger(__name__)
16
+
17
+
18
+ @click.group()
19
+ @click.version_option(version=__version__, prog_name="boards")
20
+ def cli() -> None:
21
+ """Boards CLI - manage server, database, and tenants."""
22
+ pass
23
+
24
+
25
+ @cli.command()
26
+ @click.option(
27
+ "--host",
28
+ default="0.0.0.0",
29
+ help="Host to bind to (default: 0.0.0.0)",
30
+ )
31
+ @click.option(
32
+ "--port",
33
+ default=8088,
34
+ type=int,
35
+ help="Port to bind to (default: 8088)",
36
+ )
37
+ @click.option(
38
+ "--reload",
39
+ is_flag=True,
40
+ default=False,
41
+ help="Enable auto-reload for development",
42
+ )
43
+ @click.option(
44
+ "--workers",
45
+ default=1,
46
+ type=int,
47
+ help="Number of worker processes (default: 1)",
48
+ )
49
+ @click.option(
50
+ "--log-level",
51
+ default="info",
52
+ type=click.Choice(["debug", "info", "warning", "error"]),
53
+ help="Log level (default: info)",
54
+ )
55
+ def serve(
56
+ host: str,
57
+ port: int,
58
+ reload: bool,
59
+ workers: int,
60
+ log_level: str,
61
+ ) -> None:
62
+ """Start the Boards API server."""
63
+
64
+ # Configure logging
65
+ configure_logging(debug=(log_level == "debug"))
66
+
67
+ logger.info(
68
+ "Starting Boards API server",
69
+ host=host,
70
+ port=port,
71
+ reload=reload,
72
+ workers=workers,
73
+ log_level=log_level,
74
+ )
75
+
76
+ # Set environment variables for app configuration when using reload/workers
77
+ # This ensures the app imports with the correct settings
78
+ if log_level == "debug":
79
+ os.environ["BOARDS_DEBUG"] = "true"
80
+ os.environ["BOARDS_LOG_LEVEL"] = "debug"
81
+ else:
82
+ os.environ.setdefault("BOARDS_DEBUG", "false")
83
+ os.environ.setdefault("BOARDS_LOG_LEVEL", log_level)
84
+
85
+ try:
86
+ # When using reload or multiple workers, pass app as import string
87
+ if reload or workers > 1:
88
+ uvicorn.run(
89
+ "boards.api.app:app",
90
+ host=host,
91
+ port=port,
92
+ reload=reload,
93
+ workers=(workers if not reload else 1), # reload doesn't work with multiple workers
94
+ log_level=log_level,
95
+ access_log=True,
96
+ )
97
+ else:
98
+ # Import app directly when not using reload/workers
99
+ from boards.api.app import app
100
+
101
+ uvicorn.run(
102
+ app,
103
+ host=host,
104
+ port=port,
105
+ reload=reload,
106
+ workers=workers,
107
+ log_level=log_level,
108
+ access_log=True,
109
+ )
110
+ except KeyboardInterrupt:
111
+ logger.info("Server shutdown requested by user")
112
+ except Exception as e:
113
+ logger.error("Server startup failed", error=str(e))
114
+ sys.exit(1)
115
+
116
+
117
+ @cli.group()
118
+ def tenant() -> None:
119
+ """Manage tenants in the database."""
120
+ pass
121
+
122
+
123
+ @tenant.command("create")
124
+ @click.option(
125
+ "--name",
126
+ required=True,
127
+ help="Display name for the tenant",
128
+ )
129
+ @click.option(
130
+ "--slug",
131
+ required=True,
132
+ help="Unique slug for the tenant (used in URLs)",
133
+ )
134
+ @click.option(
135
+ "--sample-data",
136
+ is_flag=True,
137
+ default=False,
138
+ help="Include sample data for the tenant",
139
+ )
140
+ def create_tenant(
141
+ name: str,
142
+ slug: str,
143
+ sample_data: bool,
144
+ ) -> None:
145
+ """Create a new tenant in the database."""
146
+ import asyncio
147
+
148
+ from boards.database.connection import get_async_session
149
+ from boards.database.seed_data import seed_tenant_with_data
150
+
151
+ configure_logging()
152
+
153
+ async def do_create():
154
+ async with get_async_session() as db:
155
+ try:
156
+ tenant_id = await seed_tenant_with_data(
157
+ db,
158
+ tenant_name=name,
159
+ tenant_slug=slug,
160
+ include_sample_data=sample_data,
161
+ )
162
+ logger.info(
163
+ "Tenant created successfully",
164
+ tenant_id=str(tenant_id),
165
+ name=name,
166
+ slug=slug,
167
+ )
168
+ click.echo(f"✓ Tenant created: {tenant_id}")
169
+ click.echo(f" Name: {name}")
170
+ click.echo(f" Slug: {slug}")
171
+ if sample_data:
172
+ click.echo(" Sample data: included")
173
+ except Exception as e:
174
+ logger.error("Failed to create tenant", error=str(e))
175
+ click.echo(f"✗ Error creating tenant: {e}", err=True)
176
+ sys.exit(1)
177
+
178
+ asyncio.run(do_create())
179
+
180
+
181
+ @tenant.command("list")
182
+ def list_tenants() -> None:
183
+ """List all tenants in the database."""
184
+ import asyncio
185
+
186
+ from sqlalchemy import select
187
+
188
+ from boards.database.connection import get_async_session
189
+ from boards.dbmodels import Tenants
190
+
191
+ configure_logging()
192
+
193
+ async def do_list():
194
+ async with get_async_session() as db:
195
+ try:
196
+ stmt = select(Tenants).order_by(Tenants.created_at)
197
+ result = await db.execute(stmt)
198
+ tenants = result.scalars().all()
199
+
200
+ if not tenants:
201
+ click.echo("No tenants found.")
202
+ else:
203
+ click.echo(f"Found {len(tenants)} tenant(s):")
204
+ click.echo()
205
+ for t in tenants:
206
+ click.echo(f" ID: {t.id}")
207
+ click.echo(f" Name: {t.name}")
208
+ click.echo(f" Slug: {t.slug}")
209
+ click.echo(f" Created: {t.created_at}")
210
+ click.echo()
211
+ except Exception as e:
212
+ logger.error("Failed to list tenants", error=str(e))
213
+ click.echo(f"✗ Error listing tenants: {e}", err=True)
214
+ sys.exit(1)
215
+
216
+ asyncio.run(do_list())
217
+
218
+
219
+ @tenant.command("audit")
220
+ @click.option(
221
+ "--tenant-slug",
222
+ help="Slug of specific tenant to audit (audits all if not specified)",
223
+ )
224
+ @click.option(
225
+ "--output-format",
226
+ default="table",
227
+ type=click.Choice(["table", "json"]),
228
+ help="Output format (default: table)",
229
+ )
230
+ def audit_tenant_isolation(tenant_slug: str | None, output_format: str) -> None:
231
+ """Audit tenant isolation for security validation."""
232
+ import asyncio
233
+ import json
234
+
235
+ from sqlalchemy import select
236
+
237
+ from boards.database.connection import get_async_session
238
+ from boards.dbmodels import Tenants
239
+ from boards.tenant_isolation import TenantIsolationValidator
240
+
241
+ configure_logging()
242
+
243
+ async def do_audit():
244
+ async with get_async_session() as db:
245
+ try:
246
+ validator = TenantIsolationValidator(db)
247
+
248
+ # Determine which tenants to audit
249
+ if tenant_slug:
250
+ stmt = select(Tenants).where(Tenants.slug == tenant_slug)
251
+ result = await db.execute(stmt)
252
+ tenant = result.scalar_one_or_none()
253
+ if not tenant:
254
+ click.echo(f"✗ Tenant '{tenant_slug}' not found", err=True)
255
+ sys.exit(1)
256
+ tenants_to_audit = [tenant]
257
+ else:
258
+ stmt = select(Tenants).order_by(Tenants.created_at)
259
+ result = await db.execute(stmt)
260
+ tenants_to_audit = result.scalars().all()
261
+
262
+ if not tenants_to_audit:
263
+ click.echo("No tenants found to audit.")
264
+ return
265
+
266
+ # Perform audits
267
+ audit_results = []
268
+ for tenant in tenants_to_audit:
269
+ click.echo(f"🔍 Auditing tenant: {tenant.slug}")
270
+ audit_result = await validator.audit_tenant_isolation(tenant.id)
271
+ audit_results.append(audit_result)
272
+
273
+ # Output results
274
+ if output_format == "json":
275
+ click.echo(json.dumps(audit_results, indent=2))
276
+ else:
277
+ _display_audit_results_table(audit_results)
278
+
279
+ except Exception as e:
280
+ logger.error("Failed to audit tenant isolation", error=str(e))
281
+ click.echo(f"✗ Error auditing tenants: {e}", err=True)
282
+ sys.exit(1)
283
+
284
+ def _display_audit_results_table(results):
285
+ """Display audit results in table format."""
286
+ click.echo("\n" + "=" * 80)
287
+ click.echo("TENANT ISOLATION AUDIT RESULTS")
288
+ click.echo("=" * 80)
289
+
290
+ for result in results:
291
+ violations = result["isolation_violations"]
292
+ stats = result["statistics"]
293
+
294
+ click.echo(f"\n📋 Tenant: {result['tenant_id']}")
295
+ click.echo(f" Audit Time: {result['audit_timestamp']}")
296
+
297
+ # Statistics
298
+ click.echo("\n📊 Statistics:")
299
+ click.echo(f" Users: {stats.get('users_count', 0)}")
300
+ click.echo(f" Boards: {stats.get('boards_count', 0)}")
301
+ click.echo(f" Generations: {stats.get('generations_count', 0)}")
302
+ click.echo(f" Board Memberships: {stats.get('board_memberships_count', 0)}")
303
+
304
+ # Violations
305
+ if violations:
306
+ click.echo(f"\n⚠️ Isolation Violations ({len(violations)}):")
307
+ for i, violation in enumerate(violations, 1):
308
+ click.echo(f" {i}. {violation['type']}: {violation['description']}")
309
+ else:
310
+ click.echo("\n✅ No isolation violations found")
311
+
312
+ # Recommendations
313
+ click.echo("\n💡 Recommendations:")
314
+ for rec in result["recommendations"]:
315
+ click.echo(f" • {rec}")
316
+
317
+ if result != results[-1]: # Not the last result
318
+ click.echo("\n" + "-" * 60)
319
+
320
+ click.echo("\n" + "=" * 80)
321
+
322
+ asyncio.run(do_audit())
323
+
324
+
325
+ @cli.command()
326
+ def seed() -> None:
327
+ """Seed the database with initial data."""
328
+ import asyncio
329
+
330
+ from boards.database.connection import get_async_session
331
+ from boards.database.seed_data import seed_initial_data
332
+
333
+ configure_logging()
334
+
335
+ async def do_seed():
336
+ async with get_async_session() as db:
337
+ try:
338
+ await seed_initial_data(db)
339
+ click.echo("✓ Database seeded successfully")
340
+ except Exception as e:
341
+ logger.error("Failed to seed database", error=str(e))
342
+ click.echo(f"✗ Error seeding database: {e}", err=True)
343
+ sys.exit(1)
344
+
345
+ asyncio.run(do_seed())
346
+
347
+
348
+ def main():
349
+ """Entry point for the CLI."""
350
+ cli()
351
+
352
+
353
+ if __name__ == "__main__":
354
+ cli()
@@ -0,0 +1,116 @@
1
+ """
2
+ Configuration management for Boards backend
3
+ """
4
+
5
+ import os
6
+
7
+ from pydantic_settings import BaseSettings
8
+
9
+
10
+ class Settings(BaseSettings):
11
+ """Application settings loaded from environment variables."""
12
+
13
+ # Database
14
+ database_url: str = "postgresql://boards:boards_dev@localhost:5433/boards_dev"
15
+ database_pool_size: int = 10
16
+ database_max_overflow: int = 20
17
+
18
+ # Redis (for job queue)
19
+ redis_url: str = "redis://localhost:6380"
20
+
21
+ # Storage
22
+ storage_config_path: str | None = None
23
+
24
+ # Auth
25
+ auth_provider: str = "none" # 'none', 'supabase', 'clerk', 'auth0', 'jwt'
26
+ auth_config: dict = {}
27
+ jwt_secret: str | None = None
28
+ jwt_algorithm: str = "HS256"
29
+ jwt_tenant_claim: str | None = None # Custom JWT claim for tenant extraction
30
+
31
+ # API Settings
32
+ api_host: str = "0.0.0.0"
33
+ api_port: int = 8088
34
+ api_reload: bool = False
35
+ cors_origins: list[str] = ["http://localhost:3033"]
36
+
37
+ # Generators Configuration
38
+ generators_config_path: str | None = None
39
+ generator_api_keys: dict[str, str] = {}
40
+
41
+ # Environment
42
+ environment: str = "development" # 'development', 'staging', 'production'
43
+ debug: bool = True
44
+ sql_echo: bool = False
45
+ log_level: str = "INFO"
46
+
47
+ # Tenant Settings (for multi-tenant mode)
48
+ multi_tenant_mode: bool = False
49
+ default_tenant_slug: str = "default"
50
+
51
+ # Tenant Registration Settings
52
+ tenant_registration_requires_approval: bool = False
53
+ tenant_registration_allowed_domains: list[str] | None = None
54
+ max_tenants_per_user: int | None = None
55
+
56
+ # Frontend Integration
57
+ frontend_base_url: str | None = None
58
+
59
+ # Job Queue Settings
60
+ job_queue_name: str = "boards-jobs"
61
+ job_timeout: int = 3600 # 1 hour default timeout
62
+
63
+ # File Upload Settings
64
+ max_upload_size: int = 100 * 1024 * 1024 # 100MB
65
+ allowed_upload_extensions: list[str] = [
66
+ ".jpg",
67
+ ".jpeg",
68
+ ".png",
69
+ ".gif",
70
+ ".webp", # Images
71
+ ".mp4",
72
+ ".mov",
73
+ ".avi",
74
+ ".webm", # Videos
75
+ ".mp3",
76
+ ".wav",
77
+ ".ogg",
78
+ ".m4a", # Audio
79
+ ".txt",
80
+ ".md",
81
+ ".json", # Text
82
+ ]
83
+
84
+ class Config:
85
+ env_file = ".env"
86
+ env_prefix = "BOARDS_"
87
+ case_sensitive = False
88
+
89
+ # Allow extra fields for provider-specific configs
90
+ extra = "allow"
91
+
92
+
93
+ # Global settings instance
94
+ settings = Settings()
95
+
96
+
97
+ def initialize_generator_api_keys() -> None:
98
+ """
99
+ Sync generator API keys from settings to os.environ.
100
+
101
+ This allows third-party packages (like replicate) that expect
102
+ environment variables to work correctly with our Pydantic settings.
103
+ """
104
+ for key, value in settings.generator_api_keys.items():
105
+ if value: # Only set non-empty values
106
+ os.environ[key] = value
107
+
108
+
109
+ # Helper functions
110
+ def get_database_url(tenant_slug: str | None = None) -> str:
111
+ """Get database URL, optionally with tenant-specific schema."""
112
+ if settings.multi_tenant_mode and tenant_slug:
113
+ # In multi-tenant mode, could use schemas or separate databases
114
+ # For now, we'll use the same database with tenant isolation via queries
115
+ return settings.database_url
116
+ return settings.database_url
@@ -0,0 +1,7 @@
1
+ """
2
+ Database module for Boards backend
3
+ """
4
+
5
+ from .connection import get_engine, get_session, init_database
6
+
7
+ __all__ = ["get_engine", "get_session", "init_database"]
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env python3
2
+ """
3
+ CLI entry point for Boards database migrations.
4
+ """
5
+
6
+ import sys
7
+ from pathlib import Path
8
+
9
+ import click
10
+
11
+ from alembic import command
12
+ from alembic.config import Config
13
+ from boards import __version__
14
+ from boards.logging import configure_logging, get_logger
15
+
16
+ logger = get_logger(__name__)
17
+
18
+
19
+ def get_alembic_config() -> Config:
20
+ """Get Alembic configuration."""
21
+ # Find alembic.ini in the package directory
22
+ package_dir = Path(__file__).parent.parent.parent.parent
23
+ alembic_ini = package_dir / "alembic.ini"
24
+
25
+ if not alembic_ini.exists():
26
+ raise FileNotFoundError(f"alembic.ini not found at {alembic_ini}")
27
+
28
+ return Config(str(alembic_ini))
29
+
30
+
31
+ @click.group()
32
+ @click.option(
33
+ "--log-level",
34
+ default="info",
35
+ type=click.Choice(["debug", "info", "warning", "error"]),
36
+ help="Log level (default: info)",
37
+ )
38
+ @click.version_option(version=__version__, prog_name="boards-migrate")
39
+ def main(log_level: str) -> None:
40
+ """Boards database migration management."""
41
+ configure_logging(debug=(log_level == "debug"))
42
+
43
+
44
+ @main.command()
45
+ @click.argument("revision", default="head")
46
+ def upgrade(revision: str) -> None:
47
+ """Upgrade database to a revision (default: head)."""
48
+ try:
49
+ config = get_alembic_config()
50
+ logger.info("Upgrading database", revision=revision)
51
+ command.upgrade(config, revision)
52
+ logger.info("Database upgrade completed successfully")
53
+ except Exception as e:
54
+ logger.error("Database upgrade failed", error=str(e))
55
+ sys.exit(1)
56
+
57
+
58
+ @main.command()
59
+ @click.argument("revision", default="-1")
60
+ def downgrade(revision: str) -> None:
61
+ """Downgrade database to a revision (default: -1)."""
62
+ try:
63
+ config = get_alembic_config()
64
+ logger.info("Downgrading database", revision=revision)
65
+ command.downgrade(config, revision)
66
+ logger.info("Database downgrade completed successfully")
67
+ except Exception as e:
68
+ logger.error("Database downgrade failed", error=str(e))
69
+ sys.exit(1)
70
+
71
+
72
+ @main.command()
73
+ @click.option("-m", "--message", required=True, help="Revision message")
74
+ @click.option("--autogenerate/--no-autogenerate", default=True, help="Auto-generate migration")
75
+ def revision(message: str, autogenerate: bool) -> None:
76
+ """Create a new migration revision."""
77
+ try:
78
+ config = get_alembic_config()
79
+ logger.info("Creating new migration", message=message, autogenerate=autogenerate)
80
+ command.revision(config, message=message, autogenerate=autogenerate)
81
+ logger.info("Migration created successfully")
82
+ except Exception as e:
83
+ logger.error("Migration creation failed", error=str(e))
84
+ sys.exit(1)
85
+
86
+
87
+ @main.command()
88
+ def current() -> None:
89
+ """Show current database revision."""
90
+ try:
91
+ config = get_alembic_config()
92
+ command.current(config)
93
+ except Exception as e:
94
+ logger.error("Failed to get current revision", error=str(e))
95
+ sys.exit(1)
96
+
97
+
98
+ @main.command()
99
+ def history() -> None:
100
+ """Show migration history."""
101
+ try:
102
+ config = get_alembic_config()
103
+ command.history(config)
104
+ except Exception as e:
105
+ logger.error("Failed to get migration history", error=str(e))
106
+ sys.exit(1)
107
+
108
+
109
+ if __name__ == "__main__":
110
+ main()