Migrations
Migration system based on Drizzle Kit, integrated with @iskra-bun/db-kit.
Configuration
Section titled “Configuration”1. Create the schema
Section titled “1. Create the schema”import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
export const users = sqliteTable('users', { id: integer('id').primaryKey({ autoIncrement: true }), name: text('name').notNull(), email: text('email').notNull().unique(), createdAt: text('created_at').default('CURRENT_TIMESTAMP'),});
export const posts = sqliteTable('posts', { id: integer('id').primaryKey({ autoIncrement: true }), title: text('title').notNull(), content: text('content'), authorId: integer('author_id').references(() => users.id),});2. Create drizzle.config.ts
Section titled “2. Create drizzle.config.ts”Use the helper from @iskra-bun/db-kit:
import { createDrizzleConfig } from '@iskra-bun/db-kit';
export default createDrizzleConfig({ dialect: 'sqlite', dbUrl: process.env.DATABASE_URL || 'app.db', schemaPath: './src/db/schema.ts', migrationsDir: './drizzle',});Commands
Section titled “Commands”Via CLI
Section titled “Via CLI”# Generar migraciones a partir de cambios en el schemabun run packages/db-kit/src/cli.ts generate mi-migracion
# Aplicar migraciones pendientesbun run packages/db-kit/src/cli.ts migrate
# Empujar schema directo a la DB (sin archivos de migracion, ideal para dev)bun run packages/db-kit/src/cli.ts push
# Delete a generated migration file (does not touch the database)bun run packages/db-kit/src/cli.ts dropVia bunx (directly with drizzle-kit)
Section titled “Via bunx (directly with drizzle-kit)”bunx drizzle-kit generate --name mi-migracionbunx drizzle-kit migratebunx drizzle-kit pushbunx drizzle-kit studio # UI web para explorar la DBProgrammatic
Section titled “Programmatic”From your application:
import { DbDriver } from '@iskra-bun/db-kit';
const db = new DbDriver();app.register(db);await app.start();
// Apply the pending migrations in ./drizzle (created with `generate`)// over the live connection. The first argument (schema) is not needed to apply.await db.runMigrations(undefined, './drizzle');Or using MigrationHelper directly:
import { MigrationHelper } from '@iskra-bun/db-kit';
const helper = new MigrationHelper({ dialect: 'sqlite', dbUrl: 'app.db', schemaPath: './src/db/schema.ts', migrationsDir: './drizzle',});
await helper.generate('agregar-tabla-posts');await helper.migrate();Recommended Workflow
Section titled “Recommended Workflow”- Development: Use
pushto iterate quickly without generating files - Pre-deploy: Generate migrations with
generateand commit the files - Deploy: Run
migrateat app startup or in the CI/CD
Modificar schema.ts → generate → commit → deploy → migrateFile Structure
Section titled “File Structure”mi-proyecto/├── src/│ └── db/│ └── schema.ts # Definicion de tablas├── drizzle/│ ├── 0000_initial.sql # Migracion generada│ ├── 0001_add-posts.sql # Otra migracion│ └── meta/ # Metadata de drizzle-kit├── drizzle.config.ts # Config de drizzle-kit└── app.config.ts # Config de la appErrors
Section titled “Errors”If a migration fails, a MigrationError is thrown with the error detail:
import { MigrationError } from '@iskra-bun/db-kit';
try { await helper.migrate();} catch (err) { if (err instanceof MigrationError) { console.error(err.code); // 'MIGRATION_ERROR' console.error(err.context); // { operation: 'migrate', exitCode: 1, stderr: '...' } }}