마이그레이션 업그레이드

문서

변경된 사항

  • 우선, 마이그레이션의 순차적 명명(001_create_users, 002_create_posts)이 더 이상 지원되지 않습니다. CodeIgniter 버전 4는 타임스탬프 방식 (20121031100537_create_users, 20121031500638_create_posts)만 지원합니다. 순차적 명명을 사용한 경우 각 마이그레이션 파일의 이름을 변경해야 합니다.

  • 마이그레이션 테이블 정의가 변경되었습니다. CI3에서 CI4로 업그레이드하고 동일한 데이터베이스를 사용하는 경우, 마이그레이션 테이블 정의와 데이터를 업그레이드해야 합니다.

  • 마이그레이션 절차도 변경되었습니다. 이제 간단한 CLI 명령어로 데이터베이스를 마이그레이션할 수 있습니다:

php spark migrate

업그레이드 가이드

  1. v3 프로젝트에서 순차적 마이그레이션 이름을 사용하는 경우 타임스탬프 이름으로 변경해야 합니다.

  2. 모든 마이그레이션 파일을 새 폴더 app/Database/Migrations로 이동해야 합니다.

  3. defined('BASEPATH') OR exit('No direct script access allowed'); 줄이 있으면 제거하십시오.

  4. PHP 여는 태그 바로 뒤에 다음 줄을 추가하십시오: namespace App\Database\Migrations;

  5. namespace App\Database\Migrations; 줄 아래에 다음 줄을 추가하십시오: use CodeIgniter\Database\Migration;

  6. extends CI_Migrationextends Migration으로 교체하십시오.

  7. Forge 클래스 내의 메서드 이름이 camelCase를 사용하도록 변경되었습니다. 예를 들어:

    • $this->dbforge->add_field$this->forge->addField

    • $this->dbforge->add_key$this->forge->addKey

    • $this->dbforge->create_table$this->forge->addTable

    • $this->dbforge->drop_table$this->forge->addTable

  8. (선택 사항) 배열 구문을 array(...)에서 [...]로 변경할 수 있습니다.

  9. 동일한 데이터베이스를 사용하는 경우 마이그레이션 테이블을 업그레이드하십시오.

    • (개발) 새 마이그레이션 테이블을 생성하기 위해 개발 환경 또는 완전히 새로운 데이터베이스에서 CI4 마이그레이션을 실행하십시오.

    • (개발) 마이그레이션 테이블을 내보내십시오.

    • (프로덕션) 기존 CI3 마이그레이션 테이블을 삭제(또는 이름 변경)하십시오.

    • (프로덕션) 새 마이그레이션 테이블과 데이터를 가져오십시오.

코드 예제

CodeIgniter 버전 3.x

경로: application/migrations:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_blog extends CI_Migration
{
    public function up()
    {
        $this->dbforge->add_field(array(
            'blog_id' => array(
                'type'           => 'INT',
                'constraint'     => 5,
                'unsigned'       => true,
                'auto_increment' => true,
            ),
            'blog_title' => array(
                'type'       => 'VARCHAR',
                'constraint' => '100',
            ),
            'blog_description' => array(
                'type' => 'TEXT',
                'null' => true,
            ),
        ));
        $this->dbforge->add_key('blog_id', true);
        $this->dbforge->create_table('blog');
    }

    public function down()
    {
        $this->dbforge->drop_table('blog');
    }
}

CodeIgniter 버전 4.x

경로: app/Database/Migrations:

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddBlog extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'blog_id' => [
                'type'           => 'INT',
                'constraint'     => 5,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'blog_title' => [
                'type'       => 'VARCHAR',
                'constraint' => '100',
            ],
            'blog_description' => [
                'type' => 'TEXT',
                'null' => true,
            ],
        ]);
        $this->forge->addKey('blog_id', true);
        $this->forge->createTable('blog');
    }

    public function down()
    {
        $this->forge->dropTable('blog');
    }
}

검색 및 교체

다음 표를 사용하여 이전 CI3 파일에서 검색 및 교체할 수 있습니다.

검색

교체

extends CI_Migration

extends Migration

$this->dbforge->add_field

$this->forge->addField

$this->dbforge->add_key

$this->forge->addKey

$this->dbforge->create_table

$this->forge->createTable

$this->dbforge->drop_table

$this->forge->dropTable