Overview
The SQL schema connector (sql-schema-connector) provides a unified implementation for all SQL databases with database-specific “flavours” for PostgreSQL, MySQL, SQLite, SQL Server (MSSQL), and CockroachDB.
Location: schema-engine/connectors/sql-schema-connector/
Architecture
SqlSchemaConnector
The top-level SQL connector wraps database-specific flavour implementations:SQL Dialect
Each database has a dialect implementing database-specific behavior:SQL Connector Trait
Internal trait for SQL-specific operations:Supported Databases
- PostgreSQL
- MySQL
- SQLite
- SQL Server
- CockroachDB
PostgreSQL Connector
Provider:postgresqlSupported Versions: 9.6+, recommended 12+Features:- Full ACID transactions with all isolation levels
- Native enums
- JSON/JSONB with filtering support
- Array types (scalar lists)
- Full-text search
- Multi-schema support
- Extensions (PostGIS, pg_trgm, etc.)
- Lateral joins
- Partial indexes
- Row-level security (RLS) aware
schema: Default schema (default:public)connection_limit: Max connections (default: unlimited)connect_timeout: Connection timeout in secondspool_timeout: Pool acquisition timeoutsslmode: SSL mode (prefer,disable,require)sslcert,sslidentity,sslpassword: SSL certificate optionshost: Unix socket path (alternative to hostname)
SmallInt,Integer,BigIntDecimal(p,s),MoneyReal,DoublePrecisionVarChar(n),Char(n),TextByteATimestamp(p),Timestamptz(p),Date,Time(p),Timetz(p)BooleanUuidJson,JsonBXml,Inet,Citext,Bit(n),VarBit(n)
Migration Workflow
1. Schema Diffing
CreateTable,DropTable,AlterTableCreateIndex,DropIndexCreateEnum,DropEnum,AlterEnumAddColumn,DropColumn,AlterColumnAddForeignKey,DropForeignKey- Database-specific steps (e.g.,
CreateExtensionfor PostgreSQL)
2. Script Rendering
- PostgreSQL: DDL with
IF EXISTS,CASCADEsupport - MySQL: DDL with backtick identifiers
- SQLite: DDL with table recreations (no
ALTER COLUMN) - SQL Server: DDL with
GObatch separators
3. Destructive Change Checking
- Data loss (dropping tables, columns)
- Type changes that may truncate data
- Constraint changes that may fail on existing data
4. Migration Application
- Acquire advisory lock (if supported)
- Start transaction (if supported for DDL)
- Execute migration steps
- Record in
_prisma_migrationstable - Commit and release lock
Introspection
Introspection scans the database and generates a Prisma schema:- Describe schema: Query system tables/catalogs
- Build internal schema: Create
SqlSchemarepresentation - Calculate datamodel: Generate PSL from schema
- Add warnings: Flag unsupported features
- Models from tables
- Fields from columns with native types
- Relations from foreign keys
- Enums from database enums
- Indexes and unique constraints
- Default values
Shadow Database
For migration validation, SQL connectors can use a shadow database:- Is a temporary database for applying migrations
- Validates migration scripts work correctly
- Is automatically created and dropped
- Can be user-provided or auto-generated
Multi-Schema Support
PostgreSQL and SQL Server support multiple schemas/namespaces:- Schema-qualified table names
- Cross-schema relations
- Search path configuration
- Migration targeting specific schemas
Advisory Locks
PostgreSQL and MySQL use advisory locks to prevent concurrent migrations:- PostgreSQL:
pg_advisory_lock() - MySQL:
GET_LOCK() - Other databases: No-op or file-based locking
Performance Considerations
- Connection pooling: Configure
connection_limitappropriately - Batch operations: Use
create_recordsinstead of multiplecreate_record - Relation loading:
JoinStrategyfor N+1 query prevention (database version dependent) - Indexes: Introspection captures all indexes for query optimization
Next Steps
MongoDB Connector
Learn about the MongoDB connector implementation