Code Monkey home page Code Monkey logo

laravel-custom-migrations's Introduction

Laravel Custom Migrations

Documentation Status Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Dependency Status

Run separate Laravel migrations on multiple databases

This package provides a simple way of including different types of migrations over multiple databases. A common use case would be that you are running a main database and separate databases for each customer.

It is a bit painful to run each migration independently for each database connection. You are also not able to differ which migration is responsible for which type of migration.

Custom Migrations to the Aid!

Compatibility

Laravel Version Version
4.x 1.0.*
5.0 1.1.*
5.1 1.2.*
5.2 1.3.*

Installation

To install this package, simply put the following into your composer.json

For Laravel 5.x:

{
    "require": {
        "codengine/laravel-custom-migrations": "1.3.*"
    }
}

The displayed version is for Laravel 5.2, use the Compatibility table above for your Laravel 5 version

For Laravel 4.x:

{
    "require": {
        "codengine/laravel-custom-migrations": "1.0.*"
    }
}

After updating composer, replace the default Laravel Migration Service Provider

'Illuminate\Database\MigrationServiceProvider',

with

'Codengine\CustomMigrations\CustomMigrationsServiceProvider',

You should then update the laravel autoload files with

php artisan dump-autoload

Configuration

In your database.php file, you have to specify the type of the database which will later be used to decide which migrations apply for them. Here is an example:

'my_customer' => array(
	'driver'    => 'mysql',
	'host'      => 'localhost',
	'database'  => 'customer',
	'username'  => 'customer',
	'password'  => '',
	'charset'   => 'utf8',
	'collation' => 'utf8_unicode_ci',
	'prefix'    => '',
	'migration_type' => 'customer'
)		

The important part is "migration_type". It does not have to be unique.

Usage

Each migration file needs to be type-hinted as we need to differ between default migrations and custom ones.

You have to specify the type of the migration in your migration class like this:

class MyMigration {
    public $type = "customer";
    
    public function up(){
    ....
}

It has to be public and should be the same as in your database configuration. If a $type is not specified, the migration will only be used for the default database.

Running Custom Migrations

As this package only extends the Laravel commands, every command like "migrate", "rollback", "reset" and "refresh" works like it did before.

There is just one difference: Each command got a new option: --type=xyz. So, in order to run the "customer" migrations for each customer-related database you just have to execute the command like this:

php artisan migrate --type=customer
php artisan migrate:rollback --type=customer
php artisan migrate:refresh --type=customer
php artisan migrate:reset --type=customer

It will iterate over each customer database connection and run the applicable migrations.

You can also limit the migration to just one database using the --database option like this

php artisan migrate --type=customer --database=mySpecialCustomer

License

The Laravel Custom Migrations Package is open-sourced software licensed under the MIT license

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.