schema-commands crate (also known as schema-core) provides the core functionality for the Prisma schema engine, including migrations, introspection, and schema operations.
Overview
This library implements all schema engine commands that are exposed through the JSON-RPC API. It handles database migrations, schema introspection, schema diffing, and validation.Installation
Core Modules
Commands
Thecommands module contains implementations for all schema engine operations:
apply_migrations- Apply pending migrations to the databasecreate_migration- Create a new migration from schema changesdev_diagnostic- Run development-time diagnosticsdiagnose_migration_history- Analyze migration history for issuesdiff- Compare two schemas and show differencesevaluate_data_loss- Analyze potential data loss in migrationsintrospect_sql- Introspect a database and generate a schemamark_migration_applied- Mark a migration as applied without running itmark_migration_rolled_back- Mark a migration as rolled backschema_push- Push schema changes directly to the database
Key Types
GenericApi
The main API trait for schema engine operations.CoreError
Error type for schema engine operations.Diagnostics
Schema parsing and validation errors
ConnectorError
Database connector errors
String
Generic error with message
Schema Operations
Introspection
Introspect a database and generate a Prisma schema.SchemaContainer
Base schema with datasource configuration
String
Database connection string to introspect
object
Contains the generated schema and warnings
schema- Generated Prisma schemawarnings- Introspection warningsviews- Database views discovered
Create Migration
Create a new migration from schema changes.String
Name for the new migration
SchemaContainer
The target schema to migrate to
bool
Whether to create a draft migration
object
migration_id- ID of the created migrationmigration_script- SQL script for the migrationwarnings- Warnings about the migration
Apply Migrations
Apply pending migrations to the database.String
Path to the migrations directory
SchemaContainer
The Prisma schema
object
applied_migration_names- List of applied migration nameswarnings- Warnings from applying migrations
Schema Push
Push schema changes directly to the database without migrations.SchemaContainer
The schema to push
bool
Whether to force push even with data loss
object
executed_steps- Number of steps executedwarnings- Push warningsunexecutable- Reasons why push failed (if applicable)
Diff
Compare two schemas or a schema and database.DiffTarget
Source schema or database
DiffTarget
Target schema or database
bool
Whether to return SQL script or human-readable diff
object
diff- The diff as SQL script or human-readable formatexit_code- Exit code (0 if no changes, 2 if changes)
Helper Functions
dialect_for_provider
Create a schema dialect for a given provider.&str
required
Database provider name (postgresql, mysql, sqlite, mssql, mongodb)
Box<dyn SchemaDialect>
The schema dialect implementation for the provider
extract_namespaces
Extract database namespaces from schema files.Feature Flags
Database provider support is controlled by feature flags:Dependencies
Key dependencies:psl- Prisma Schema Language parserschema-connector- Schema connector traitsql-schema-connector- SQL database schema operationsmongodb-schema-connector- MongoDB schema operations (optional)quaint- Database abstraction layeruser-facing-errors- User-friendly error messagesjsonrpc-core- JSON-RPC protocol support
Examples
Introspecting a Database
Creating a Migration
Pushing Schema Changes
Related
- psl-core - Schema parsing and validation
- Schema Engine Overview - Architecture guide
- Migrations Guide - Migration workflows