Skip to content

Commit

Permalink
Откат миграций отличных от мастера с помощью git ls-tree. Используе…
Browse files Browse the repository at this point in the history
…м такой вариант при развертывании в k8s на стейдже.
  • Loading branch information
lazeevv committed Sep 23, 2021
1 parent 5544937 commit a30441a
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 25 deletions.
14 changes: 10 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
- name: 'Setup problem matchers for PHPUnit'
run: 'echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"'
- name: 'Install PHP dependencies with Composer'
run: composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader
run: COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader
working-directory: './'
- name: 'Linting PHP source files'
run: 'composer lint'
Expand All @@ -80,8 +80,14 @@ jobs:
operating_system: [ubuntu-latest]
postgres: ['9.6']
laravel: ['^6.0']
php_versions: ['7.3']
php_versions: ['7.4']
include:
- operating_system: ubuntu-latest
postgres: '9.6'
php_versions: '7.3'
laravel: '^6.0'
experimental: true
coverage: false
- operating_system: ubuntu-latest
postgres: '10'
php_versions: '7.4'
Expand Down Expand Up @@ -154,7 +160,7 @@ jobs:
- name: 'Install PHP dependencies with Composer'
run: |
composer require "laravel/framework=${{ matrix.laravel }}" --no-update
composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader
COMPOSER_MEMORY_LIMIT=-1 composer install --prefer-dist --no-progress --no-suggest --optimize-autoloader
working-directory: './'
- name: 'Run Unit Tests with PHPUnit'
continue-on-error: ${{ matrix.experimental }}
Expand All @@ -176,7 +182,7 @@ jobs:
cp composer.json release/composer.json
cp -rf database/new_migrations release/database/migrations
cp .env release/.env
cd release && composer update
cd release && COMPOSER_MEMORY_LIMIT=-1 composer update
cd ../../
sed -e "s/\${USERNAME}/${{ env.DB_USER }}/" \
-e "s/\${PASSWORD}/${{ env.DB_PASSWORD }}/" \
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ php artisan rollback_missing_migrations:rollback /projects/old_release/your_app/
--realpath
```

In case if you need rollback new migrations different from origin/master, you can use `rollback_new_migrations:rollback`

## Authors

Created by Art4es & Korben Dallas.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"squizlabs/php_codesniffer": "^3.5"
},
"scripts": {
"lint": "vendor/bin/ecs check src --config=ecs.yml",
"lint-fix": "vendor/bin/ecs check src --fix --config=ecs.yml"
"lint": "vendor/bin/ecs check src --config=ecs.php",
"lint-fix": "vendor/bin/ecs check src --fix --config=ecs.php"
},
"extra": {
"laravel": {
Expand Down
32 changes: 32 additions & 0 deletions ecs.php
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/*']);
};
15 changes: 0 additions & 15 deletions ecs.yml

This file was deleted.

3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<include>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/Console/RollbackNewMigrations.php</file>
</exclude>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
Expand Down
64 changes: 64 additions & 0 deletions src/Console/RollbackNewMigrations.php
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();
}
}
}
3 changes: 2 additions & 1 deletion src/RollbackMissingMigrationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use Illuminate\Support\ServiceProvider;
use Umbrellio\Deploy\Console\RollbackMissingMigrations;
use Umbrellio\Deploy\Console\RollbackNewMigrations;

class RollbackMissingMigrationServiceProvider extends ServiceProvider
{
public function register()
{
if ($this->app->runningInConsole()) {
$this->commands([RollbackMissingMigrations::class]);
$this->commands([RollbackMissingMigrations::class, RollbackNewMigrations::class]);
}
}
}
6 changes: 3 additions & 3 deletions tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sed -e "s/\${USERNAME}/postgres/" \
-e "s/\${HOST}/127.0.0.1/" \
.env.sample > .env
sed -e "s/\${LARAVEL_VERSION}/^8.0/" \
-e "s/\${PHP_VERSION}/8.0/" \
-e "s/\${PHP_VERSION}/7.4.16/" \
composer.json.sample > composer.json
rm -rf release
composer create-project laravel/laravel release
Expand All @@ -21,8 +21,8 @@ rm -rf release/tests
cp composer.json release/composer.json
cp -rf database/new_migrations release/database/migrations
cp .env release/.env
cd release && composer update
cd ../../ && composer update
cd release && COMPOSER_MEMORY_LIMIT=-1 composer update
cd ../../ && COMPOSER_MEMORY_LIMIT=-1 composer update
composer lint-fix
sed -e "s/\${USERNAME}/postgres/" \
-e "s/\${PASSWORD}//" \
Expand Down

0 comments on commit a30441a

Please sign in to comment.