Skip to content

Commit

Permalink
Merge pull request #165 from fortrabbit/fix/mysqldump-ssl
Browse files Browse the repository at this point in the history
Change ssl option until all dependencies fully support it
  • Loading branch information
Oliver Stark authored Apr 3, 2023
2 parents 3966222 + 015b91f commit e8aa3bc
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
jobs:
build:
name: Build
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
defaults:
run:
working-directory: ./docker
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.3.1 - 2023-04-03
- More tweaks on the mysql command line options

## 2.3.0 - 2023-02-06
- use `--defaults-extra-file` to support custom .my.cnf
- ignore `project.yaml` in production to prevent comparison of external and internal Project Config
Expand Down
2 changes: 1 addition & 1 deletion bin/craft-copy-import-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
'{MYSQL_DATABASE}' => getenv('MYSQL_DATABASE'),
];

$cmd = 'mysql --defaults-extra-file={EXTRA_FILE} --force {MYSQL_DATABASE} < {FILE} && echo 1';
$cmd = 'mysql --defaults-extra-file={EXTRA_FILE} --ssl-mode=DISABLED --force {MYSQL_DATABASE} < {FILE} && echo 1';
$cmd = str_replace(array_keys($tokens), array_values($tokens), $cmd);

$process = \Symfony\Component\Process\Process::fromShellCommandline($cmd);
Expand Down
15 changes: 4 additions & 11 deletions src/Actions/DbExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use Craft;
use craft\errors\ShellCommandException;
use fortrabbit\Copy\Helpers\MysqlConfigFile;
use fortrabbit\Copy\Plugin;
use ostark\Yii2ArtisanBridge\base\Action;
use yii\base\Exception;
use yii\console\ExitCode;

class DbExportAction extends Action
{
use MysqlConfigFile;

/**
* Export database from file
*
Expand All @@ -21,7 +24,7 @@ class DbExportAction extends Action
public function run(?string $file = null): int
{
$plugin = Plugin::getInstance();
$this->assureMyCfnForMysqldump();
$this->assureMyCnf();
$this->info("Creating DB Dump in '{$file}'");

try {
Expand All @@ -40,15 +43,5 @@ public function run(?string $file = null): int
}
}

protected function assureMyCfnForMysqldump(): bool
{
$mycnfDest = Craft::getAlias('@root') . '/.my.cnf';
$mycnfSrc = Plugin::PLUGIN_ROOT_PATH . '/.my.cnf.example';

if (! file_exists($mycnfDest)) {
return copy($mycnfSrc, $mycnfDest);
}

return true;
}
}
4 changes: 4 additions & 0 deletions src/Actions/DbImportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

use craft\errors\ShellCommandException;
use craft\helpers\FileHelper;
use fortrabbit\Copy\Helpers\MysqlConfigFile;
use fortrabbit\Copy\Plugin;
use ostark\Yii2ArtisanBridge\base\Action;
use yii\base\Exception;
use yii\console\ExitCode;

class DbImportAction extends Action
{
use MysqlConfigFile;

/**
* Import database from file
*
Expand All @@ -31,6 +34,7 @@ public function run(string $file): int
}

$this->info("Importing DB Dump from '{$file}'");
$this->assureMyCnf();

try {
$file = Plugin::getInstance()->database->import($file);
Expand Down
27 changes: 27 additions & 0 deletions src/Helpers/MysqlConfigFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace fortrabbit\Copy\Helpers;

use Craft;
use fortrabbit\Copy\Plugin;

trait MysqlConfigFile
{
public function assureMyCnf(): bool
{
$mycnfDest = Craft::getAlias('@root') . '/.my.cnf';
$mycnfSrc = Plugin::PLUGIN_ROOT_PATH . '/.my.cnf.example';

// Create
if (! file_exists($mycnfDest)) {
return copy($mycnfSrc, $mycnfDest);
}

// Update
if (file_get_contents($mycnfSrc) !== file_get_contents($mycnfDest)) {
return copy($mycnfSrc, $mycnfDest);
}

return true;
}
}
33 changes: 32 additions & 1 deletion src/Services/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function import(?string $file = null): string
{
$file = $this->prepareFile($file);

$this->alterCraftDefaultRestoreCommand($file);

$this->db->restore($file);

return $file;
Expand Down Expand Up @@ -81,10 +83,39 @@ protected function alterCraftDefaultBackupCommand(string $file): void
// The actual overwrite to allow .my.cnf again
// It basically reverts this change:
// https://github.com/craftcms/cms/commit/c1068dd56974172a98213b616461266711aef86a
Craft::$app->getConfig()->getGeneral()->backupCommand = str_replace(
$backupCommand = str_replace(
'--defaults-file',
'--defaults-extra-file',
$backupCommand
);

// Disable single-transaction for now
$backupCommand = str_replace(
' --single-transaction',
'',
$backupCommand
);

// Disable ssl-mode for now
$backupCommand = str_replace(
'--no-tablespaces',
'--no-tablespaces --ssl-mode=DISABLED',
$backupCommand
);

Craft::$app->getConfig()->getGeneral()->backupCommand = $backupCommand;
}

protected function alterCraftDefaultRestoreCommand(): void
{
// Determine the command that should be executed
$restoreCommand = $this->db->getSchema()->getDefaultRestoreCommand();

Craft::$app->getConfig()->getGeneral()->restoreCommand = str_replace(
'.cnf"',
'.cnf" --ssl-mode=DISABLED',
$restoreCommand
);

}
}

0 comments on commit e8aa3bc

Please sign in to comment.