kimberlite-migration
SQL migration system for Kimberlite database with file-based tracking, checksums, and tamper detection.
Features
- SQL-based migrations - Write migrations in standard SQL
- Auto-numbering - Sequential migration IDs (0001, 0002, etc.)
- Checksum validation - SHA-256 checksums prevent tampering
- Lock file - Detects modifications to applied migrations
- File-based tracking - Simple TOML files for migration state
- No custom DSL - Just SQL, no new language to learn
Quick Start
# Create a new migration
# Check migration status
# Validate migrations (checksums, sequence)
# Apply pending migrations
Migration File Format
Migration files use a simple comment-based metadata format:
-- Migration: Add users table
-- Created: 2026-02-01T10:00:00Z
-- Author: alice@example.com
-- Up Migration
(
id BIGINT NOT NULL,
name TEXT NOT NULL,
email TEXT,
PRIMARY KEY (id)
);
(email);
-- Down Migration (optional, for rollback)
-- DROP INDEX users_email;
-- DROP TABLE users;
Filename Convention
Migrations use zero-padded sequential IDs:
0001_add_users.sql0002_add_posts.sql0003_add_comments.sql
Directory Structure
project/
├── migrations/ # SQL migration files
│ ├── 0001_initial.sql
│ ├── 0002_add_users.sql
│ └── 0003_add_posts.sql
└── .kimberlite/
└── migrations/
├── .lock # Lock file with checksums
└── applied.toml # Tracker state
API Usage
use ;
use PathBuf;
// Initialize manager
let config = MigrationConfig ;
let manager = new?;
// Create a migration
let file = manager.create?;
println!;
// List pending migrations
let pending = manager.list_pending?;
for migration in pending
// Validate checksums and sequence
manager.validate?;
Migration Workflow
1. Create Migration
2. Edit Migration File
Edit migrations/0001_add_patients_table.sql:
-- Migration: Add patients table
-- Created: 2026-02-01T10:00:00Z
(
id BIGINT NOT NULL,
name TEXT NOT NULL,
dob DATE,
PRIMARY KEY (id)
);
3. Check Status
|
4. Apply Migrations
)
5. Validate Integrity
Configuration
Project Config (kimberlite.toml)
[]
= "migrations"
= true
Migration Manager Config
use MigrationConfig;
let config = MigrationConfig ;
Lock File
The lock file (.kimberlite/migrations/.lock) stores SHA-256 checksums to detect tampering:
= 1
[[]]
= 1
= "add_users_table"
= "a1b2c3d4e5f6..."
[[]]
= 2
= "add_posts_table"
= "b2c3d4e5f6a1..."
Checksum Validation
If a migration file is modified after being applied, validation will fail:
Tracker State
Applied migrations are tracked in applied.toml:
[[]]
= 1
= "add_users_table"
= "a1b2c3d4e5f6..."
= "2026-02-01T10:30:00Z"
= "alice"
Error Handling
Invalid Migration Name
let result = manager.create;
// Error: Invalid migration name: invalid/name
Sequence Gap
// Missing 0002_*.sql
manager.validate?;
// Error: Invalid migration sequence: expected 2, found 3
Tampered Migration
// File modified after application
manager.validate?;
// Error: Checksum mismatch for migration 1
Testing
# Run migration system tests
# Run specific test
Future Enhancements
- SQL execution integration with kimberlite_client
- Rollback support (DOWN migrations)
- Migration templates (CRUD, indexes, etc.)
- Dry-run mode
- Migration dependencies
- Data migrations (not just schema)
- Migration squashing
- Cloud sync for migrations
Contributing
Follow the Kimberlite contribution guidelines in CLAUDE.md.
License
Apache-2.0