├── .gitignore ├── src ├── Traits │ └── OnlyAndExceptArguments.php ├── Providers │ └── RevengeDbServiceProvider.php ├── Abstracts │ └── DatabaseCommand.php └── Console │ └── MigrationsCommand.php ├── composer.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea 6 | -------------------------------------------------------------------------------- /src/Traits/OnlyAndExceptArguments.php: -------------------------------------------------------------------------------- 1 | passedOnly() && $this->passedExcept()); 11 | } 12 | 13 | /** 14 | * @return bool 15 | */ 16 | protected function passedOnly() 17 | { 18 | return !is_null($this->option('only')); 19 | } 20 | 21 | /** 22 | * @return bool 23 | */ 24 | protected function passedExcept() 25 | { 26 | return !is_null($this->option('except')); 27 | } 28 | 29 | protected function commaParse($string) 30 | { 31 | $string = str_replace(' ', '', $string); 32 | 33 | return explode(',', $string); 34 | } 35 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "daavelar/revengedb", 3 | "require": { 4 | "php": ">=5.5.0", 5 | "illuminate/support": "~5.1|^6.0", 6 | "laracasts/generators": "~1.1.2", 7 | "doctrine/dbal": "~2.5.1" 8 | }, 9 | "authors": [ 10 | { 11 | "name": "Diego Armando Avelar", 12 | "email": "diegoarmando2011@gmail.com" 13 | } 14 | ], 15 | "autoload": { 16 | "psr-4": { 17 | "Daavelar\\RevengeDb\\": "src/" 18 | } 19 | }, 20 | "keywords": [ 21 | "laravel", 22 | "migration", 23 | "generator", 24 | "migrations", 25 | "artisan", 26 | "models", 27 | "reverse engineer", 28 | "from existent db" 29 | ], 30 | "description": "RevengeDb provides an artisan command to make a reverse engineer of the database, generating automatically the migrations for you" 31 | } 32 | -------------------------------------------------------------------------------- /src/Providers/RevengeDbServiceProvider.php: -------------------------------------------------------------------------------- 1 | app['revengedb.migrations'] = $this->app->share(function ($app) { 28 | return new MigrationsCommand; 29 | }); 30 | $this->commands('revengedb.migrations'); 31 | } 32 | 33 | public function boot() 34 | { 35 | $this->app->register(GeneratorsServiceProvider::class); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Laravel 5/6 Package :: RevengeDb 2 | 3 | With this package you will be able to transform your existent schema (only support mysql at the moment) in migration files of Laravel 4 | with the following command: 5 | 6 | - `php artisan revengedb:migrations` 7 | 8 | # Installation 9 | 10 | Add the package to your Laravel project: 11 | 12 | composer require daavelar/revengedb 13 | 14 | Add the ServiceProvider to the app.php file 15 | 16 | Daavelar\RevengeDb\Providers\RevengeDbServiceProvider::class 17 | 18 | # Using: 19 | 20 | Generating a migration file for all tables of the database of your mysql config 21 | 22 | php artisan revengedb:migrations 23 | 24 | If you want just execute the action in one or more tables, you can pass only as a parameter 25 | 26 | php artisan revengedb:migrations --only=table1,table2,table3 27 | 28 | If you want just exclude one or more tables, you can pass the except parameter 29 | 30 | php artisan revengedb:migrations --except=table1,table2,table3 31 | 32 | Important: 33 | All migration files will be generated with the $table->timestamps() line. 34 | 35 | -------------------------------------------------------------------------------- /src/Abstracts/DatabaseCommand.php: -------------------------------------------------------------------------------- 1 | config('database.connections.mysql.database'), 19 | 'user' => config('database.connections.mysql.username'), 20 | 'password' => config('database.connections.mysql.password'), 21 | 'host' => config('database.connections.mysql.host'), 22 | 'driver' => 'pdo_mysql', 23 | ]; 24 | 25 | $conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config); 26 | 27 | $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); 28 | $this->schema = $conn->getSchemaManager(); 29 | 30 | parent::__construct(); 31 | } 32 | 33 | /** 34 | * @return mixed 35 | */ 36 | protected function tables() 37 | { 38 | if($this->passedOnlyAndExcept()) { 39 | $this->error('You can`t set the only and the except param at the same time.'); 40 | exit; 41 | } 42 | 43 | $tables = $this->schema->listTables(); 44 | 45 | $tables_to_return = []; 46 | 47 | if($this->passedOnly()) { 48 | foreach($tables as $table) { 49 | if(in_array($table->getName(), $this->commaParse($this->option('only')))) { 50 | $tables_to_return[] = $table; 51 | } 52 | } 53 | } 54 | 55 | if($this->passedExcept()) { 56 | foreach($tables as $table) { 57 | if(!in_array($table->getName(), $this->commaParse($this->option('only')))) { 58 | $tables_to_return[] = $table; 59 | } 60 | } 61 | } 62 | 63 | if(!$this->passedExcept() && !$this->passedOnly()) { 64 | foreach($tables as $table) { 65 | $tables_to_return[] = $table; 66 | } 67 | } 68 | 69 | return $tables_to_return; 70 | } 71 | 72 | /** 73 | * Get the console command options. 74 | * 75 | * @return array 76 | */ 77 | protected function getOptions() 78 | { 79 | return [ 80 | ['only', null, InputOption::VALUE_OPTIONAL, 'The tables to make the action.', null], 81 | ['except', null, InputOption::VALUE_OPTIONAL, 'The tables to not make the action.', null], 82 | ['filename', null, InputOption::VALUE_OPTIONAL, 'The name of the file.', null], 83 | ]; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/Console/MigrationsCommand.php: -------------------------------------------------------------------------------- 1 | tables() as $table) { 42 | if($table->getName() == 'migrations') continue; 43 | 44 | $columns = $this->schema->listTableColumns($table->getName()); 45 | 46 | $argumentos = ""; 47 | 48 | foreach($columns as $column) { 49 | if($column->getName() == 'id') continue; 50 | if(!in_array($column->getName(), ['created_at', 'updated_at'])) { 51 | $argumentos .= $column->getName() . ':' . $this->doctrineTypeToGenerator($column->getType()) . ','; 52 | } 53 | } 54 | 55 | $argumentos = rtrim($argumentos, ','); 56 | 57 | $this->callSilent('make:migration:schema', [ 58 | 'name' => "create_{$table->getName()}_table", 59 | '--schema' => $argumentos 60 | ]); 61 | 62 | $this->info("Creating migration file for table {$table->getName()}"); 63 | } 64 | 65 | } 66 | 67 | public function doctrineTypeToGenerator($type) 68 | { 69 | $type = strtolower($type); 70 | 71 | switch($type) { 72 | case 'integer': 73 | return 'integer'; 74 | case 'string': 75 | case 'simplearray': 76 | return 'string'; 77 | case 'date': 78 | return 'date'; 79 | case 'datetime': 80 | return 'datetime'; 81 | case 'time': 82 | return 'time'; 83 | case 'float': 84 | return 'float'; 85 | case 'text': 86 | case 'blob': 87 | return 'text'; 88 | } 89 | } 90 | 91 | /** 92 | * Get the console command arguments. 93 | * 94 | * @return array 95 | */ 96 | protected function getArguments() 97 | { 98 | return [ 99 | 100 | ]; 101 | } 102 | 103 | 104 | } 105 | --------------------------------------------------------------------------------