Skip to main content
The 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

The commands module contains implementations for all schema engine operations:
  • apply_migrations - Apply pending migrations to the database
  • create_migration - Create a new migration from schema changes
  • dev_diagnostic - Run development-time diagnostics
  • diagnose_migration_history - Analyze migration history for issues
  • diff - Compare two schemas and show differences
  • evaluate_data_loss - Analyze potential data loss in migrations
  • introspect_sql - Introspect a database and generate a schema
  • mark_migration_applied - Mark a migration as applied without running it
  • mark_migration_rolled_back - Mark a migration as rolled back
  • schema_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.
SchemaParserError
Diagnostics
Schema parsing and validation errors
ConnectorError
ConnectorError
Database connector errors
Generic
String
Generic error with message

Schema Operations

Introspection

Introspect a database and generate a Prisma schema.
schema
SchemaContainer
Base schema with datasource configuration
connection_string
String
Database connection string to introspect
IntrospectSqlOutput
object
Contains the generated schema and warnings
  • schema - Generated Prisma schema
  • warnings - Introspection warnings
  • views - Database views discovered

Create Migration

Create a new migration from schema changes.
migration_name
String
Name for the new migration
schema
SchemaContainer
The target schema to migrate to
draft
bool
Whether to create a draft migration
CreateMigrationOutput
object
  • migration_id - ID of the created migration
  • migration_script - SQL script for the migration
  • warnings - Warnings about the migration

Apply Migrations

Apply pending migrations to the database.
migrations_directory
String
Path to the migrations directory
schema
SchemaContainer
The Prisma schema
ApplyMigrationsOutput
object
  • applied_migration_names - List of applied migration names
  • warnings - Warnings from applying migrations

Schema Push

Push schema changes directly to the database without migrations.
schema
SchemaContainer
The schema to push
force
bool
Whether to force push even with data loss
SchemaPushOutput
object
  • executed_steps - Number of steps executed
  • warnings - Push warnings
  • unexecutable - Reasons why push failed (if applicable)

Diff

Compare two schemas or a schema and database.
from
DiffTarget
Source schema or database
to
DiffTarget
Target schema or database
script
bool
Whether to return SQL script or human-readable diff
DiffOutput
object
  • diff - The diff as SQL script or human-readable format
  • exit_code - Exit code (0 if no changes, 2 if changes)

Helper Functions

dialect_for_provider

Create a schema dialect for a given provider.
provider
&str
required
Database provider name (postgresql, mysql, sqlite, mssql, mongodb)
SchemaDialect
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 parser
  • schema-connector - Schema connector trait
  • sql-schema-connector - SQL database schema operations
  • mongodb-schema-connector - MongoDB schema operations (optional)
  • quaint - Database abstraction layer
  • user-facing-errors - User-friendly error messages
  • jsonrpc-core - JSON-RPC protocol support

Examples

Introspecting a Database

Creating a Migration

Pushing Schema Changes