db-migration icon indicating copy to clipboard operation
db-migration copied to clipboard

[Enh] add tableOptions in Migration

Open pana1990 opened this issue 8 years ago • 7 comments

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?

pana1990 avatar Aug 04 '17 09:08 pana1990

to db component? How it look like in code?

githubjeka avatar Aug 04 '17 15:08 githubjeka

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 avatar Aug 04 '17 20:08 samdark

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

samdark avatar Aug 04 '17 20:08 samdark

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)

pana1990 avatar Aug 05 '17 19:08 pana1990

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

samdark avatar Aug 08 '17 10:08 samdark

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

cebe avatar Aug 14 '17 11:08 cebe

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';
     ]
],

santilin avatar Nov 09 '17 08:11 santilin