yiisoft / db-migration

The package implementing migration for yiisoft/db.
https://www.yiiframework.com/
BSD 3-Clause "New" or "Revised" License
29 stars 16 forks source link

[Enh] add tableOptions in Migration #20

Open pana1990 opened 7 years ago

pana1990 commented 7 years ago

Yii use the following code in multiple places :

public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
        $this->createTable('{{%whatever}}', [

        ], $tableOptions);
    }

https://github.com/yiisoft/yii2/blob/master/framework/rbac/migrations/m140506_102106_rbac_init.php#L56

https://github.com/yiisoft/yii2/blob/master/framework/web/migrations/m160313_153426_session_init.php#L26

https://github.com/yiisoft/yii2/blob/master/framework/caching/migrations/m150909_153426_cache_init.php#L44

some extensions as well

https://github.com/dektrium/yii2-user/blob/master/migrations/Migration.php#L39

you like we include this option to global custom tableOptions in configuration file?

what do you think about it?

githubjeka commented 7 years ago

to db component? How it look like in code?

samdark commented 7 years ago

In some of my projects I'm using custom Migration class instead:

class Migration extends \yii\db\Migration
{
    /**
     * @inheritdoc
     */
    public function createTable($table, $columns, $options = null)
    {
        if ($options === null && $this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }
        parent::createTable($table, $columns, $options);
    }
}
samdark commented 7 years ago

@pana1990 how should it look like in the config file?

pana1990 commented 7 years ago

I use that fragment code as well but if you have any third extension that uses migrations it is not as easy, i have move migrations to my migration path and change yii\db\migration for my custom Migration class

i am not sure but i imagine something like this one :

// console.php
'migrate' => [
      'tableOptions => 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB' // my custom tableOptions
],

if tableOptions is null it get value by default of dbms (CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB for mysql for example)

samdark commented 7 years ago

I'd introduce defaultTableOptions into migration class and a corresponding property into the command so it could be configured.

cebe commented 7 years ago

https://github.com/yiisoft-contrib/yiiframework.com/blob/059fef69b972404e21fa7d32c7e5e7b955ca7799/migrations/BaseMigration.php#L18

santilin commented 6 years ago

I think those setting should be configured in a per driver basis. For example, in console.php:

// console.php
'migrate' => [
      'mysql' => [
          'tableOptions => 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB' // my custom tableOptions
     ], 
    'sqlite' => [
         'tableOptions' => 'WITHOUT ROWID';
     ]
],