Skip to content

Commit 96a10fd

Browse files
committed
Initial commit of the package
0 parents  commit 96a10fd

12 files changed

+442
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nbproject
2+
.AppleDouble

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 José Lorente Martín
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Data Migrations for Laravel
2+
===========================
3+
This extension allows you to separate data migrations from structure migrations.
4+
5+
Installation
6+
------------
7+
8+
The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
9+
10+
With Composer installed, you can then install the extension using the following commands:
11+
12+
```bash
13+
$ php composer.phar require jlorente/laravel-data-migrations "*"
14+
```
15+
16+
or add
17+
18+
```json
19+
...
20+
"require": {
21+
// ... other configurations ...
22+
"jlorente/laravel-data-migrations": "*"
23+
}
24+
```
25+
26+
to the ```require``` section of your `composer.json` file.
27+
28+
### Configuration
29+
30+
1. Register the ServiceProvider in your config/app.php service provider list. This step can be skipped in Laravel 5.5+
31+
32+
```php
33+
\Jlorente\DataMigrations\DataMigrationsServiceProvider::class
34+
```
35+
36+
2. Publish the new assets.
37+
```shell
38+
php artisan vendor:publish --tag=data-migrations
39+
```
40+
41+
3. Run the database migration in order to create the data migrations table.
42+
```shell
43+
php artisan migrate
44+
```
45+
46+
### Usage
47+
48+
You can use this package the same way as the regular migrations package. The data migrations will be stored in a different folder.
49+
50+
`make:data-migration` creates a new data migration.
51+
52+
`migrate:data` runs the data migration
53+
54+
`migrate:rollback-data` rolls back the migration.
55+
56+
## License
57+
Copyright &copy; 2018 José Lorente Martín <[email protected]>.
58+
59+
Licensed under the MIT license. See LICENSE.txt for details.

