AgentSkillsCN

ayunis-core-migrations

在ayunis-core中进行数据库迁移开发。当需要对数据库结构进行变更时(如新增列、新表、新索引、新约束),可使用此技能。

SKILL.md
--- frontmatter
name: ayunis-core-migrations
description: Creating database migrations in ayunis-core. Use when schema changes are needed (new columns, tables, indexes, constraints).

Database Migrations — ayunis-core-backend

Golden Rule

Never write migration files by hand. Always auto-generate them from TypeORM entity changes.

Working Directory

bash
cd ayunis-core-backend

Workflow

1. Modify the TypeORM Entity (Record)

Change the schema definition in the relevant infrastructure/persistence/postgres/schema/ file.

All records must extend BaseRecord (src/common/db/base-record.ts), which provides id, createdAt, and updatedAt columns automatically:

typescript
// e.g. src/domain/agents/infrastructure/persistence/postgres/schema/agent.record.ts
import { BaseRecord } from 'src/common/db/base-record';

@Entity('agents')
export class AgentRecord extends BaseRecord {
  @Column({ type: 'varchar', length: 255, nullable: true })
  marketplaceSlug: string | null;
}

2. Generate the Migration

Run the generate command with the full output path (no file extension):

bash
npm run migration:generate:dev -- src/db/migrations/DescriptiveMigrationName

Example:

bash
npm run migration:generate:dev -- src/db/migrations/AddMarketplaceSlugToAgents

TypeORM will diff the current database schema against your entities and produce a timestamped migration file, e.g.:

code
src/db/migrations/1770650039552-AddMarketplaceSlugToAgents.ts

3. Review the Generated Migration

Read the generated file and verify:

  • It contains only the expected changes (no unrelated drift)
  • up() and down() are symmetric (what up creates, down drops)

If the migration contains unexpected changes, your local database may be out of sync. Run npm run migration:run:dev first, then regenerate.

4. Run the Migration

bash
npm run migration:run:dev

5. Validate

bash
npx tsc --noEmit        # Types still compile
npm run test             # Tests pass
npm run docker:dev       # Service starts

Naming Convention

Use PascalCase describing the change:

ChangeMigration Name
Add a columnAddMarketplaceSlugToAgents
Create a tableCreateTeamSharesTable
Add an indexAddIndexOnThreadCreatedAt
Remove a columnRemoveUserShareScope
Add a constraintCascadeDeleteSharesOnScopeDelete

Commands Reference

bash
npm run migration:generate:dev -- src/db/migrations/Name   # Generate from entity diff
npm run migration:run:dev                                   # Apply pending migrations
npm run migration:revert:dev                                # Revert last migration
npm run migration:show:dev                                  # List migration status

Anti-Patterns

Don'tWhyInstead
Write migration SQL by handDrift between entities and schemaModify the entity, then auto-generate
Run generate without a pathFile lands in wrong locationAlways pass src/db/migrations/Name
Skip reviewing the generated fileMay include unrelated schema driftRead and verify before running
Commit a migration without running itMay fail at runtimeAlways migration:run:dev first
Edit a migration that's already been run in shared environmentsBreaks migration historyCreate a new migration instead