├── .gitignore ├── assets ├── database │ └── migrations_data │ │ └── .gitkeep └── config │ └── data-migrations.php ├── src ├── Repositories │ └── DatabaseDataMigrationRepository.php ├── Console │ ├── Commands │ │ ├── InstallCommand.php │ │ ├── RollbackDataCommand.php │ │ ├── MakeMigrateDataCommand.php │ │ └── MigrateDataCommand.php │ └── Traits │ │ └── DataMigrationCommandTrait.php └── DataMigrationsServiceProvider.php ├── composer.json ├── LICENSE.txt └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | nbproject 2 | .AppleDouble 3 | -------------------------------------------------------------------------------- /assets/database/migrations_data/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/Repositories/DatabaseDataMigrationRepository.php: -------------------------------------------------------------------------------- 1 | 5 | * @license The MIT License (MIT) 6 | * @copyright José Lorente 7 | * @version 1.0 8 | */ 9 | 10 | namespace Jlorente\DataMigrations\Repositories; 11 | 12 | use Illuminate\Database\Migrations\DatabaseMigrationRepository; 13 | 14 | /** 15 | * DatabaseDataMigrationRepository class. 16 | * 17 | * @author José Lorente 18 | */ 19 | class DatabaseDataMigrationRepository extends DatabaseMigrationRepository 20 | { 21 | 22 | } 23 | -------------------------------------------------------------------------------- /assets/config/data-migrations.php: -------------------------------------------------------------------------------- 1 | 5 | * @license The MIT License (MIT) 6 | * @copyright José Lorente 7 | * @version 1.0 8 | */ 9 | return [ 10 | /* 11 | |-------------------------------------------------------------------------- 12 | | Table 13 | |-------------------------------------------------------------------------- 14 | | 15 | | The name of the table where the data migrations will be registered. Be 16 | | careful to not using the same name for the table as the normal migrations. 17 | */ 18 | 'table' => 'migrations_data' 19 | ]; 20 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 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": "jose.lorente.martin@gmail.com" 8 | }], 9 | "require": { 10 | "php": ">=7.4.0", 11 | "illuminate/support": ">=8.0" 12 | }, 13 | "autoload": { 14 | "psr-4": { 15 | "Jlorente\\DataMigrations\\": "src/" 16 | } 17 | }, 18 | "extra": { 19 | "laravel": { 20 | "providers": [ 21 | "Jlorente\\DataMigrations\\DataMigrationsServiceProvider" 22 | ] 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Console/Commands/InstallCommand.php: -------------------------------------------------------------------------------- 1 | 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\InstallCommand as BaseInstallCommand; 13 | 14 | /** 15 | * InstallCommand class. 16 | * 17 | * @author José Lorente 18 | */ 19 | class InstallCommand extends BaseInstallCommand 20 | { 21 | 22 | /** 23 | * The console command name. 24 | * 25 | * @var string 26 | */ 27 | protected $name = 'migrate-data:install'; 28 | 29 | /** 30 | * The console command description. 31 | * 32 | * @var string 33 | */ 34 | protected $description = 'Create the data migration repository'; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/Console/Traits/DataMigrationCommandTrait.php: -------------------------------------------------------------------------------- 1 | 5 | * @license The MIT License (MIT) 6 | * @copyright José Lorente 7 | * @version 1.0 8 | */ 9 | 10 | namespace Jlorente\DataMigrations\Console\Traits; 11 | 12 | /** 13 | * DataMigrationCommandTrait trait. 14 | * 15 | * @author José Lorente 16 | */ 17 | trait DataMigrationCommandTrait 18 | { 19 | 20 | /** 21 | * Get the path to the migration directory. 22 | * 23 | * @return string 24 | */ 25 | protected function getMigrationPath() 26 | { 27 | if (is_string($targetPath = $this->input->getOption('path'))) { 28 | return $this->laravel->basePath() . '/' . $targetPath; 29 | } else { 30 | return $this->laravel->databasePath() . DIRECTORY_SEPARATOR . 'migrations_data'; 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/Console/Commands/RollbackDataCommand.php: -------------------------------------------------------------------------------- 1 | 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 Jlorente\DataMigrations\Console\Traits\DataMigrationCommandTrait; 14 | 15 | /** 16 | * RollbackDataCommand class. 17 | * 18 | * @author José Lorente 19 | */ 20 | class RollbackDataCommand extends RollbackCommand 21 | { 22 | 23 | use DataMigrationCommandTrait; 24 | 25 | /** 26 | * The name and signature of the console command. 27 | * 28 | * @var string 29 | */ 30 | protected $name = 'migrate-data:rollback'; 31 | 32 | /** 33 | * The console command description. 34 | * 35 | * @var string 36 | */ 37 | protected $description = 'Rollback the last database data migration'; 38 | 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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. -------------------------------------------------------------------------------- /src/Console/Commands/MakeMigrateDataCommand.php: -------------------------------------------------------------------------------- 1 | 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 | use Jlorente\DataMigrations\Console\Traits\DataMigrationCommandTrait; 14 | 15 | /** 16 | * MakeMigrateDataCommand class. 17 | * 18 | * @author José Lorente 19 | */ 20 | class MakeMigrateDataCommand extends MigrateMakeCommand 21 | { 22 | 23 | use DataMigrationCommandTrait; 24 | 25 | protected $signature = 'make:data-migration {name : The name of the migration.} 26 | {--create= : The table to be created.} 27 | {--table= : The table to migrate.} 28 | {--path= : The location where the migration file should be created.} 29 | {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} 30 | {--fullpath : Output the full path of the migration}'; 31 | protected $description = 'Create a new data migration file'; 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/Console/Commands/MigrateDataCommand.php: -------------------------------------------------------------------------------- 1 | 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\MigrateCommand; 13 | use Jlorente\DataMigrations\Console\Traits\DataMigrationCommandTrait; 14 | 15 | /** 16 | * MigrateDataCommand class. 17 | * 18 | * @author José Lorente 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-data {--database= : The database connection to use.} 31 | {--force : Force the operation to run when in production} 32 | {--path=* : The path(s) to the migrations files to be executed} 33 | {--realpath : Indicate any provided migration file paths are pre-resolved absolute paths} 34 | {--pretend : Dump the SQL queries that would be run} 35 | {--seed : Indicates if the seed task should be re-run} 36 | {--step : Force the migrations to be run so they can be rolled back individually} 37 | {--graceful : Return a successful exit code even if an error occurs}'; 38 | 39 | /** 40 | * The console command description. 41 | * 42 | * @var string 43 | */ 44 | protected $description = 'Run the data migrations'; 45 | 46 | /** 47 | * Prepare the migration database for running. 48 | * 49 | * @return void 50 | */ 51 | protected function prepareDatabase() 52 | { 53 | $this->migrator->setConnection($this->option('database')); 54 | 55 | if (!$this->migrator->repositoryExists()) { 56 | $this->call( 57 | 'migrate-data:install', ['--database' => $this->option('database')] 58 | ); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Data Migrations for Laravel 2 | =========================== 3 | This extension allows you to separate data migrations from structure migrations. 4 | 5 | ## Installation 6 | 7 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 8 | 9 | With Composer installed, you can then install the extension using the following commands: 10 | 11 | **NOTE:** Until laravel 7.x use version 1.x. From laravel 8.0 and later use 2.x 12 | 13 | ```bash 14 | $ php composer.phar require jlorente/laravel-data-migrations 15 | ``` 16 | 17 | or add 18 | 19 | ```json 20 | ... 21 | "require": { 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 | config/app.php 33 | ```php 34 | return [ 35 | //other stuff 36 | 'providers' => [ 37 | //other stuff 38 | \Jlorente\DataMigrations\DataMigrationsServiceProvider::class, 39 | ]; 40 | ]; 41 | ``` 42 | 43 | 2. Publish the new assets. 44 | ```shell 45 | php artisan vendor:publish --tag=data-migrations 46 | ``` 47 | This will create the default migrations directory and the config/data-migrations.php file. 48 | 49 | ## Usage 50 | 51 | By default, the table used to store the data migrations is "migrations_data" table. You 52 | can change the table on the config/data-migrations.php file. 53 | 54 | The data migrations will be stored in the migrations_data folder of the database path if no 55 | path is specified in the command execution. 56 | 57 | The available commands of the package are: 58 | 59 | *Create migration command* 60 | ```shell 61 | php artisan make:data-migration [name] [--path=] 62 | ``` 63 | The firts time you use it the data migrations table will be created. 64 | 65 | *Run migration command* 66 | ```shell 67 | php artisan migrate-data [--path=] 68 | ``` 69 | 70 | *Rollback migration command* 71 | ```shell 72 | php artisan migrate-data:rollback [--path=] 73 | ``` 74 | 75 | The behavior of the migrations is the same as the regular migrations. 76 | 77 | ## License 78 | Copyright © 2018 José Lorente Martín . 79 | 80 | Licensed under the MIT license. See LICENSE.txt for details. 81 | -------------------------------------------------------------------------------- /src/DataMigrationsServiceProvider.php: -------------------------------------------------------------------------------- 1 | 5 | * @license The MIT License (MIT) 6 | * @copyright José Lorente 7 | * @version 1.0 8 | */ 9 | 10 | namespace Jlorente\DataMigrations; 11 | 12 | use Illuminate\Contracts\Events\Dispatcher; 13 | use Illuminate\Support\ServiceProvider; 14 | use Illuminate\Database\Migrations\Migrator; 15 | use Jlorente\DataMigrations\Console\Commands\InstallCommand as MigrateInstallCommand; 16 | use Jlorente\DataMigrations\Console\Commands\MakeMigrateDataCommand; 17 | use Jlorente\DataMigrations\Console\Commands\MigrateDataCommand; 18 | use Jlorente\DataMigrations\Console\Commands\RollbackDataCommand; 19 | use Jlorente\DataMigrations\Repositories\DatabaseDataMigrationRepository; 20 | 21 | /** 22 | * DataMigrationsServiceProvider class. 23 | * 24 | * @author José Lorente 25 | */ 26 | class DataMigrationsServiceProvider extends ServiceProvider 27 | { 28 | 29 | /** 30 | * Indicates if loading of the provider is deferred. 31 | * 32 | * @var bool 33 | */ 34 | protected $defer = true; 35 | 36 | /** 37 | * The commands to be registered. 38 | * 39 | * @var array 40 | */ 41 | protected $provides = [ 42 | 'migrator.data' 43 | , 'migration.data.repository' 44 | ]; 45 | 46 | /** 47 | * The commands to be registered. 48 | * 49 | * @var array 50 | */ 51 | protected $commands = [ 52 | 'command.migrate-data' 53 | , 'command.migrate-data.install' 54 | , 'command.migrate-data.rollback' 55 | , 'command.migrate-data.make' 56 | ]; 57 | 58 | /** 59 | * Bootstrap the application services. 60 | * 61 | * @return void 62 | */ 63 | public function boot() 64 | { 65 | if ($this->app->runningInConsole()) { 66 | $this->registerConfig(); 67 | $this->registerFolder(); 68 | } 69 | } 70 | 71 | /** 72 | * Registers the config file for the package. 73 | */ 74 | protected function registerConfig() 75 | { 76 | $this->publishes([ 77 | __DIR__ . '/../assets/config/data-migrations.php' => config_path('data-migrations.php'), 78 | ], 'data-migrations'); 79 | } 80 | 81 | /** 82 | * Registers the default folder of where the data migrations will be created. 83 | */ 84 | protected function registerFolder() 85 | { 86 | $this->publishes([ 87 | __DIR__ . '/../assets/database/migrations_data' => database_path('migrations_data'), 88 | ], 'data-migrations'); 89 | } 90 | 91 | /** 92 | * Register the application services. 93 | * 94 | * @return void 95 | */ 96 | public function register() 97 | { 98 | $this->bindRepository(); 99 | $this->bindMigrator(); 100 | $this->bindArtisanCommands(); 101 | 102 | $this->commands($this->commands); 103 | } 104 | 105 | /** 106 | * Binds the repository used by the data migrations. 107 | */ 108 | protected function bindRepository() 109 | { 110 | $this->app->singleton('migration.data.repository', function ($app) { 111 | $table = config('data-migrations.table'); 112 | 113 | return new DatabaseDataMigrationRepository($app['db'], $table); 114 | }); 115 | } 116 | 117 | /** 118 | * Binds the migrator used by the data migrations. 119 | */ 120 | protected function bindMigrator() 121 | { 122 | $this->app->singleton('migrator.data', function($app) { 123 | $repository = $app['migration.data.repository']; 124 | 125 | return new Migrator($repository, $app['db'], $app['files']); 126 | }); 127 | } 128 | 129 | /** 130 | * Binds the commands to execute the data migrations. 131 | */ 132 | protected function bindArtisanCommands() 133 | { 134 | $this->app->singleton('command.migrate-data', function ($app) { 135 | return new MigrateDataCommand($app['migrator.data'], $app[Dispatcher::class]); 136 | }); 137 | $this->app->singleton('command.migrate-data.install', function ($app) { 138 | return new MigrateInstallCommand($app['migration.data.repository']); 139 | }); 140 | $this->app->singleton('command.migrate-data.rollback', function ($app) { 141 | return new RollbackDataCommand($app['migrator.data']); 142 | }); 143 | $this->app->singleton('command.migrate-data.make', function ($app) { 144 | // Once we have the migration creator registered, we will create the command 145 | // and inject the creator. The creator is responsible for the actual file 146 | // creation of the migrations, and may be extended by these developers. 147 | $creator = $app['migration.creator']; 148 | 149 | $composer = $app['composer']; 150 | 151 | return new MakeMigrateDataCommand($creator, $composer); 152 | }); 153 | } 154 | 155 | /** 156 | * Get the services provided by the provider. 157 | * 158 | * @return array 159 | */ 160 | public function provides() 161 | { 162 | return array_merge($this->provides, $this->commands); 163 | } 164 | 165 | } 166 | --------------------------------------------------------------------------------