diff --git a/src/migrations/m200123_081049_convert_settings_to_contact_template.php b/src/migrations/m200123_081049_convert_settings_to_contact_template.php index 5a919f0..7e45cf6 100644 --- a/src/migrations/m200123_081049_convert_settings_to_contact_template.php +++ b/src/migrations/m200123_081049_convert_settings_to_contact_template.php @@ -15,57 +15,60 @@ class m200123_081049_convert_settings_to_contact_template extends Migration */ public function safeUp() { - $schemaRows = (new Query()) - ->select(['name' => new Expression('SUBSTR(`key`, 1, LOCATE(".", `key`) - 1)')]) - ->from('{{%settings}}') - ->where(['section' => 'contact']) - ->groupBy('name') - ->all(); - $schemaNames = ArrayHelper::getColumn($schemaRows, 'name'); - - - foreach ($schemaNames as $schemaName) { - $settingRows = (new Query()) - ->select([ - 'value', - 'identifier' => new Expression('SUBSTR(`key`, LOCATE(".", `key`) + 1)'), - 'id' - ]) + if ($this->db->getDriverName() === 'mysql') { + $schemaRows = (new Query()) + ->select(['name' => new Expression('SUBSTR([[key]], 1, LOCATE(".", [[key]]) - 1)')]) ->from('{{%settings}}') ->where(['section' => 'contact']) - ->andWhere("`key` LIKE '{$schemaName}.%'") + ->groupBy('name') ->all(); - $columns = [ - 'name' => $schemaName, - 'created_at' => new Expression('NOW()'), - 'updated_at' => new Expression('NOW()') - ]; + $schemaNames = ArrayHelper::getColumn($schemaRows, 'name'); + + + foreach ($schemaNames as $schemaName) { + $settingRows = (new Query()) + ->select([ + 'value', + 'identifier' => new Expression('SUBSTR([[key]], LOCATE(".", [[key]]) + 1)'), + 'id' + ]) + ->from('{{%settings}}') + ->where(['section' => 'contact']) + ->andWhere("[[key]] LIKE '{$schemaName}.%'") + ->all(); - $settingsIds = []; + $columns = [ + 'name' => $schemaName, + 'created_at' => new Expression('NOW()'), + 'updated_at' => new Expression('NOW()') + ]; - foreach ($settingRows as $settingRow) { - $value = $settingRow['value']; - switch ($settingRow['identifier']) { - case 'schema': - $columns['form_schema'] = $value; - break; - case 'toEmail': - $columns['to_email'] = $value; - break; - case 'fromEmail': - $columns['from_email'] = $value; - break; - case 'subject': - $columns['email_subject'] = $value; - break; + $settingsIds = []; + + foreach ($settingRows as $settingRow) { + $value = $settingRow['value']; + switch ($settingRow['identifier']) { + case 'schema': + $columns['form_schema'] = $value; + break; + case 'toEmail': + $columns['to_email'] = $value; + break; + case 'fromEmail': + $columns['from_email'] = $value; + break; + case 'subject': + $columns['email_subject'] = $value; + break; + } + $settingsIds[] = $settingRow['id']; } - $settingsIds[] = $settingRow['id']; - } - $this->insert('{{%dmstr_contact_template}}', $columns); - $this->delete('{{%settings}}', ['id' => $settingsIds]); + $this->insert('{{%dmstr_contact_template}}', $columns); + $this->delete('{{%settings}}', ['id' => $settingsIds]); + } } } diff --git a/src/migrations/m200123_115704_alter_contact_log_table.php b/src/migrations/m200123_115704_alter_contact_log_table.php index 53702da..76b1ba2 100644 --- a/src/migrations/m200123_115704_alter_contact_log_table.php +++ b/src/migrations/m200123_115704_alter_contact_log_table.php @@ -11,9 +11,11 @@ class m200123_115704_alter_contact_log_table extends Migration public function up() { + $dateTimeType = $this->db->getDriverName() === 'mysql' ? 'DATETIME' : 'TIMESTAMP'; + $this->alterColumn('{{%dmstr_contact_log}}', 'json', 'TEXT NOT NULL'); - $this->alterColumn('{{%dmstr_contact_log}}', 'created_at', 'DATETIME NULL'); - $this->addColumn('{{%dmstr_contact_log}}', 'updated_at', 'DATETIME NULL AFTER created_at'); + $this->alterColumn('{{%dmstr_contact_log}}', 'created_at', $dateTimeType); + $this->addColumn('{{%dmstr_contact_log}}', 'updated_at', $dateTimeType); $schemaCluster = []; @@ -32,7 +34,14 @@ public function up() } $this->renameColumn('{{%dmstr_contact_log}}', 'schema', 'contact_template_id'); - $this->alterColumn('{{%dmstr_contact_log}}', 'contact_template_id', 'INT(11) NOT NULL'); + + if ($this->db->getDriverName() === 'pgsql') { + $columnType = $this->integer() . ' USING contact_template_id::integer'; + } else { + $columnType = 'INT(11) NULL'; + } + + $this->alterColumn('{{%dmstr_contact_log}}', 'contact_template_id', $columnType); }