Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration customizations #7

Closed
dersonsena opened this issue Sep 17, 2019 · 0 comments
Closed

Migration customizations #7

dersonsena opened this issue Sep 17, 2019 · 0 comments
Labels
improvement General improvements

Comments

@dersonsena
Copy link
Owner

In the config/console.php file, update the migrate values to:

'migrate' => [
    'class' => 'yii\console\controllers\MigrateController',
    'templateFile' => '@app/Core/Migration/migration-template.php',
    'migrationPath' => null,
    'migrationNamespaces' => ['App\Migrations']
]

Create the package Core/Migration with 2 files:

MigrationAbstract file:

<?php

namespace App\Core\Migration;

use yii\db\Expression;
use yii\db\Migration;

abstract class MigrationAbstract extends Migration
{
    /**
     * Setup the binary id column
     */
    public function binaryId(): string
    {
        return "BINARY(16) NOT NULL COMMENT '(DC2Type:uuid_binary_ordered_time)'";
        //return $this->binary(16)->notNull()->unique()->comment('(DC2Type:uuid_binary_ordered_time)');
    }

    /**
     * Setup the UUID column
     * @return \yii\db\ColumnSchemaBuilder
     */
    public function uuid()
    {
        return $this->string(36)->notNull()->unique()->comment('(DC2Type:uuid)');
    }

    /**
     * Setup the status column
     * @return \yii\db\ColumnSchemaBuilder
     */
    public function status()
    {
        return $this->smallInteger(1)->notNull()->defaultValue(1);
    }

    /**
     * Setup the deleted column
     * @return \yii\db\ColumnSchemaBuilder
     */
    public function deleted()
    {
        return $this->smallInteger(1)->notNull()->defaultValue(0);
    }

    /**
     * Returns the blame and timed events columns list
     * @return array
     */
    public function blameAndTimedEventsColumns(): array
    {
        return array_merge(
            $this->timedEventsColumns(),
            $this->blameColumns()
        );
    }

    /**
     * Returns the blame columns list
     * @return array
     */
    public function blameColumns(): array
    {
        return [
            'created_by' => $this->string(100)->notNull(),
            'updated_by' => $this->string(100)->defaultValue(new Expression('NULL')),
            'deleted_by' => $this->string(100)->defaultValue(new Expression('NULL')),
        ];
    }

    /**
     * Returns the timed events columns list
     * @return array
     */
    public function timedEventsColumns(): array
    {
        return [
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->defaultValue(new Expression('NULL')),
            'deleted_at' => $this->dateTime()->defaultValue(new Expression('NULL')),
        ];
    }
}

migration-template.php file:

<?php
/**
 * This view is used by console/controllers/MigrateController.php.
 *
 * The following variables are available in this view:
 */
/* @var $className string the new migration class name without namespace */
/* @var $namespace string the new migration class namespace */

echo "<?php\n";
if (!empty($namespace)) {
    echo "\nnamespace {$namespace};\n";
}
?>

use App\Core\Migration\MigrationAbstract;

/**
 * Class <?= $className . "\n" ?>
 */
class <?= $className ?> extends MigrationAbstract
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        // set the table columns
        $columns = [
            'id' => $this->binaryId(),
            'uuid' => $this->uuid(),
            'status' => $this->status(),
            'deleted' => $this->deleted()
        ];

        $this->createTable(
            '{{%table_name}}',
            array_merge($columns, $this->blameAndTimedEventsColumns()),
            'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'
        );

        $this->addPrimaryKey('pk_table_name_id', '{{%table_name}}', 'id');
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('{{%table_name}}');
    }
}
@dersonsena dersonsena added the improvement General improvements label Oct 2, 2019
dersonsena added a commit that referenced this issue Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement General improvements
Projects
None yet
Development

No branches or pull requests

1 participant