Tự học Laravel: (7) DB VERSION CONTROL IN MIGRATION

Let’s use the migration function to manage the database version.

  • Perform migration and create tables in DB
  • Roll back and return DB version
  • Look at the contents of the migration file

DB ENVIRONMENT SETTINGS

First of all, configure the DB environment. For the DB environment settings , refer to the database environment settings written earlier .

In this article I will specify SQLite as the database.

# .env
 
DB_CONNECTION=sqlite
 
# sqlite のデフォルト設定を使うのでコメントアウト
# DB_DATABASE=homestead

Manually create a SQLite database file.

touch database/database.sqlite 

EXECUTION OF MIGRATION

In fact, two migration files were created when the Laravel project was created.

database / 
mig migrations 
    2014 2014_10_12_000000_create_users_table.php 
    2014 2014_10_12_100000_create_password_resets_table.php

These two files are contents to create a table respectively. When Laravel creates a project, it adds built-in user authentication. Therefore, there is already a migration file that creates a table for users and a table for password reconfirmation. I will leave the user authentication topic this time and focus on migration.

Now let’s run these.

Artisan Migrate Php 

. Migration Table Created Successfully 
Migrating: 2014_10_12_000000_Create_users_table 
Migrated: 2014_10_12_000000_Create_users_table 
Migrating: 2014_10_12_100000_Create_password_resets_table 
Migrated: 2014_10_12_100000_Create_password_resets_table

Use the sqlite3 command to check if the table has been created.

sqlite3 database/database.sqlite 

sqlite> .tables 
migrations password_resets users 

Three tables have been created.

  • migrations
  • users
  • password_resets

If you look at the contents of the migrations table, the executed migration file name was recorded.

sqlite> select * from migrations; 
migration batch      
------------------------------------------- ----- 
2014_10_12_000000_create_users_table 1          
2014_10_12_100000_create_password_re 1       

Exit from sqlite3 once.

sqlite> .q

If you try to run the migration again, it will end without doing anything.

php artisan migrate 

Nothing to migrate.

MIGRATION ROLLBACK

Cancel table creation and restore DB to the previous state.

php artisan migrate:rollback 

Rolling back: 2014_10_12_100000_create_password_resets_table 
Rolled back: 2014_10_12_100000_create_password_resets_table 
Rolling back: 2014_10_12_000000_create_users_table 
Rolled back: 2014_10_12_000000_create_users_table

Use the sqlite3 command to check if the table has been dropped.

sqlite3 database/database.sqlite 

sqlite> .tables 
migrations 

sqlite> select * from migrations; 
sqlite>   

The users, password_resets table was deleted, and the contents of the migrations table were empty. The migrations table is still present, but everything else is back to what it was before the migration was performed.


CONTENTS OF MIGRATION FILE

Let’s look at the contents of the migration file.

<?php //database/migrations/2014_10_12_000000_create_users_table.php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }
 
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

The CreateUsersTable class is defined, which inherits from the Migration class. 
The points are the up() and donw() functions.
Write the DB version upgrade process at migration execution time to the up() function. On the other hand, describe down processing of DB version when rollback is executed in the down() function. In the above file, create the users table with up() and delete the users table with down().

SCHEMA BUILDER

The part written as Schema::xxxx is the part that actually performs operations on the DB, and these are read as schema builders.

  • Schema::create(‘table name’, Closure $callback) – create table
  • Schema::drop(‘table name’) – drop table

You can check the details of the schema builder on the following page.
https://laravel.com/docs/master/migrations


SUMMARY

Managing DB version by migration makes DB rebuild easier. It makes it easy to match DB consistency on multiple developers’ PCs, and also makes it easy to upgrade DB versions in staging and production environments.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.