Giter Site home page Giter Site logo

trek's Introduction

Trek

Trek is a simple migration and versioning library. It abstracts the version tracking and migration between versions allowing you to focus on writing clean migration scrpts based on a simple interface.

Directory Structure

Trek assumes a conventional directory structure:

<namespace>/<major>.<minor>.<patch>/<number>_<classname>.php

<namespace>

By default it is set to Migration but you can change it to whatever you want as long as it is a valid namespace.

<major>.<minor>.<patch>

In order to map the version directory to part of the migration class namespace, it is transformed so that each number is mapped to its English word and each period is replaced by a namespace separator.

<number>

The number part of the file name allows you to simply define which order you want your migrations to be run in. It is removed from the actual class name during resolution.

<classname>

This part of the file name maps directly to the class name within the resolved namespace.

Writing Migrations

The following is a migration for Migration/1.0.10/1_AddUserTable.php:

<?php

namespace Migration\One\Zero\OneZero;
use Trek\MigrationInterface;

class AddUserTable implements MigrationInterface
{
    public function up()
    {
        // upgrade code here...
    }
    
    public function down()
    {
        // downgrade code here...
    }
}

It is up to you how you write your code in your methods; there are no dependencies and you can use whatever libraries you want.

Migrating

Migration is done by using the Trek\Migrator class.

<?php

use Trek\Migrator;

$migrator = new Migrator('path/to/migrations', 'Migration\Namespace');

The migrator requires you pass it a directory to load the migrations from and an optional, preferred namespace to use instead of the default.

Moving between versions is quite simple. If you want to update to the latest version no matter what version you are currently on:

$migrator->up();

If you want to move to a specific version:

$migrator->to('1.0.0');

If you want to rollback because of an error:

try {
    $migrator->up();
} catch (\Exception $e) {
    $migrator->rollback();
}

If errors occur during the rollback process, you will need to handle those manually.

Running Tests

To run the tests from the working copy root:

php bin/tests.php

trek's People

Contributors

deceze avatar treshugart avatar

Watchers

 avatar  avatar

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.