assets/config/data-migrations.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* @author José Lorente <[email protected]>
5+
* @license The MIT License (MIT)
6+
* @copyright José Lorente
7+
* @version 1.0
8+
*/
9+
return [
10+
// The name of the table where data migrations are stored.
11+
'migrations_data' => 'migrations_data',
12+
// Flag if a migration rollback should delete the data_migrations table.
13+
'rollback_table' => false,
14+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* @author José Lorente <[email protected]>
5+
* @license The MIT License (MIT)
6+
* @copyright José Lorente
7+
* @version 1.0
8+
*/
9+
use Illuminate\Support\Facades\Schema;
10+
use Illuminate\Database\Schema\Blueprint;
11+
use Illuminate\Database\Migrations\Migration;
12+
13+
class CreateDataMigrationTable extends Migration
14+
{
15+
16+
/**
17+
* Run the migrations.
18+
*
19+
* @return void
20+
*/
21+
public function up()
22+
{
23+
if (!Schema::hasTable('data_migrations')) {
24+
25+
Schema::create('data_migrations', function (Blueprint $table) {
26+
$table->string('migration');
27+
$table->integer('batch');
28+
});
29+
}
30+
}
31+
32+
/**
33+
* Reverse the migrations.
34+
*
35+
* @return void
36+
*/
37+
public function down()
38+
{
39+
if (config('data-migrations.rollback_table')) {
40+
Schema::dropIfExists('data_migrations');
41+
}
42+
}
43+
44+
}

composer.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "jlorente/laravel-data-migrations",
3+
"description": "An extension to allow you to separate data migrations from structure migrations",
4+
"license": "MIT",
5+
"authors": [{
6+
"name": "Jose Lorente",
7+
"email": "[email protected]"
8+
}],
9+
"require": {},
10+
"autoload": {
11+
"psr-4": {
12+
"Jlorente\\DataMigrations\\": "src/"
13+
}
14+
},
15+
"extra": {
16+
"laravel": {
17+
"providers": [
18+
"Jlorente\\DataMigrations\\DataMigrationsServiceProvider"
19+
]
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* @author José Lorente <[email protected]>
5+
* @license The MIT License (MIT)
6+
* @copyright José Lorente
7+
* @version 1.0
8+
*/
9+
10+
namespace Jlorente\DataMigrations\Console\Commands;
11+
12+
use Illuminate\Database\Console\Migrations\MigrateMakeCommand;
13+
14+
/**
15+
* MakeMigrateDataCommand class.
16+
*
17+
* @author José Lorente <[email protected]>
18+
*/
19+
class MakeMigrateDataCommand extends MigrateMakeCommand
20+
{
21+
22+
use DataMigrationCommandTrait;
23+
24+
protected $signature = 'make:data-migration {name : The name of the migration.}
25+
{--create= : The table to be created.}
26+
{--table= : The table to migrate.}
27+
{--path= : The location where the migration file should be created.}';
28+
protected $description = 'Create a new data migration file';
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* @author José Lorente <[email protected]>
5+
* @license The MIT License (MIT)
6+
* @copyright José Lorente
7+
* @version 1.0
8+
*/
9+
namespace Jlorente\DataMigrations\Console\Commands;
10+
11+
use Illuminate\Database\Console\Migrations\MigrateCommand;
12+
use Illuminate\Database\Migrations\Migrator;
13+
use Jlorente\DataMigrations\Console\Traits\DataMigrationCommandTrait;
14+
15+
/**
16+
* MigrateDataCommand class.
17+
*
18+
* @author José Lorente <[email protected]>
19+
*/
20+
class MigrateDataCommand extends MigrateCommand
21+
{
22+
23+
use DataMigrationCommandTrait;
24+
25+
/**
26+
* The name and signature of the console command.
27+
*
28+
* @var string
29+
*/
30+
protected $signature = 'migrate {--database= : The database connection to use.}
31+
{--force : Force the operation to run when in production.}
32+
{--path= : The path of migrations files to be executed.}
33+
{--pretend : Dump the SQL queries that would be run.}
34+
{--seed : Indicates if the seed task should be re-run.}
35+
{--step : Force the migrations to be run so they can be rolled back individually.}';
36+
37+
/**
38+
* The console command description.
39+
*
40+
* @var string
41+
*/
42+
protected $description = 'Run the data migrations';
43+
44+
/**
45+
* Create a new data migration command instance.
46+
*
47+
* @param \Illuminate\Database\Migrations\Migrator $migrator
48+
* @return void
49+
*/
50+
public function __construct(Migrator $migrator)
51+
{
52+
parent::__construct($migrator);
53+
$this->migrator = \App::make('migrator.data');
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* @author José Lorente <[email protected]>
5+
* @license The MIT License (MIT)
6+
* @copyright José Lorente
7+
* @version 1.0
8+
*/
9+
10+
namespace Jlorente\DataMigrations\Console\Commands;
11+
12+
use Illuminate\Database\Console\Migrations\RollbackCommand;
13+
use Illuminate\Database\Migrations\Migrator;
14+
use Jlorente\DataMigrations\Console\Traits\DataMigrationCommandTrait;
15+
16+
/**
17+
* RollbackDataCommand class.
18+
*
19+
* @author José Lorente <[email protected]>
20+
*/
21+
class RollbackDataCommand extends RollbackCommand
22+
{
23+
24+
use DataMigrationCommandTrait;
25+
26+
/**
27+
* The name and signature of the console command.
28+
*
29+
* @var string
30+
*/
31+
protected $signature = 'migrate:rollback-data';
32+
33+
/**
34+
* The console command description.
35+
*
36+
* @var string
37+
*/
38+
protected $description = 'Rollback the last database data migration';
39+
40+
/**
41+
* Create a new data migration rollback command instance.
42+
*
43+
* @param \Illuminate\Database\Migrations\Migrator $migrator
44+
* @return void
45+
*/
46+
public function __construct(Migrator $migrator)
47+
{
48+
parent::__construct($migrator);
49+
$this->migrator = \App::make('migrator.data');
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* @author José Lorente <[email protected]>
5+
* @license The MIT License (MIT)
6+
* @copyright José Lorente
7+
* @version 1.0
8+
*/
9+
namespace Jlorente\DataMigrations\Console\Traits;
10+
11+
/**
12+
* DataMigrationCommandTrait trait.
13+
*
14+
* @author José Lorente <[email protected]>
15+
*/
16+
trait DataMigrationCommandTrait
17+
{
18+
19+
/**
20+
* Get the path to the migration directory.
21+
*
22+
* @return string
23+
*/
24+
protected function getMigrationPath()
25+
{
26+
return $this->laravel->databasePath() . DIRECTORY_SEPARATOR . 'data_migrations';
27+
}
28+
29+
}

0 commit comments

Comments
 (0)