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.Schema parsing and validation errors
Database connector errors
Generic error with message
Schema Operations
Introspection
Introspect a database and generate a Prisma schema.Base schema with datasource configuration
Database connection string to introspect
Contains the generated schema and warnings
schema- Generated Prisma schemawarnings- Introspection warningsviews- Database views discovered
Create Migration
Create a new migration from schema changes.Name for the new migration
The target schema to migrate to
Whether to create a draft migration
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.Path to the migrations directory
The Prisma schema
applied_migration_names- List of applied migration nameswarnings- Warnings from applying migrations
Schema Push
Push schema changes directly to the database without migrations.The schema to push
Whether to force push even with data loss
executed_steps- Number of steps executedwarnings- Push warningsunexecutable- Reasons why push failed (if applicable)
Diff
Compare two schemas or a schema and database.Source schema or database
Target schema or database
Whether to return SQL script or human-readable diff
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.Database provider name (postgresql, mysql, sqlite, mssql, mongodb)
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