Giter Site home page Giter Site logo

alwex / php-database-migration Goto Github PK

View Code? Open in Web Editor NEW
85.0 6.0 33.0 167 KB

Database Migration tool for PHP

Home Page: https://alwex.github.io/php-database-migration/

PHP 96.94% Smarty 1.73% Shell 0.39% Dockerfile 0.94%
database-migrations php-database-migration php

php-database-migration's Introduction

PhpDbMigration - full PHP database migration tool

Build Status Packagist Version

SensioLabsInsight

This is a full standalone PHP tool based on Symfony Console and inspired by the Rails database migration tool and MyBatis. It merges the functionality of the two tools and has been designed to be as flexible as possible.

Usage

$ ./bin/migrate
Console Tool

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  help            Displays help for a command
  list            Lists commands
 migrate
  migrate:addenv  Initialise an environment to work with php db migrate
  migrate:create  Create a SQL migration
  migrate:down    Rollback all waiting migration down to [to] option if precised
  migrate:init    Create the changelog table on your environment database
  migrate:status  Display the current status of the specified environment
  migrate:up      Execute all waiting migration up to [to] option if precised

Installing it to your project

Just add it to your composer.json (don't forget to specify your bin directory) Warning, all migrate commands must be executed on your root folder like bin/migrate migrate:command...

{
    "name": "jdoe/testproject",
    "authors": [
        {
            "name": "Jhon DOE",
            "email": "[email protected]"
        }
    ],
    "require": {
        "php-database-migration/php-database-migration" :"3.6.*"
    },
    "config": {
        "bin-dir": "bin"
    }
}

Adding an environment

The first thing to do before playing with SQL migrations is to add an environment, let's add the dev one.

$ ./bin/migrate migrate:addenv

You will be prompted to answer a series of questions about your environment, and then a config file will be saved in ./.php-database-migration/environments/[env].yml.

Initialization

Once the environment is added, you have to initialize it. This verifies that the database connection works, and creates a new database table for tracking the current database changes:

$ ./bin/migrate migrate:init [env]

Create a migration

It is time to create our first migration file.

$ ./bin/migrate migrate:create

Migrations file are like this

-- // add table users
-- Migration SQL that makes the change goes here.
create table users (id integer, name text);
-- @UNDO
-- SQL to undo the change goes here.
drop table users;

List migrations

View all available migrations and their status.

$ ./bin/migrate migrate:status [env]
+----------------+---------+------------------+--------------------+
| id             | version | applied at       | description        |
+----------------+---------+------------------+--------------------+
| 14679010838251 |         |                  | create table users |
+----------------+---------+------------------+--------------------+

Up and down

You can now up all the pending migrations. If you decide to down a migration, the last one will be downed alone to prevent mistakes. You will be asked to confirm the downgrade of your database before running the real SQL script.

$ ./bin/migrate migrate:up [env]

You can mark migrations as applied without executing SQL (e.g. if you switched from another migration system)

$ ./bin/migrate migrate:up [env] --changelog-only

For development purposes, it is also possible to up a single migration without taking care of the other ones:

$ ./bin/migrate migrate:up [env] --only=[migrationid]

or migrate to specific migration (it will run all migrations, including the specified migration)

$ ./bin/migrate migrate:up [env] --to=[migrationid]

Same thing for down:

$ ./bin/migrate migrate:down [env] --only=[migrationid]

or

$ ./bin/migrate migrate:down [env] --to=[migrationid]

php-database-migration's People

Contributors

alwex avatar br39 avatar compwright avatar pyldin601 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

php-database-migration's Issues

Status of project ?

Is this project anymore maintained ? I would like to with latest symfony but no luck due to old dependencies.

[Insight] Error silenced by the at sign (@) - in Migrate/Migration.php, line 180

in Migrate/Migration.php, line 180

Adding "@" before $content = file_get_contents($migrationDir . '/' . $this->getFile()) prevents warning and errors during this function execution from being displayed.
If you need to do that, you should probably implement a better way to qualify and recover from errors, using Exceptions.

        );
    }

    public function load($migrationDir)
    {
        @$content = file_get_contents($migrationDir . '/' . $this->getFile());
        if ($content && strpos($content, '@UNDO') > 0) {
            $sql = explode('-- @UNDO', $content);
            $this->setSqlUp($sql[0]);
            $this->setSqlDown($sql[1]);
        }

Posted from SensioLabsInsight

Changelog

The tool should have a Changelog.md

Environment variables in env files

Is it possible to use environment variables in env configurations? I populated database credentials into system env and would like to use it in migrations. Thanks!

Migrate Errors

Errors from failed SQL execution from migration files not displayed. The console shows 100% complete even when there was an error executing the SQL script at the database side. The changelog also changes regardless of problems in execution. This can lead to failed migrations seen as completed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.