-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Откат миграций отличных от мастера с помощью
git ls-tree
. Используе…
…м такой вариант при развертывании в k8s на стейдже.
- Loading branch information
Showing
9 changed files
with
118 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpCsFixer\Fixer\Operator\BinaryOperatorSpacesFixer; | ||
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTestAnnotationFixer; | ||
use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer; | ||
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
|
||
return static function (ContainerConfigurator $containerConfigurator): void { | ||
$containerConfigurator->import(__DIR__ . '/vendor/umbrellio/code-style-php/umbrellio-cs.php'); | ||
|
||
$services = $containerConfigurator->services(); | ||
|
||
$services->set(PhpUnitTestAnnotationFixer::class) | ||
->call('configure', [[ | ||
'style' => 'annotation', | ||
]]); | ||
|
||
$services->set(DeclareStrictTypesFixer::class); | ||
|
||
$services->set(BinaryOperatorSpacesFixer::class) | ||
->call('configure', [[ | ||
'default' => 'single_space', | ||
]]); | ||
|
||
$parameters = $containerConfigurator->parameters(); | ||
|
||
$parameters->set('cache_directory', '.ecs_cache'); | ||
|
||
$parameters->set('exclude_files', ['vendor/*', 'app/*']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Umbrellio\Deploy\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
use SplFileInfo; | ||
|
||
class RollbackNewMigrations extends Command | ||
{ | ||
private const COMMAND = 'git ls-tree --name-only origin/master database/migrations/'; | ||
|
||
protected $signature = 'rollback_new_migrations:rollback'; | ||
protected $description = 'Rollback new migrations (default way for k8s staging)'; | ||
|
||
public function handle(): void | ||
{ | ||
if (App::environment('production')) { | ||
$this->error('It`s restricted to rollback new migrations on production.'); | ||
return; | ||
} | ||
|
||
$migrationsFromMaster = collect(explode(PHP_EOL, shell_exec(self::COMMAND))) | ||
->filter() | ||
->map(fn (string $path) => new SplFileInfo(base_path($path))); | ||
$migrationsFromCurrent = collect(File::files(base_path('database/migrations'))); | ||
|
||
/** @var Collection|SplFileInfo[] $migrationsToRollback */ | ||
$migrationsToRollback = $migrationsFromCurrent->keyBy->getFileName() | ||
->diffKeys($migrationsFromMaster->keyBy->getFileName()) | ||
->sort(fn (SplFileInfo $file1, SplFileInfo $file2) => strcmp($file1->getFilename(), $file2->getFilename())) | ||
->reverse(); | ||
|
||
if ($migrationsToRollback->isEmpty()) { | ||
$this->info('There are no migrations to rollback.'); | ||
return; | ||
} | ||
|
||
DB::transaction(function () use ($migrationsToRollback) { | ||
foreach ($migrationsToRollback as $migration) { | ||
$this->info('Rolling back: ' . $migration->getFilename()); | ||
$this->downMigrationFile($migration); | ||
} | ||
}); | ||
} | ||
|
||
private function downMigrationFile(SplFileInfo $f): void | ||
{ | ||
require_once $f->getPathname(); | ||
$filename = explode('.php', $f->getRelativePathname())[0]; | ||
$class = Str::studly(implode('_', array_slice(explode('_', $filename), 4))); | ||
$migration = new $class(); | ||
if (method_exists($migration, 'down')) { | ||
$migration->down(); | ||
DB::table(config('database.migrations', 'migrations'))->where('migration', $filename)->delete(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters