elao/command-migration

PHP lib to run commands, for example on deployment.

v0.2.3 2019-01-15 13:21 UTC

This package is auto-updated.

Last update: 2024-12-16 03:26:48 UTC


README

PHP library to run commands, for example on deployment.

Why

Sometimes, on deployment or when switching to a branch, we need to run some migration commands, for example reindex a database, calculate aggregation, remove whatever uploaded files...

Do not do it manually anymore by connecting to your server in ssh! This library allow you to declare in your feature git branch what command(s) need to be run once deployed.

Install

$ composer require elao/command-migration

Enable the bundle on Symfony <= 3.4 (app/AppKernel.php)

public function registerBundles()
{
    $bundles = array(
        // ...
        new Elao\CommandMigration\Bridge\Symfony\Bundle\CommandMigrationBundle(),
        // ...
    );
}

Enable the bundle on Symfony 4 (config/bundles.php)

return [
    // ...
    Elao\CommandMigration\Bridge\Symfony\Bundle\CommandMigrationBundle::class => ['all' => true],
    // ...
];

Add a elao_command_migration.yaml file (in a not public directory of course!):

elao_command_migration:
    storage:
        type: dbal
        dsn: '%env(DATABASE_DSN)%'
        table_name: 'command_migrations'
    migrations: []

Usage

Declare what command(s) need to be run in the migrations entry in your elao_command_migration.yaml file:

elao_command_migration:
    # ...
    migrations:
        whateverUniqueIdentifier:
            - php bin/console app:posts:reindex
        20180510173033:
            - php bin/console app:posts:reindex
            - php bin/console doctrine:migrations:migrate
        20180622110900:
            - php bin/console app:posts:reindex
            - node hello-world.js
            - php bin/console doctrine:schema:update --force
            - rm -rf public/uploads/lolcats
            - php bin/console app:recalculate:turnover

Entries in migrations could have whatever identifier, but we recommend to use a date + time format: YYYYMMDDHHMMSS

Run php bin/elao-command-migration path/to/elao_command_migration.yaml to test it.

Integration

Add php bin/elao-command-migration path/to/elao_command_migration.yaml to your deployment process.

Capifony

Set in deploy.rb:

    after :deploy, 'app_tasks:elao_command_migration'

    namespace :app_tasks do
      task :elao_command_migration do
        capifony_pretty_print "--> Run command migrations"
        invoke_command "php bin/elao-command-migration path/to/elao_command_migration.yaml", :via => run_method
        capifony_puts_ok
      end
    end

Ansible

With Manala/ansible-role-deploy, add in ansible/group_vars/deploy.yml:

    manala_deploy_tasks:
      - command: php bin/elao-command-migration path/to/elao_command_migration.yaml

or

    manala_deploy_post_tasks:
      - command: php bin/elao-command-migration path/to/elao_command_migration.yaml

How it works

CommandMigration is very inspired by Doctrine Migrations but for running commands.

The elao:command-migration:run command :

  • Fetch all migrations already ran from command_migrations database table
  • Get only migrations not already ran from elao_command_migration.migrations
  • Store migration identifier in command_migrations database table.

Clean old migrations

When the commands have been deployed and ran on production environment, you can (manually) delete the entries in elao_command_migration.migrations.

Who is using it?