PHP code example of nobuhiko / ec-cube2-migration

1. Go to this page and download the library: Download nobuhiko/ec-cube2-migration library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nobuhiko / ec-cube2-migration example snippets




use Eccube2\Migration\Migration;
use Eccube2\Migration\Schema\Table;

class Version20260130000001_CreateLoginAttemptTable extends Migration
{
    public function up(): void
    {
        $this->create('dtb_login_attempt', function (Table $table) {
            // serial() は自動的に login_attempt_id を PRIMARY KEY として作成
            $table->serial();
            $table->text('login_id')->notNull();
            $table->text('ip_address')->nullable();
            $table->smallint('result')->notNull();
            $table->timestamp('create_date')->default('CURRENT_TIMESTAMP');

            $table->index(['login_id', 'create_date']);
        });
    }

    public function down(): void
    {
        $this->drop('dtb_login_attempt');
    }
}

public function up(): void
{
    $this->table('dtb_customer', function (Table $table) {
        // カラム追加
        $table->addColumn('reset_token', 'text')->nullable();
        $table->addColumn('reset_token_expire', 'timestamp')->nullable();

        // インデックス追加
        $table->addIndex(['reset_token']);
    });
}

public function down(): void
{
    $this->table('dtb_customer', function (Table $table) {
        $table->dropIndex('idx_dtb_customer_reset_token');
        $table->dropColumn('reset_token_expire');
        $table->dropColumn('reset_token');
    });
}

public function up(): void
{
    // 全DBで同じSQL
    $this->sql("UPDATE dtb_baseinfo SET shop_name = 'New Name'");

    // DB別に異なるSQL
    $this->sql(
        "SELECT DATE_ADD(NOW(), INTERVAL 1 DAY)",        // MySQL
        "SELECT NOW() + INTERVAL '1 day'",              // PostgreSQL
        "SELECT datetime('now', '+1 day')"              // SQLite
    );
}

$this->create('dtb_customer', function (Table $table) {
    $table->serial();  // customer_id が自動作成される
});

$this->create('dtb_login_attempt', function (Table $table) {
    $table->serial();  // login_attempt_id が自動作成される
});

$table->text('name')
    ->notNull()              // NOT NULL
    ->nullable()             // NULL許可(デフォルト)
    ->default('value');      // デフォルト値

// 主キーは serial() で自動設定されます
$table->serial();  // 自動的に PRIMARY KEY

$table->index(['column']);              // 通常のインデックス
$table->unique(['column']);             // ユニークインデックス
$table->index(['col1', 'col2']);        // 複合インデックス

$table->text('memo');
$table->index(['memo']);  // MySQL: CREATE INDEX ... ON ... (memo(255))

$table->index(['memo(100)']);  // memo(100) を使用
bash
# マイグレーションの作成
php data/vendor/bin/eccube migrate:create CreateLoginAttemptTable

# 未実行のマイグレーションを全て実行
php data/vendor/bin/eccube migrate

# ステータス確認
php data/vendor/bin/eccube migrate:status

# ロールバック(1つ戻す)
php data/vendor/bin/eccube migrate:rollback

# ロールバック(3つ戻す)
php data/vendor/bin/eccube migrate:rollback --steps=3