laravel-migrations
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLaravel Migrations
Laravel 数据库迁移
Agent Workflow (MANDATORY)
Agent 工作流(强制要求)
Before ANY implementation, use to spawn 3 agents:
TeamCreate- fuse-ai-pilot:explore-codebase - Check existing migrations
- fuse-ai-pilot:research-expert - Verify Laravel 12 patterns via Context7
- mcp__context7__query-docs - Check specific Schema Builder features
After implementation, run fuse-ai-pilot:sniper for validation.
在进行任何实现之前,使用生成3个Agent:
TeamCreate- fuse-ai-pilot:explore-codebase - 检查现有迁移文件
- fuse-ai-pilot:research-expert - 通过Context7验证Laravel 12的开发模式
- mcp__context7__query-docs - 查阅Schema Builder的特定功能
完成实现后,运行fuse-ai-pilot:sniper进行验证。
Overview
概述
| Feature | Description |
|---|---|
| Schema Builder | Create, modify, drop tables |
| Columns | 50+ column types with modifiers |
| Indexes | Primary, unique, fulltext, spatial |
| Foreign Keys | Constraints with cascade options |
| Seeders | Populate tables with data |
| 功能 | 说明 |
|---|---|
| Schema Builder | 创建、修改、删除表 |
| 列 | 50+种列类型及修饰符 |
| 索引 | 主键、唯一、全文、空间索引 |
| 外键 | 包含级联选项的约束 |
| 数据填充器 | 为表填充测试或初始数据 |
Critical Rules
关键规则
- Always define down() - Reversible migrations
- Use foreignId()->constrained() - Not raw unsignedBigInteger
- Add indexes on foreign keys - Performance critical
- Test rollback before deploy - Validate down() works
- Never modify deployed migrations - Create new ones
- 必须定义down()方法 - 支持可逆迁移
- 使用foreignId()->constrained() - 不要使用原生unsignedBigInteger
- 为外键添加索引 - 对性能至关重要
- 部署前测试回滚 - 验证down()方法可用
- 切勿修改已部署的迁移文件 - 应创建新的迁移文件
Decision Guide
决策指南
Migration Type
迁移类型
Need schema change?
├── New table → make:migration create_X_table
├── Add column → make:migration add_X_to_Y_table
├── Modify column → make:migration modify_X_in_Y_table
├── Add index → make:migration add_index_to_Y_table
└── Seed data → make:seeder XSeeder需要修改架构?
├── 新建表 → make:migration create_X_table
├── 添加列 → make:migration add_X_to_Y_table
├── 修改列 → make:migration modify_X_in_Y_table
├── 添加索引 → make:migration add_index_to_Y_table
└── 填充数据 → make:seeder XSeederColumn Type Selection
列类型选择
| Use Case | Type | Example |
|---|---|---|
| Primary Key | | Auto-increment BIGINT |
| Foreign Key | | References parent |
| UUID Primary | | UUIDs |
| Boolean | | is_active |
| Enum | | order_status |
| JSON | | preferences |
| Money | | 99999999.99 |
| Timestamps | | created_at, updated_at |
| Soft Delete | | deleted_at |
| 使用场景 | 类型 | 示例 |
|---|---|---|
| 主键 | | 自增BIGINT |
| 外键 | | 关联父表 |
| UUID主键 | | UUID标识符 |
| 布尔值 | | is_active |
| 枚举 | | order_status |
| JSON类型 | | preferences |
| 金额 | | 99999999.99 |
| 时间戳 | | created_at, updated_at |
| 软删除 | | deleted_at |
Foreign Key Cascade
外键级联选项
| Scenario | onDelete | Use Case |
|---|---|---|
| Strict integrity | | Financial records |
| Auto-cleanup | | Post → Comments |
| Preserve with null | | Optional relations |
| No action | | Audit logs |
| 场景 | onDelete 选项 | 适用案例 |
|---|---|---|
| 严格数据完整性 | | 财务记录 |
| 自动清理关联数据 | | 文章 → 评论 |
| 保留数据并设为null | | 可选关联关系 |
| 无操作 | | 审计日志 |
Reference Guide
参考指南
Core Concepts
核心概念
| Topic | Reference | When to Consult |
|---|---|---|
| Schema | schema.md | Table operations |
| Columns | columns.md | Column types |
| Indexes | indexes.md | Performance indexes |
| Foreign Keys | foreign-keys.md | Constraints |
| Commands | commands.md | Artisan commands |
| Seeding | seeding.md | Populate data |
| 主题 | 参考文档 | 查阅场景 |
|---|---|---|
| Schema | schema.md | 表操作相关 |
| 列 | columns.md | 列类型选择 |
| 索引 | indexes.md | 性能优化索引 |
| 外键 | foreign-keys.md | 约束配置 |
| 命令 | commands.md | Artisan命令使用 |
| 数据填充 | seeding.md | 数据填充操作 |
Advanced Topics
进阶主题
| Topic | Reference | When to Consult |
|---|---|---|
| Testing | testing.md | Test migrations |
| Production | production.md | Deploy safely |
| Troubleshooting | troubleshooting.md | Fix errors |
| 主题 | 参考文档 | 查阅场景 |
|---|---|---|
| 测试 | testing.md | 迁移测试 |
| 生产环境 | production.md | 安全部署 |
| 故障排查 | troubleshooting.md | 错误修复 |
Templates
模板
| Template | When to Use |
|---|---|
| CreateTableMigration.php.md | New table |
| ModifyTableMigration.php.md | Alter table |
| PivotTableMigration.php.md | Many-to-many |
| Seeder.php.md | Seed patterns |
| MigrationTest.php.md | Test migrations |
| 模板 | 使用场景 |
|---|---|
| CreateTableMigration.php.md | 新建表 |
| ModifyTableMigration.php.md | 修改表 |
| PivotTableMigration.php.md | 多对多关联表 |
| Seeder.php.md | 数据填充器模板 |
| MigrationTest.php.md | 迁移测试模板 |
Quick Reference
快速参考
Create Table
创建表
php
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->text('content');
$table->enum('status', ['draft', 'published'])->default('draft');
$table->timestamps();
$table->softDeletes();
$table->index(['user_id', 'status']);
});php
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->text('content');
$table->enum('status', ['draft', 'published'])->default('draft');
$table->timestamps();
$table->softDeletes();
$table->index(['user_id', 'status']);
});Modify Table
修改表
php
Schema::table('posts', function (Blueprint $table) {
$table->string('slug')->after('title')->unique();
$table->boolean('featured')->default(false);
});php
Schema::table('posts', function (Blueprint $table) {
$table->string('slug')->after('title')->unique();
$table->boolean('featured')->default(false);
});Commands
命令
bash
php artisan make:migration create_posts_table
php artisan migrate
php artisan migrate:rollback --step=1
php artisan migrate:fresh --seedbash
php artisan make:migration create_posts_table
php artisan migrate
php artisan migrate:rollback --step=1
php artisan migrate:fresh --seedBest Practices
最佳实践
DO
建议
- Use for foreign keys
foreignId()->constrained() - Add composite indexes for common queries
- Test down() method before deploying
- Use to preview SQL
--pretend
- 使用定义外键
foreignId()->constrained() - 为常用查询添加复合索引
- 部署前测试down()方法
- 使用参数预览SQL语句
--pretend
DON'T
不建议
- Modify already-deployed migrations
- Forget down() method
- Use raw SQL without Schema Builder
- Skip indexes on foreign keys
- 修改已部署的迁移文件
- 遗漏down()方法
- 绕过Schema Builder直接使用原生SQL
- 不为外键添加索引