Generating migration from existing database in Yii or Laravel

后端 未结 9 900
南方客
南方客 2021-01-31 05:39

I\'m working on a project that has a fairly complex database (150+ tables). In order to be able to maintain changes, I\'ve decided to add migrations, preferably using Yii or Lar

9条回答
  •  后悔当初
    2021-01-31 06:20

    Well since migration is about setting up your database structure and make changes to it, not to reflect a current database there is no such way.

    And this is also not a step you have to make. You can start from where you are at the moment, which will make you able to rollback up to this point. Which means you can make migrations for your current tables without having to specify their entire structure, but just the changes only.

    Let's say you have a table called user and want to add their firstname to it.

    php artisan migrate:make add_firstname_to_user
    

    Now go into application/migrations and find the migration file, add this

    public function up()
    {
        Schema::table('user', function($table)
        {
            $table->string('firstname');
        });
    }
    
    public function down() {
        Schema::table('user', function($table)
        {
            $table->drop_column('firstname');
        });
    }
    

    Now you can add migrate it

    php artisan migrate:install // if you haven't run this, should only be once
    php artisan migrate
    

    .. and rollback using

    php artisan migrate:rollback
    

    This will add or drop the column firstname, without affecting your table in any other way.

提交回复
热议问题