gersonalves/migration

Package para criar migrations em seu BD

1.0.5 2021-09-21 00:04 UTC

This package is auto-updated.

Last update: 2024-10-22 17:57:35 UTC


README

To configure it you must add it to your composer.

composer require gersonalves/migration

The project has the example files for easy understanding.

Implementation

use migration\migration;
$myConnection = new PDO("mysql:host=localhost:3307;dbname=docker", 'docker', 'docker', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

first, import the necessary files and start your PDO connection.

then select the appropriate place in your code to call to run a migration. and Add:

$migration = migration::run($myConnection, [
    'table' => 'migracoes',
    'migrations_dir' => 'migrations',
    'onlyJSON' => true,
    'continueWithErrors' => true,
    'mailTo' => 'gersonalvesdev@gmail.com'
]);
$migration->getResponse();

The second parameter is the configuration, here are some important information.

  • table - This is the field to define the migration log table. Default 'migrations'
  • migrations_dir - This is the directory to save yours .sql files to migrate. Default is null.
  • onlyJSON - if is true, the response have a JSON header. If false the response is an Array. Default false.
  • continueWithErrors - If is true, when an error occurs, the other migrations will be persisted and it will return a list with successes and failures. if false, on error the interrupt the migration script and return a throw exception. Default false.

Response example

On JSON Success

  {
    "status": "success",
    "message": "Migração realizado com sucesso",
    "bag": [
      "YOUR SQL HERE"
    ]
  }

Informations

The following features have execution blocking: DROP, TRUNCATE, DELETE, UPDATE without WHERE.

  • In the next update, it will inform you which ones you want to allow.
  • For all errors, the "status" is "error" regardless of using JSON or not.
  • Only .sql files will be executed.

Public methods

  • static RUN() - This is the framework's main method, responsible for making the necessary validations and executing a migration.
  • getJSON() - This method will return the last response generated by the system, whether error or success. It is possible to be called only after the RUN method that will generate some response in JSON format.
  • getArray() - is similar to getJSON but is an array response.
  • getResponse() - It's the same as all the other two methods. But it will return according to the onlyJSON setting.