Fix 43 add table options

This commit is contained in:
Antonio Ramirez
2017-08-06 11:53:42 +02:00
parent db4458ab12
commit b72abab0ee
5 changed files with 78 additions and 7 deletions

View File

@ -0,0 +1,59 @@
<?php
namespace Da\User\Helper;
use RuntimeException;
class MigrationHelper
{
/**
* @param string $driverName
*
* @return null|string
*/
public static function resolveTableOptions($driverName)
{
switch ($driverName) {
case 'mysql':
return 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
case 'pgsql':
case 'dblib':
case 'mssql':
case 'sqlsrv':
return null;
default:
throw new RuntimeException('Your database is not supported!');
}
}
/**
* @param $driverName
*
* @return string
*/
public static function resolveDbType($driverName)
{
switch ($driverName) {
case 'mysql':
return $driverName;
case 'pgsql':
return $driverName;
case 'dblib':
case 'mssql':
case 'sqlsrv':
return 'sqlsrv';
default:
throw new RuntimeException('Your database is not supported!');
}
}
/**
* @param string $driverName
*
* @return bool
*/
public static function isMicrosoftSQLServer($driverName)
{
return self::resolveDbType($driverName) == 'sqlsrv';
}
}

View File

@ -11,6 +11,7 @@
namespace Da\User\Migration;
use Da\User\Helper\MigrationHelper;
use yii\db\Migration;
class m000000_000001_create_user_table extends Migration
@ -32,7 +33,8 @@ class m000000_000001_create_user_table extends Migration
'blocked_at' => $this->integer(),
'updated_at' => $this->integer()->notNull(),
'created_at' => $this->integer()->notNull(),
]
],
MigrationHelper::resolveTableOptions($this->db->driverName)
);
$this->createIndex('idx_user_username', '{{%user}}', 'username', true);

View File

@ -11,6 +11,7 @@
namespace Da\User\Migration;
use Da\User\Helper\MigrationHelper;
use yii\db\Migration;
class m000000_000002_create_profile_table extends Migration
@ -29,10 +30,13 @@ class m000000_000002_create_profile_table extends Migration
'website' => $this->string(255),
'timezone' => $this->string(40),
'bio' => $this->text(),
]
],
MigrationHelper::resolveTableOptions($this->db->driverName)
);
$this->addForeignKey('fk_profile_user', '{{%profile}}', 'user_id', '{{%user}}', 'id', 'CASCADE', 'RESTRICT');
$restrict = MigrationHelper::isMicrosoftSQLServer($this->db->driverName) ? 'NO ACTION' : 'RESTRICT';
$this->addForeignKey('fk_profile_user', '{{%profile}}', 'user_id', '{{%user}}', 'id', 'CASCADE', $restrict);
}
public function down()

View File

@ -11,6 +11,7 @@
namespace Da\User\Migration;
use Da\User\Helper\MigrationHelper;
use yii\db\Migration;
class m000000_000003_create_social_account_table extends Migration
@ -29,7 +30,8 @@ class m000000_000003_create_social_account_table extends Migration
'username' => $this->string(255),
'data' => $this->text(),
'created_at' => $this->integer(),
]
],
MigrationHelper::resolveTableOptions($this->db->driverName)
);
$this->createIndex(
@ -48,7 +50,7 @@ class m000000_000003_create_social_account_table extends Migration
'{{%user}}',
'id',
'CASCADE',
'RESTRICT'
(MigrationHelper::isMicrosoftSQLServer($this->db->driverName) ? 'NO ACTION' : 'RESTRICT')
);
}

View File

@ -11,6 +11,7 @@
namespace Da\User\Migration;
use Da\User\Helper\MigrationHelper;
use yii\db\Migration;
class m000000_000004_create_token_table extends Migration
@ -24,12 +25,15 @@ class m000000_000004_create_token_table extends Migration
'code' => $this->string(32)->notNull(),
'type' => $this->smallInteger(6)->notNull(),
'created_at' => $this->integer()->notNull(),
]
],
MigrationHelper::resolveTableOptions($this->db->driverName)
);
$this->createIndex('idx_token_user_id_code_type', '{{%token}}', ['user_id', 'code', 'type'], true);
$this->addForeignKey('fk_token_user', '{{%token}}', 'user_id', '{{%user}}', 'id', 'CASCADE', 'RESTRICT');
$restrict = MigrationHelper::isMicrosoftSQLServer($this->db->driverName) ? 'NO ACTION' : 'RESTRICT';
$this->addForeignKey('fk_token_user', '{{%token}}', 'user_id', '{{%user}}', 'id', 'CASCADE', $restrict);
}
public function down()