Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
initial commit to transfer to artesãos
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Hernandes committed Jun 2, 2016
0 parents commit a51ec07
Show file tree
Hide file tree
Showing 22 changed files with 2,080 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
24 changes: 24 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Migrator is based on the original laravel/framework migrator system.
Here is a copy of the original license.
----------------------------------------------------------------------------
The MIT License (MIT)

Copyright (c) <Taylor Otwell>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "artesaos/migrator",
"description": "Namespaced Migrations for Laravel 5.1+",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Diego Hernandes",
"email": "diego@hernandev.com"
}
],
"minimum-stability": "stable",
"require": {},
"autoload": {
"psr-4": {
"Migrator\\": "src/"
}
}
}
129 changes: 129 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# artesaos/migrator

[![Latest Stable Version](https://poser.pugx.org/artesaos/migrator/v/stable)](https://packagist.org/packages/artesaos/migrator) [![Total Downloads](https://poser.pugx.org/artesaos/migrator/downloads)](https://packagist.org/packages/artesaos/migrator) [![Monthly Downloads](https://poser.pugx.org/artesaos/migrator/d/monthly)](https://packagist.org/packages/artesaos/migrator) [![License](https://poser.pugx.org/artesaos/migrator/license)](https://packagist.org/packages/artesaos/migrator)

This package is a customized version of Laravel's default database migrator, it was designed to register migrations on services providers and support namespacing as well.

There is no timestamp previews since the run order is based on how you register the migrations.

### Warning
This Package Supports Laravel 5.1 and 5.2, other versions may need some adjusts in order to work and are not recommended.

### Installing

In order to install Migrator, run the following command into your Laravel 5.2+ project:

> composer require artesaos/migrator
After installing the Package, you can now register it's provider into your config/app.php file:

```php
'providers' => [
// other providers omitted.
Migrator\MigrationServiceProvider::class,
]
```

### Usage

As the default Laravel migrator, this one has all the original commands, to list the available options, you can see all the available options using `php artisan` command.

```
migrator Run the database migrations
migrator:install Create the migration repository
migrator:make Create a new migration file
migrator:refresh Reset and re-run all migrations
migrator:reset Rollback all database migrations
migrator:rollback Rollback the last database migration
migrator:status Show the status of each migration
```

#### Creating Migrations

In order to generate an empty migration, please provide the migrator with the full qualified class name, as the example.

`php artisan migrator:make 'MyApp\MyModule\Database\Migrations\CreateOrdersTable' --create=orders`

This will create a migration class into the right directory, the resulting file is slightly different from the default Laravel generated:

```php
<?php

namespace MyApp\MyModule\Database\Migrations;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrdersTable extends Migration
{
/**
* @var \Illuminate\Database\Schema\Builder
*/
protected $schema;

/**
* Migration constructor.
*/
public function __construct()
{
$this->schema = app('db')->connection()->getSchemaBuilder();
}

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->schema->create('orders', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$this->schema->drop('orders');
}
}
```

To declare your table fields, just follow the usual schema build practices, this package don't make anything different there.

As the normal migrator, you can pass the option `--table` instead of `--create` in order to generate a update migration instead of a create one. Also, you can create a empty migration not passing any of those options.

#### Registering migrations.

Inside any service provider of your choice (usually on the same namespace that you're storing the migrations), you easily register the migrations using the *`Migrator\MigratorTrait`*:

```php
<?php

namespace MyApp\MyModule\Providers;

use Illuminate\Support\ServiceProvider;
use Migrator\MigratorTrait;
use MyApp\MyModule\Database\Migrations\CreateOrdersTable;
use MyApp\MyModule\Database\Migrations\CreateProductsTable;

class MyModuleServiceProvider extends ServiceProvider
{
use MigratorTrait;

public function register()
{
$this->migrations([
CreateOrdersTable::class,
CreateProductsTable::class,
]);
}
}
```


10 changes: 10 additions & 0 deletions src/Console/BaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Migrator\Console;

use Illuminate\Console\Command;

class BaseCommand extends Command
{
// Empty Base Command.
}
68 changes: 68 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Migrator\Console;

use Symfony\Component\Console\Input\InputOption;
use Migrator\MigrationRepositoryInterface;

class InstallCommand extends BaseCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'migrator:install';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create the migration repository';

/**
* The repository instance.
*
* @var MigrationRepositoryInterface
*/
protected $repository;

/**
* Create a new migration install command instance.
*
* @param \Migrator\MigrationRepositoryInterface $repository
*/
public function __construct(MigrationRepositoryInterface $repository)
{
parent::__construct();

$this->repository = $repository;
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$this->repository->setSource($this->input->getOption('database'));

$this->repository->createRepository();

$this->info('Migration table created successfully.');
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],
];
}
}
119 changes: 119 additions & 0 deletions src/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Migrator\Console;

use Illuminate\Console\ConfirmableTrait;
use Migrator\Migrator;
use Symfony\Component\Console\Input\InputOption;

class MigrateCommand extends BaseCommand
{
use ConfirmableTrait;

/**
* The console command name.
*
* @var string
*/
protected $name = 'migrator';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Run the database migrations';

/**
* The migrator instance.
*
* @var \Illuminate\Database\Migrations\Migrator
*/
protected $migrator;

/**
* Create a new migration command instance.
*
* @param \Migrator\Migrator $migrator
*/
public function __construct(Migrator $migrator)
{
parent::__construct();

$this->migrator = $migrator;
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
if (! $this->confirmToProceed()) {
return;
}

$this->prepareDatabase();

// The pretend option can be used for "simulating" the migration and grabbing
// the SQL queries that would fire if the migration were to be run against
// a database for real, which is helpful for double checking migrations.
$pretend = $this->input->getOption('pretend');

$this->migrator->run([
'pretend' => $pretend,
'step' => $this->input->getOption('step'),
]);

// Once the migrator has run we will grab the note output and send it out to
// the console screen, since the migrator itself functions without having
// any instances of the OutputInterface contract passed into the class.
foreach ($this->migrator->getNotes() as $note) {
$this->output->writeln($note);
}

// Finally, if the "seed" option has been given, we will re-run the database
// seed task to re-populate the database, which is convenient when adding
// a migration and a seed at the same time, as it is only this command.
if ($this->input->getOption('seed')) {
$this->call('db:seed', ['--force' => true]);
}
}

/**
* Prepare the migration database for running.
*
* @return void
*/
protected function prepareDatabase()
{
$this->migrator->setConnection($this->input->getOption('database'));

if (! $this->migrator->repositoryExists()) {
$options = ['--database' => $this->input->getOption('database')];

$this->call('migrator:install', $options);
}
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to use.'],

['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],

['pretend', null, InputOption::VALUE_NONE, 'Dump the SQL queries that would be run.'],

['seed', null, InputOption::VALUE_NONE, 'Indicates if the seed task should be re-run.'],

['step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually.'],
];
}
}
Loading

0 comments on commit a51ec07

Please sign in to comment.