Skip to content

Commit

Permalink
Merge pull request #31 from dmstr/feature/updated-export
Browse files Browse the repository at this point in the history
updated export filename
  • Loading branch information
schmunk42 authored Apr 28, 2021
2 parents d9223fa + 45b0187 commit febb1ea
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions console/controllers/MysqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ class MysqlController extends Controller
*/
public function options($actionId)
{
switch ($actionId) {
switch (true) {
case $actionId == 'dump':
$additionalOptions = ['noDataTables', 'dryRun'];
break;
case $actionId = 'import';
case $actionId == 'import';
$additionalOptions = ['cache', 'dryRun'];
break;
case $actionId == 'export':
Expand All @@ -96,6 +96,7 @@ public function options($actionId)
default:
$additionalOptions = [];
}

return array_merge(
parent::options($actionId),
['verbose', 'db', 'outputPath'], // global options
Expand Down Expand Up @@ -272,7 +273,10 @@ public function actionDestroy(
*/
public function actionExport()
{
$fileName = $this->getFilePrefix() . "_data.sql";
$latestMigrationId = $this->getLatestMigrationId();
$date = date('ymd_His');
$fileName = "{$latestMigrationId}x_export_at_{$date}.sql";

$db = \Yii::$app->get($this->db);
$opts = CliHelper::getMysqlOptsFromDsn($db);

Expand Down Expand Up @@ -451,4 +455,16 @@ private function getFilePrefix()
return 'm' . gmdate('ymd_His') . '_' . \Yii::$app->id . $sanitizedVersion;
}

/**
* @return mixed return the last applied migration id (m123456_123456)
*/
private function getLatestMigrationId(){
$command = new Command();
$command->setCommand('yii migrate/history 1');
$command->execute();
$output = $command->getOutput();
preg_match('/m[0-9_]{6}_[0-9_]{6}/', $output, $matches);
$latestMigrationId = $matches[0];
return $latestMigrationId;
}
}

0 comments on commit febb1ea

Please sign in to comment.