Manage database operations for the ExFabrica Agentic Factory project, including migrations, seeding, backup, restore, and schema management using Drizzle ORM and PostgreSQL.
Manage database operations including migrations, seeding, backup, and restore using Drizzle ORM and PostgreSQL for the ExFabrica Agentic Factory project.
/plugin marketplace add hubexab/EAF-PluginClaude/plugin install exfabrica-af-plugin@exfabrica-af-marketplaceManage database operations for the ExFabrica Agentic Factory project, including migrations, seeding, backup, restore, and schema management using Drizzle ORM and PostgreSQL.
/db-operations <operation> [--env=<environment>] [--force]
migrate - Run pending database migrationsmigrate:create - Create a new migration filemigrate:rollback - Rollback the last migrationseed - Seed database with test/demo datareset - Drop all tables and recreate schemabackup - Create database backuprestore - Restore from backupstatus - Show migration and database statusvalidate - Validate schema against models/db-operations migrate
Applies all pending migrations to the database in order.
Process:
Output:
šļø Running Database Migrations
================================
Database: exfabrica_af_dev
Environment: development
Pending migrations:
ā 001_create_users_table.sql
ā 002_create_projects_table.sql
ā 003_add_user_roles.sql
All migrations completed successfully! (3.2s)
/db-operations migrate:create add_organizations_table
Creates a new migration file with timestamp.
Generated File: apps/backend/drizzle/migrations/20251029123456_add_organizations_table.sql
/db-operations migrate:rollback
Rolls back the most recently applied migration.
ā ļø WARNING: Use with extreme caution in production!
/db-operations seed
Populates the database with test/demo data for development.
Seed Data Includes:
Process:
Output:
š± Seeding Database
===================
Environment: development
Seeding data:
ā Users: 10 records created
ā Organizations: 3 records created
ā Projects: 15 records created
ā Workflows: 23 records created
ā API Keys: 5 records created
Database seeded successfully! (2.7s)
Test Accounts:
- admin@exfabrica.com / Admin123!
- user@exfabrica.com / User123!
/db-operations reset
ā ļø DESTRUCTIVE OPERATION: Drops all tables and recreates the schema.
Confirmation Required (unless --force is used):
ā ļø WARNING: This will delete ALL data in the database!
Database: exfabrica_af_dev
Environment: development
Are you sure you want to continue? (yes/no):
Process:
Use Cases:
/db-operations backup
Creates a full backup of the current database.
Backup Location: backups/db/exfabrica_af_dev_20251029_123456.sql
Backup Includes:
Process:
pg_dump to create SQL backupOutput:
š¾ Creating Database Backup
===========================
Database: exfabrica_af_dev
Backup file: backups/db/exfabrica_af_dev_20251029_123456.sql.gz
ā Backup created successfully
ā Size: 12.4 MB (compressed)
ā Records: ~45,000
Backup location:
c:\Users\nicol\Source\ExFabrica\ExFabrica\EAF\backups\db\exfabrica_af_dev_20251029_123456.sql.gz
/db-operations restore backups/db/exfabrica_af_dev_20251029_123456.sql.gz
Restores database from a backup file.
ā ļø WARNING: This will replace all current data!
Process:
/db-operations status
Shows comprehensive database and migration status.
Output:
š Database Status
==================
Connection: ā Connected
Database: exfabrica_af_dev
Host: localhost:5432
Version: PostgreSQL 15.3
Migrations:
Applied: 23
Pending: 0
Last migration: 20251028_add_workflow_permissions.sql
Schema:
Tables: 12
Indexes: 34
Constraints: 28
Data Summary:
Users: 145 records
Projects: 67 records
Workflows: 234 records
Organizations: 8 records
Size:
Database: 124 MB
Largest table: workflows (45 MB)
Health: ā All checks passed
/db-operations validate
Validates that the database schema matches Drizzle ORM model definitions.
Checks:
Output:
ā All models validated successfully
ā Warning: Index 'idx_users_email' missing on users table
ā Warning: Column 'updated_at' type mismatch (expected: timestamptz, found: timestamp)
# .env.development
DATABASE_URL=postgresql://dev:devpass@localhost:5432/exfabrica_af_dev
# .env.test
DATABASE_URL=postgresql://test:testpass@localhost:5442/exfabrica_af_test
# .env.production
DATABASE_URL=postgresql://prod:prodpass@db.example.com:5432/exfabrica_af_prod
# Start fresh with clean database
/db-operations reset --force
/db-operations seed
# Make schema changes in code
# Generate migration
/db-operations migrate:create add_new_feature
# Apply migration
/db-operations migrate
# Verify
/db-operations status
# Create backup before migration
/db-operations backup --env=production
# Apply migrations
/db-operations migrate --env=production
# Verify
/db-operations status --env=production
# Reset test database
/db-operations reset --env=test --force
# Seed with test data
/db-operations seed --env=test
# Run tests
/test-all backend
Use descriptive names
/db-operations migrate:create add_workflow_approvals
Make migrations reversible
Include both up and down migrations:
-- Up
CREATE TABLE approvals (...);
-- Down
DROP TABLE approvals;
Test migrations locally first
/db-operations migrate --env=dev
/db-operations migrate:rollback --env=dev
Keep migrations small and focused
Drizzle ORM ensures migrations run in order:
001_create_users_table.sql
002_create_organizations_table.sql
003_add_user_organization_relation.sql (depends on 001 and 002)
Error: Connection refused at localhost:5432
Solution: Ensure PostgreSQL is running
docker compose up -d postgres
Error: Migration 003_add_column.sql failed
Solution: Check migration SQL syntax and rollback
/db-operations migrate:rollback
# Fix migration file
/db-operations migrate
Error: Table 'users' does not match model definition
Solution: Run validation and create corrective migration
/db-operations validate
/db-operations migrate:create fix_users_schema
Error: Duplicate key violation on users.email
Solution: Clear existing seed data first
/db-operations reset --force
/db-operations seed
Error: Invalid backup file format
Solution: Use a different backup or restore from automated backups
ls backups/db/automated/
/db-operations restore backups/db/automated/latest.sql.gz
- task: Script@1
displayName: 'Database Migrations'
inputs:
script: |
/db-operations backup --env=$(ENVIRONMENT)
/db-operations migrate --env=$(ENVIRONMENT)
/db-operations validate --env=$(ENVIRONMENT)
ā Backup created ā Migrations tested in staging ā Schema validated ā Rollback plan prepared
// apps/backend/src/database/schema/users.schema.ts
import { pgTable, serial, varchar, timestamp } from 'drizzle-orm/pg-core';
export const users = pgTable('users', {
id: serial('id').primaryKey(),
email: varchar('email', { length: 255 }).notNull().unique(),
password: varchar('password', { length: 255 }).notNull(),
firstName: varchar('first_name', { length: 100 }),
lastName: varchar('last_name', { length: 100 }),
createdAt: timestamp('created_at').defaultNow(),
updatedAt: timestamp('updated_at').defaultNow(),
});
/test-all backend - Test database operations/deploy - Deploy with database migrations/analyze-code - Check migration file qualityCreate custom seed files in apps/backend/drizzle/seeds/:
// custom-seed.ts
export async function seed(db: Database) {
await db.insert(users).values([
{ email: 'custom@example.com', password: 'hashed' }
]);
}
Run specific migration range:
/db-operations migrate --from=001 --to=005
Operate on multiple databases:
/db-operations migrate --all-environments
Note: Always test database operations in development before applying to staging or production. Keep backups and maintain a rollback plan for production changes.