Database (Overview)
What lives where
This repo uses two different persistence layers for two different jobs:
-
Azure SQL (schemas:
Core,Memory,Ingestion,K12SAFETY,SDAC,TAP,REASONING,RECOVERED,HANDBOOK)- Stores: operational logs, workflow request/status records, agent prompt profiles, cost report data, and validation results.
- This is the system of record for "what happened" and for the Admin UI-managed agent prompts.
-
Mastra local store (LibSQL) in
.mastra/data/mastra.db- Stores: agent/workflow memory and Mastra runtime state.
- This is not the same database as Azure SQL; don't look here for
Core.AgentProfilesor K12 request logs.
When debugging workflows, tool execution, request timelines, or prompt overrides, consult Azure SQL as the operational source of truth.
Schemas at a glance
| Schema | Purpose | Tables | Views |
|---|---|---|---|
Core | Shared tables: agent profiles, LLM metadata, resource dimensions, feature flags, evaluations | 16 | 7 |
Memory | Mastra memory resources, threads, and messages | 4 | 0 |
Ingestion | File ingestion runs, batches, files, mappings, and config | 5 | 0 |
K12SAFETY | K12 workload: EOP request/status logging, QRG storage, scenario evaluation | 21 | 9 |
SDAC | SDAC workload: cost reports, personnel, validation, review workflows | 15 | 3 |
TAP | TAP workload: user context, tagging suggestions, legacy batch/content safety logs | 10 | 3 |
REASONING | Reasoning engine: payloads, step outputs, task logs | 5 | 0 |
RECOVERED | Archive: historical workflow outputs | 1 | 0 |
HANDBOOK | MSBA handbook RAG audit records for retrieval, generation, verification, warnings, and telemetry | 1 | 0 |
Total: 78 tables, 22 views, 5 stored procedures
Table naming conventions
| Prefix | Purpose | Example |
|---|---|---|
log_ | Execution logs, audit trails | log_EOP_TaskStatus, log_AIAgentCalls |
data_ | Data storage (facts, reports) | data_CostReports, data_PersonnelRecords |
fact_ | Fact tables (dimensional modeling) | fact_AIAnalysisResults |
dim_ | Dimension tables (reference data) | dim_Districts, dim_ValidationRules |
val_ | Validation results | val_ValidationResults |
wf_ | Workflow tables | wf_ReviewRequests, wf_ReviewDecisions |
qrg_ | Quick Reference Guide (K12) | qrg_generations, qrg_requests |
vw_ | Views | vw_AgentProfileComparison |
usp_ | Stored procedures | usp_PromoteAgentByEnvironment |
Source of truth for schema
The SSDT/DACPAC projects under db/sqlproj/ are the authoritative schema source:
db/sqlproj/
├── Mastra.Databases.sln # Solution file
├── Mastra.AllDb/ # Master project (aggregates all schemas)
├── Mastra.CoreDb/ # Core schema
├── Mastra.Platform.K12SafetyDb/ # K12SAFETY schema
├── Mastra.Platform.SDACDb/ # SDAC schema
├── Mastra.Platform.TapDb/ # TAP schema
├── Mastra.Platform.ReasoningEngineDb/ # REASONING schema
├── Mastra.Platform.IngestionDb/ # Ingestion schema
├── Mastra.Platform.RecoveredDb/ # RECOVERED schema
├── Mastra.Platform.HandbookDb/ # HANDBOOK schema
├── migrations/ # Migration scripts
└── seeds/ # Seed data scripts
Legacy folders db/core/ddl/* and db/platform/* have been deprecated. Use db/sqlproj/ for all schema definitions.
Database projects
| Project | Schema | Purpose |
|---|---|---|
| Mastra.AllDb | All | Master project that aggregates all schemas via MSBuild wildcards |
| Mastra.CoreDb | Core | Agent profiles, LLM configuration, resource dimensions |
| Mastra.Platform.K12SafetyDb | K12SAFETY | EOP logging, QRG generation and storage |
| Mastra.Platform.SDACDb | SDAC | Cost reports, personnel, validation, review workflows |
| Mastra.Platform.TapDb | TAP | User context, content tagging, safety findings |
| Mastra.Platform.ReasoningEngineDb | REASONING | Reasoning engine payloads and task logs |
| Mastra.Platform.IngestionDb | Ingestion | File ingestion runs, batches, mappings, and files |
| Mastra.Platform.RecoveredDb | RECOVERED | Archived workflow outputs |
Bootstrapping & verification
Build and deploy a DACPAC:
# Build all schemas
cd db/sqlproj
msbuild Mastra.AllDb/Mastra.AllDb.sqlproj /t:Build /p:Configuration=Release
# Publish to database
SqlPackage /Action:Publish \
/SourceFile:"./Mastra.AllDb/bin/Release/Mastra.AllDb.dacpac" \
/TargetConnectionString:"Server=tcp:<server>.database.windows.net,1433;..." \
/p:BlockOnPossibleDataLoss=True
Verify connectivity + table presence through the registered admin tool:
curl -X POST "$MASTRA_BASE_URL/admin/tools/db-health-check" \
-H "Content-Type: application/json" \
-d '{"data":{}}'
curl -X POST "$MASTRA_BASE_URL/admin/tools/db-health-check" \
-H "Content-Type: application/json" \
-d '{"data":{"schema":"K12SAFETY"}}'
Key identifiers (so joins make sense)
Not every workload uses the same ID types:
| Workload | ID Type | Example Column |
|---|---|---|
| EOP logging | NVARCHAR | RequestID, SessionID |
| QRG tables | UNIQUEIDENTIFIER | RequestId |
| SDAC cost reports | UNIQUEIDENTIFIER | ReportId |
| Agent profiles | BIGINT (surrogate) | AgentProfileSK |
When joining or filtering, use the exact table's type (string vs GUID vs BIGINT).
Seeding infrastructure
Database seeding is organized under db/sqlproj/seeds/:
seeds/
├── _run_all.sql # Master orchestrator
├── _run_by_env.sql # Environment-specific (tap_only, sdac_only, k12_sdac, all)
├── core/ # Core schema seeds
├── k12/ # K12 schema seeds
├── sdac/ # SDAC schema seeds
├── tap/ # TAP schema seeds
├── reasoning/ # Reasoning engine seeds
└── tools/ # Python utilities
├── copy_agents_between_dbs.py # Copy agents between databases
└── sync_agents_to_control.py # Sync to control table
Run seeds for specific environments:
# All schemas
sqlcmd -S <server> -d <database> -i "db\sqlproj\seeds\_run_all.sql"
# K12 + SDAC only
sqlcmd -S <server> -d <database> -i "db\sqlproj\seeds\_run_by_env.sql" -v ENV="k12_sdac"
Agent profile control (multi-environment)
The Core.AgentProfileControl table aggregates agent profiles from all environments for comparison and management:
-- Sync agents from prod to control table (Python)
python db/sqlproj/seeds/tools/sync_agents_to_control.py
-- Compare profiles across environments
SELECT * FROM Core.vw_AgentProfileComparison;
-- Promote agent from prod to local
EXEC Core.usp_PromoteAgentByEnvironment
@AgentId = 'sdac-cost-review-agent',
@SourceEnvironment = 'prod';
Next pages
- Core schema overview - Agent profiles, LLM configuration
- K12 EOP logging tables - EOP request/status logging
- K12 QRG tables - QRG generation and storage
- Database seeding guide - Seed scripts and environment configuration
- DACPAC deployment guide - Building and publishing DACPACs