From schema to migration

A common situation that occurs during the development process is that a schema is created, and then, we need to create a migration from that schema. At the time of writing, there is no official tool to do this in the Laravel core, but there are several packages available.

One such package is the migrations-generator package.

First, add the following line to the require-dev section of the composer.json file to require the migrations-generator dependency in the composer.json file:

"require-dev": {
    "phpunit/phpunit": "~4.0",
    "phpspec/phpspec": "~2.1",
    "xethron/migrations-generator": "dev-feature/laravel-five-stable",
    "way/generators": "dev-feature/laravel-five-stable"
  },

It is also necessary to add the following text to the composer.json file at the root level:

"repositories": [
  {
    "type": "git",
    "url": "[email protected]:jamisonvalenta/Laravel-4-Generators.git"
  }],

Composer's require-dev command

The require-dev command, as opposed to require, is a mechanism of a composer that allows certain packages that are needed only in the development phase. Most testing tools and migration tools will only be used in the local development machine, QA machine, and/or in a continuous integration environment, but not in the production environment. This mechanism keeps your production installation free of unnecessary packages.

Laravel's providers array

Laravel's providers array in the config/app.php file lists the providers that are available to Laravel at all times.

We will add both the way generator and the Xethron migration service providers:

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
          IlluminateFoundationProvidersArtisanServiceProvider::class,
          IlluminateAuthAuthServiceProvider::class,
          IlluminateBroadcastingBroadcastServiceProvider::class,
        ...
    'WayGeneratorsGeneratorsServiceProvider',
    'XethronMigrationsGeneratorMigrationsGeneratorServiceProvider'
]

The composer update command

The composer update command is a simple, yet powerful way to make sure that everything that needs to be in place is actually working and free of errors. After running this command, we're now ready to run the migrations.

Generating the migrations

Simply type the following command:

$ php artisan

The artisan command will display a list of all of the possible commands. The migrate:generate command should be included on the list of valid commands. If this command is not on the list, then something is not configured correctly.

Once you confirm that the migrate:generate command exists in the list, simply run the following command:

$ php artisan migrate:generate

This will start the process.

In this example, we have used the MySQL database. By entering Y when prompted, the process will begin and the output should show one migration file created for each table in the database.

This is how your command prompt should appear at the end:

Using connection: mysql

Generating migrations for: accommodations, amenities, amenity_room, cities, countries, currencies, locations, rates, reservation_room, reservations, rooms, states, users
Do you want to log these migrations in the migrations table? [Y/n] Y
Migration table created successfully.
Next Batch Number is: 1. We recommend using Batch Number 0 so that it becomes the "first" migration [Default: 0] 
Setting up Tables and Index Migrations
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_accommodations_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_amenities_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_amenity_room_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_cities_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_countries_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_currencies_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_locations_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_rates_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_reservation_room_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_reservations_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_rooms_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_states_table.php
Created: /var/www/laravel.example/database/migrations/2015_02_07_170311_create_users_table.php

Finished!
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset