Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
Команда для архивации (#7)
Browse files Browse the repository at this point in the history
* поддержка поддиректорий

* fix

* команда для архивации

* try fix
  • Loading branch information
MsNatali authored and arrilot committed Jun 7, 2018
1 parent a49283e commit 95f5a38
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 4 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@
</tr>
<tr>
<td>`php migrator make название_миграции`</td>
<td>Создает файл миграции
Опции:<br>
`-d foo/bar` - указать поддиректорию, в которой будет создана миграция<br>
<td>
Создает файл миграции
Опции:<br>
`-d foo/bar` - указать поддиректорию, в которой будет создана миграция<br>
</td>
</tr>
<tr>
Expand All @@ -87,6 +88,14 @@
<td>`php migrator status`</td>
<td>Показывает доступные для выполнения миграции, а также последние выполненные.</td>
</tr>
<tr>
<td>`php migrator archive`</td>
<td>
Переносит все миграции в архив. По умолчанию это директория archive, но можно переопределить в конфиге, указав "dir_archive"<br>
Опции:<br>
`-w 10` - не переносить в архив последние N миграций<br>
</td>
</tr>
</table>

### Шаблоны миграций
Expand Down
3 changes: 3 additions & 0 deletions migrator
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env php
<?php

use Arrilot\BitrixMigrations\Commands\ArchiveCommand;
use Arrilot\BitrixMigrations\Commands\MakeCommand;
use Arrilot\BitrixMigrations\Commands\InstallCommand;
use Arrilot\BitrixMigrations\Commands\MigrateCommand;
Expand All @@ -22,6 +23,7 @@ CModule::IncludeModule("iblock");
$config = [
'table' => 'migrations',
'dir' => './migrations',
// 'dir_archive' => 'archive', // not required. default = "archive"
];

$database = new BitrixDatabaseStorage($config['table']);
Expand All @@ -37,4 +39,5 @@ $app->add(new MigrateCommand($migrator));
$app->add(new RollbackCommand($migrator));
$app->add(new TemplatesCommand($templates));
$app->add(new StatusCommand($migrator));
$app->add(new ArchiveCommand($migrator));
$app->run();
60 changes: 60 additions & 0 deletions src/Commands/ArchiveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Arrilot\BitrixMigrations\Commands;

use Arrilot\BitrixMigrations\Migrator;
use Symfony\Component\Console\Input\InputOption;

class ArchiveCommand extends AbstractCommand
{
/**
* Migrator instance.
*
* @var Migrator
*/
protected $migrator;

/**
* Constructor.
*
* @param Migrator $migrator
*/
public function __construct(Migrator $migrator)
{
$this->migrator = $migrator;

parent::__construct();
}

/**
* Configures the current command.
*/
protected function configure()
{
$this->setName('archive')
->setDescription('Move migration into archive')
->addOption('without', 'w', InputOption::VALUE_REQUIRED, 'Archive without last N migration');
}

/**
* Execute the console command.
*
* @return null|int
*/
protected function fire()
{
$files = $this->migrator->getAllMigrations();
$without = $this->input->getOption('without') ?: 0;
if ($without > 0) {
$files = array_slice($files, 0, $without * -1);
}

$count = $this->migrator->moveMigrationFiles($files);

if ($count) {
$this->message("<info>Moved to archive:</info> {$count}");
} else {
$this->info('Nothing to move');
}
}
}
53 changes: 52 additions & 1 deletion src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class Migrator
*/
protected $dir;

/**
* Directory to store archive m.
*
* @var string
*/
protected $dir_archive;

/**
* Files interactions.
*
Expand Down Expand Up @@ -60,6 +67,7 @@ public function __construct($config, TemplatesCollection $templates, DatabaseSto
{
$this->config = $config;
$this->dir = $config['dir'];
$this->dir_archive = isset($config['dir_archive']) ? $config['dir_archive'] : 'archive';

$this->templates = $templates;
$this->database = $database ?: new BitrixDatabaseStorage($config['table']);
Expand Down Expand Up @@ -162,6 +170,16 @@ public function getRanMigrations()
return $this->database->getRanMigrations();
}

/**
* Get all migrations.
*
* @return array
*/
public function getAllMigrations()
{
return $this->files->getMigrationFiles($this->dir);
}

/**
* Determine whether migration file for migration exists.
*
Expand Down Expand Up @@ -225,13 +243,46 @@ public function deleteMigrationFile($migration)
*/
public function getMigrationsToRun()
{
$allMigrations = $this->files->getMigrationFiles($this->dir);
$allMigrations = $this->getAllMigrations();

$ranMigrations = $this->getRanMigrations();

return array_diff($allMigrations, $ranMigrations);
}

/**
* Move migration files.
*
* @param array $files
* @param string $toDir
*
* @return int
*/
public function moveMigrationFiles($files = [], $toDir = '')
{
$toDir = trim($toDir ?: $this->dir_archive, '/');
$files = $files ?: $this->getAllMigrations();
$this->files->createDirIfItDoesNotExist("$this->dir/$toDir");

$count = 0;
foreach ($files as $migration) {
$from = $this->getMigrationFilePath($migration);
$to = "$this->dir/$toDir/$migration.php";

if ($from == $to) {
continue;
}

$flag = $this->files->move($from, $to);

if ($flag) {
$count++;
}
}

return $count;
}

/**
* Construct migration file name from migration name and current time.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Storages/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,17 @@ public function delete($path)
{
return $this->exists($path) ? unlink($path) : false;
}

/**
* Move file.
*
* @param string $path_from
* @param string $path_to
*
* @return bool
*/
public function move($path_from, $path_to)
{
return $this->exists($path_from) ? rename($path_from, $path_to) : false;
}
}

0 comments on commit 95f5a38

Please sign in to comment.