Skip to content

Add SQL Server Support & Drizzle ORM 1.0 Beta Compatibility#24

Open
ianfrye0818 wants to merge 6 commits intodrizzle-team:mainfrom
ianfrye0818:main
Open

Add SQL Server Support & Drizzle ORM 1.0 Beta Compatibility#24
ianfrye0818 wants to merge 6 commits intodrizzle-team:mainfrom
ianfrye0818:main

Conversation

@ianfrye0818
Copy link

⚠️ Breaking Change - Drizzle ORM 1.0.0-beta.1

This PR introduces breaking changes to support Drizzle ORM 1.0.0-beta.1's new defineRelations API.

Summary

This PR adds SQL Server (MSSQL) support to the generator and updates all database generators (PostgreSQL, MySQL, SQLite, MSSQL) to be compatible with Drizzle ORM's 1.0 beta release, which introduces the new defineRelations API. Additionally, it includes a comprehensive test suite and critical bug fixes.

Key Changes

New Features

  • SQL Server (MSSQL) Support (b4dea8f)
    • Added complete MSSQL schema generator (src/util/generators/mssql.ts)
    • Support for MSSQL-specific types: varchar, nvarchar, int, bigint, datetime, uniqueidentifier, etc.
    • Proper handling of MSSQL constraints and defaults
    • Updated provider detection to include sqlserver

Breaking Changes

  • Drizzle ORM 1.0 Beta Compatibility (cb2b0ba)
    • Migrated all generators from individual relations exports to single defineRelations export
    • Updated relation syntax from one(Model, {...}) to r.one.Model({...})
    • Changed internal data structure from rqb: string[] to rqb: Record<string, string[]>
    • Added tablesWithRelations Set to track which tables have relations
    • All generators now generate: export const relations = defineRelations({ ...tables }, (r) => ({...}))

Bug Fixes

  • Fixed Falsy Default Values (ab32f63)
    • Critical fix: Changed if (field.default) to if (field.hasDefaultValue && field.default !== undefined)
    • Now correctly handles falsy default values like 0, false, and ""
    • Affected all generators: PostgreSQL, MySQL, SQLite, MSSQL
    • Added null checks to prevent undefined value errors in MSSQL generator

Testing

  • Comprehensive Test Suite (e7b04f2)
    • Added 118 tests across all 4 database generators (100% passing)
    • Test coverage includes:
      • Basic table generation
      • All Prisma → Drizzle type mappings
      • Default values (including edge cases)
      • Relations and foreign keys
      • Delete actions (Cascade, SetNull, SetDefault, Restrict, NoAction)
      • Enums (MySQL and PostgreSQL)
      • Unique constraints and indexes
      • Edge cases (empty models, models without relations)
    • Set up Vitest with @vitest/ui for interactive testing
    • Added test scripts: pnpm test, pnpm test:watch, pnpm test:ui

Documentation

  • Updated README.md to document SQL Server support (f3a65d7)
  • Added comprehensive TEST_SUMMARY.md documenting test setup and results
  • Fixed documentation typos (1de4b20)

Files Changed

  • New Files:

    • src/util/generators/mssql.ts - SQL Server generator
    • src/util/generators/__tests__/*.test.ts - Test suites for all 4 generators
    • src/util/generators/__tests__/test-fixtures.ts - Shared test fixtures
    • vitest.config.ts - Vitest configuration
    • TEST_SUMMARY.md - Test documentation
  • Modified Files:

    • src/util/generators/pg.ts - Updated for defineRelations + bug fixes
    • src/util/generators/mysql.ts - Updated for defineRelations + bug fixes
    • src/util/generators/sqlite.ts - Updated for defineRelations + bug fixes
    • src/index.ts - Added MSSQL provider support
    • src/util/extract-many-to-many-models/index.ts - Code improvements
    • README.md - Updated documentation

Test Results

✓ src/util/generators/tests/mssql.test.ts (27 tests)
✓ src/util/generators/tests/mysql.test.ts (30 tests)
✓ src/util/generators/tests/pg.test.ts (33 tests)
✓ src/util/generators/tests/sqlite.test.ts (28 tests)

Test Files 4 passed (4)
Tests 118 passed (118)

Migration Guide

For users upgrading to this version:

  1. Drizzle ORM Update Required: Ensure you're using Drizzle ORM 1.0 beta or later
  2. Generated Code Changes: Your generated schema will change to use defineRelations instead of individual relation exports
  3. Import Changes: Update your imports from individual relation exports to the single relations export

Before:

import { User, userRelations, Post, postRelations } from './schema'

After:

import { User, Post, relations } from './schema'

Breaking Changes

  • Generated relation code structure has changed due to Drizzle ORM 1.0 beta
  • If you have code that imports individual relation exports, you'll need to update to import from the single relations export

Supported Databases

  • PostgreSQL
  • MySQL
  • SQLite
  • SQL Server (MSSQL) - NEW

Stats: 18 files changed, 7302 insertions(+), 952 deletions(-)

  - Add complete MSSQL generator with type mappings
  - Update provider detection to support sqlserver
  - Refactor all generators for consistency
  - Change condition from `if (field.default)` to `if (field.hasDefaultValue)`
  - Fixes bug where default values of 0, false, or "" were ignored
  - Applies to all generators: pg, mysql, sqlite, mssql
  - Migrate from individual relation exports to single defineRelations
  - Update relation syntax to r.one.Model() and r.many.Model()
  - Change rqb data structure to Record<string, string[]>
  - Add tablesWithRelations Set for tracking
  - Add test suites for all 4 database generators
  - Set up Vitest with @vitest/ui
  - 100% test pass rate across pg, mysql, sqlite, mssql
  - Add test fixtures and helper functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant