Laravel 5 的 Migrations相关(转)

说明
以前我们团队进行开发,如果数据库结构修改,就要从MySQLWorkbench把整个库或某个表的SQL语句导出来再提交到git, 别人用SQL语句重建某个库或某个表,这样好麻烦。Laravel Migrations就可以避免做这件事。只需要获取到migrations的文件,再migrate就可以了,而且还可以rollback
数据库配置
在.env文件配置数据库环境的信息。
相关的migrate命令
  • 创建migrations表记录相关的动作

    php artisan migrate:install
    
  • 更新数据库信息

    php artisan migrate
    
  • 回滚migrate操作

    php artisan migrate:rollback
    
  • 回滚到所有的migrate操作之前

    php artisan migrate:reset
    
  • 回滚到最开始的时候然后运行所有的mirgrations

    php artisan migrate:refresh
    
  • 查看migration状态

    php artisan migrate:status
    
  • 创建migration文件

    php artisan make:migration
    
    创建的所有文件在 database/migrations目录下
    
    • 查看帮助

      php artisan help make:migration
      
    • 指定创建的migration文件名

      php artisan make:migration create_admins_table
      
    • 创建migration指定创建数据库表名

      php artisan make:migration create_authors_table --create=authors
      
      执行这个命令生成的代码, up 和 down方法自动填充了相关创建表和删除表的代码
      
    • 创建migration指定修改的数据库表名

      php artisan make:migration set_email_to_unique_on_authors_table --table=authors
      执行这个命令生成的代码, up 和 down自带了指定表名的代码
      
migrations文件
所有的migrations文件在database/migrations目录下。

每个migration文件都有两个方法: up & down,up 表示执行migrate时候要执行的操作,down表示回滚up方法所进行的操作。这两个方法需要自行编写。

一般在up方法定义数据表结构, 例如:

public function up()
{
    Schema::create('authors', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}

直接用$table的相关方法设置数据表结构,相关的方法见Laravel 5的文档migrations节。
migrations文件中表的相关操作
  • 判断表是否存在

    if (Schema::hasTable('users')) {
    
    }
    
  • 指定connection

    Schema::connection('foo')->create('users', function ($table) {
        $table->increments('id');
    });
    
  • 指定数据库引擎

    Schema::create('users', function ($table) {
        $table->engine = 'InnoDB';
    
        $table->increments('id');
    });
    
  • 重命名

    Schema::rename($from, $to);
    
  • 删除表

    Schema::drop('users');
    Schema::dropIfExists('users');
    
migrations文件中列的相关操作
  • 创建列

    Schema::create('authors', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
    
  • 判断列是否存在

    if (Schema::hasColumn('users', 'email')) {
    
    }
    
  • 修改列 -- 使用change方法

    Schema::table('users', function ($table) {
        $table->string('name', 50)->change();
    });
    
  • 重命名列

    Schema::table('users', function ($table) {
        $table->renameColumn('from', 'to');
    });
    
  • 删除列

    Schema::table('users', function ($table) {
        $table->dropColumn('votes');
        $table->dropColumn(['votes', 'avatar', 'location']);
    });
    
migrations文件中索引的相关操作
  • 创建索引

    • 唯一性索引 -- 使用unique方法

      $table->string('email')->unique();
      
    • 复合索引

      $table->index(['account_id', 'created_at']);
      
    • 主键索引

      $table->primary('id');
      $table->primary(['first', 'last']);
      
    • 普通索引

      $table->index('state');
      
  • 删除索引

    必须提定索引的名称,索引的名称: 表名字段名索引类别

    • 删除主键索引

      $table->dropPrimary('users_id_primary');
      
    • 删除唯一索引

      $table->dropUnique('users_email_unique');
      
    • 删除普通索引

      $table->dropIndex('geo_state_index');
      
migrations文件中外键的相关操作
  • 创建外键

    在posts表中创建个user_id字段跟users表的id字段做外键约束

    Schema::table('posts', function ($table) {
        $table->integer('user_id')->unsigned();
    
        $table->foreign('user_id')->references('id')->on('users');
    });
    
  • 删除外建

    外键的名称: 表名_外键字段_foreign
    
    $table->dropForeign('posts_user_id_foreign');

猜你喜欢

转载自xialluyouyue.iteye.com/blog/2251120