Migration Guide

Step-by-step instructions for migrating from other ORMs to CQL


📋 Overview

This guide helps you migrate existing applications from popular ORMs to CQL. We'll cover:

  • ActiveRecord (Ruby) → CQL

  • Eloquent (PHP) → CQL

  • Sequelize (JavaScript) → CQL

  • TypeORM (TypeScript) → CQL


🎯 Migration Strategy

1. Assessment Phase

  • Analyze Current Schema: Document existing tables, relationships, and constraints

  • Identify Dependencies: Find external integrations and database-specific features

  • Plan Migration: Choose incremental vs. full migration approach

2. Preparation Phase

  • Set up Crystal Environment: Install Crystal and CQL

  • Create New Project: Initialize CQL project structure

  • Database Setup: Configure database connections

3. Migration Phase

  • Schema Migration: Recreate tables and relationships

  • Data Migration: Transfer existing data

  • Business Logic: Port model validations and methods

  • Testing: Verify functionality and performance


🔴 From ActiveRecord (Ruby)

Setting Up the Environment

Model Migration

Before (ActiveRecord)

After (CQL)

Schema Migration

Before (ActiveRecord Migration)

After (CQL Schema)

Query Migration

Before (ActiveRecord)

After (CQL)


🟡 From Eloquent (PHP)

Model Migration

Before (Eloquent)

After (CQL)

Query Migration

Before (Eloquent)

After (CQL)


🔵 From Sequelize (JavaScript)

Model Migration

Before (Sequelize)

After (CQL)


🟢 From TypeORM (TypeScript)

Model Migration

Before (TypeORM)

After (CQL)


📊 Data Migration

Export Data from Source

MySQL/PostgreSQL

SQLite

Import Data to CQL


🧪 Testing Migration

Create Test Suite

Performance Testing


🎯 Best Practices

1. Incremental Migration

  • Start Small: Migrate one model at a time

  • Dual Write: Write to both old and new systems during transition

  • Gradual Cutover: Switch reads gradually after verifying data integrity

2. Type Safety

  • Use Proper Types: Leverage Crystal's type system

  • Handle Nils: Use union types (String | Nil) for optional fields

  • Validate Conversions: Ensure data type compatibility

3. Performance Optimization

  • Index Migration: Recreate important indexes first

  • Batch Operations: Use transactions for large data sets

  • Monitor Memory: Crystal uses less memory, but monitor during migration

4. Error Handling


🔧 Common Issues and Solutions

Issue 1: Crystal Type System

Problem: Dynamic types from source ORM don't match Crystal's static types

Solution:

Issue 2: Missing Callbacks

Problem: Some ORM-specific callbacks don't have direct equivalents

Solution:

Issue 3: Query Syntax Differences

Problem: Complex query syntax differs between ORMs

Solution:


✅ Migration Checklist

Pre-Migration

Schema Migration

Model Migration

Data Migration

Testing

Deployment


🎓 Next Steps

After successful migration:

  1. Optimize Performance: Use CQL's performance guide

  2. Add Advanced Features: Explore advanced querying

  3. Set Up Monitoring: Implement logging and metrics

  4. Train Team: Share Crystal and CQL knowledge with your team


Need Help?

Last updated

Was this helpful?