Architecture
The Schema Engine follows a Core/Connector architecture that enables consistent behavior across different database systems.Core Layer
Theschema-core crate (located in schema-engine/core) provides the high-level API exposed to all consumers. It’s a thin orchestration layer that:
- Defines the unified API surface
- Routes operations to appropriate connectors
- Handles cross-connector concerns
- Manages the JSON-RPC interface
Connector Layer
Each supported database has a dedicated connector implementing theSchemaConnector trait defined in the schema-connector crate. Most of the actual migration and introspection logic lives in these connectors:
- SQL Schema Connector - PostgreSQL, MySQL, SQL Server, SQLite, CockroachDB
- MongoDB Schema Connector - MongoDB (limited support in Prisma 7)
schema-engine/connectors/.
All connectors implement the same API, ensuring consistent behavior regardless of the underlying database system.
Communication Interfaces
The Schema Engine can be invoked in two ways:1. JSON-RPC API (Primary)
The default mode when running theschema-engine binary. The TypeScript CLI communicates with the engine over stdio using JSON-RPC, allowing:
- Multiple commands on the same connection
- Bidirectional communication
- State management across operations
- Real-time result streaming
2. CLI Commands (Legacy)
Direct CLI commands for operations that don’t require a database connection:Core Functionality
The Schema Engine provides two main blocks of functionality:1. Traditional Migrations System
Like ActiveRecord migrations or Flyway, it manages migration files and tracks application state:- Creates and applies migration files (plain SQL on SQL databases)
- Tracks migration history in the
_prisma_migrationstable - Detects failed migrations and drift
- Supports migration resolution with
migrate resolve
2. Schema Diffing & Generation
Like tools such as skeema, it understands database schemas and can generate migrations:- Compares Prisma schema (state A) with database schema (state B)
- Generates migration to transform B → A
- Powers
migrate devworkflow - Enables schema introspection from existing databases
How diffing works
How diffing works
Diffing is the process of generating a migration between two schemas. The Schema Engine:
- Loads both the “from” schema and “to” schema
- Uses connector-specific logic to compare structures
- Generates DDL statements to transform one into the other
- Detects potentially destructive changes (data loss warnings)
- Renders the migration as SQL or a summary
Logging and Diagnostics
The Schema Engine outputs structured JSON logs to stderr. Each line contains:Exit Codes
| Code | Meaning |
|---|---|
0 | Normal exit |
1 | Abnormal (error) exit |
101 | Panic (unrecoverable error) |
All non-zero exit codes are accompanied by an ERROR-level log message on stderr.
Data Sources and Connection
In Prisma 7, datasource URLs are no longer embedded in the Prisma schema. Instead:- The
--datasourceCLI flag accepts a JSON payload with connection strings - The Prisma Config file (
prisma.config.ts) supplies URLs at runtime - Schema Engine receives URLs as overrides, bypassing PSL parsing
Directory Structure
Related Documentation
Migration System
Deep dive into the _prisma_migrations table and migration tracking
Introspection
Learn how database introspection works
Schema Diffing
Understand migration generation and schema comparison
CLI Reference
Complete CLI and JSON-RPC API reference