Skip to content

Commit

Permalink
Merge pull request #5 from dmstr/feature/postgres-support
Browse files Browse the repository at this point in the history
Added support for postgres db
  • Loading branch information
schmunk42 authored Aug 19, 2021
2 parents 97502e6 + 5d70439 commit 32ee07a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions src/migrations/m200123_115704_alter_contact_log_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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);

}

Expand Down

0 comments on commit 32ee07a

Please sign in to comment.