pojokcodeid.nvim-lazy/snippets/codeigniter4/docs/MIGRATIONS.md
2023-03-03 07:09:50 +07:00

3.3 KiB

Alternate Snippets for Migrations

[ProjectRoot]/app/Database/Migrations/

Table of Content

Create Migration

Command

ci4:migration

Output

<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddBlog extends Migration
{
    public function up()
    {
        //
    }

    public function down()
    {
        //
    }
}
  • Migration Up

    Command

    ci4:migration:up
    

    Output

    public function up()
    {
      $this->forge->addField([
          'id' => [
              'type' => 'INT',
              'constraint' => 11,
              'unsigned' => true,
              'auto_increment' => true,
          ],
          'name' => [
              'type' => 'VARCHAR',
              'constraint' => 255,
          ],
          'created_at' => [
              'type' => 'DATETIME',
              'null' => true,
          ],
          'updated_at' => [
              'type' => 'DATETIME',
              'null' => true,
          ],
          'deleted_at' => [
              'type' => 'DATETIME',
              'null' => true,
          ],
      ]);
      $this->forge->addKey('id', true);
      $this->forge->createTable('tableName');
    }
    
  • Migration Down

    Command

    ci4:migration:down
    

    Output

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

Create Table

  • Add Column

    • BIGINT

      Command

      ci4:migration:bigint
      

      Output

      'columnName' => [
        'type' => 'BIGINT',
        'constraint' => '20',
        'null' => true,
      ],
      
    • CHAR

      Command

      ci4:migration:char
      

      Output

      'columnName' => [
        'type' => 'CHAR',
        'constraint' => '10',
        'null' => true,
      ],
      
    • DATETIME

      Command

      ci4:migration:datetime
      

      Output

      'columnName' => [
        'type' => 'DATETIME',
        'null' => true,
      ],
      
    • INT

      Command

      ci4:migration:int
      

      Output

      'columnName' => [
        'type' => 'INT',
        'constraint' => '11',
        'null' => true,
      ],
      
    • VARCHAR

      Command

      ci4:migration:varchar
      

      Output

      'columnName' => [
        'type' => 'VARCHAR',
        'constraint' => '255',
        'null' => true,
      ],
      
  • ID

    Command :

    ci4:migration:id
    

    Output :

    'id' => [
      'type' => 'INT',
      'constraint' => 11,
      'unsigned' => true,
      'auto_increment' => true,
    ],
    
  • Timestamp

    Command :

    ci4:migration:timestamp
    

    Output :

    'created_at' => [
      'type' => 'DATETIME',
      'null' => true,
    ],
    'updated_at' => [
      'type' => 'DATETIME',
      'null' => true,
    ],
    'deleted_at' => [
      'type' => 'DATETIME',
      'null' => true,
    ],