├── .gitignore ├── composer.json ├── readme.md └── src ├── Barryvdh └── MigrationGenerator │ ├── MigrationGeneratorCommand.php │ └── MigrationGeneratorServiceProvider.php └── config └── config.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "barryvdh/laravel-migration-generator", 3 | "description": "Generate migrations based on existing database", 4 | "keywords": ["laravel", "migration", "generator", "artisan"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Barry vd. Heuvel", 9 | "email": "barryvdh@gmail.com" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.3.0", 14 | "illuminate/support": "~4", 15 | "doctrine/dbal": "~2.3", 16 | "way/generators": "1.x" 17 | }, 18 | "autoload": { 19 | "psr-0": { 20 | "Barryvdh\\MigrationGenerator": "src/" 21 | } 22 | }, 23 | "minimum-stability": "dev" 24 | } 25 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Laravel4 Migration Generator 2 | 3 | ### Generate migrations based on existing tables 4 | 5 | ***You might want to checkout https://github.com/Xethron/migrations-generator which is similar to this package, but is developed more recently and is using way/generators version 2.x. My package is still on 1.x and I'll have to see when/if I can upgrade. This package does still work, but isn't actively supported currently*** 6 | 7 | Require this package in your composer.json: 8 | 9 | "barryvdh/laravel-migration-generator": "dev-master" 10 | 11 | And update dependencies using composer: 12 | 13 | composer update 14 | 15 | And add the ServiceProvider to the providers array in app/config/app.php 16 | 17 | 'Barryvdh\MigrationGenerator\MigrationGeneratorServiceProvider', 18 | 19 | You can now generate the migration by running the command, followed by the tables you want to generate 20 | 21 | php artisan migration-generate posts,users 22 | 23 | Your migrations will be created in app/database/migrations 24 | 25 | Migrations are generated by https://github.com/JeffreyWay/Laravel-4-Generators 26 | This assumes the key is 'id' and timestamps (created_at/updated_at) are generated by default. 27 | 28 | -------------------------------------------------------------------------------- /src/Barryvdh/MigrationGenerator/MigrationGeneratorCommand.php: -------------------------------------------------------------------------------- 1 | generator = $generator; 50 | 51 | foreach($config as $key=>$value){ 52 | if (property_exists($this, $key)){ 53 | $this->$key = $value; 54 | } 55 | } 56 | } 57 | 58 | /** 59 | * Execute the console command. 60 | * 61 | * @return void 62 | */ 63 | public function fire() 64 | { 65 | $tables = explode(',', $this->argument('tables')); 66 | $prefix = \DB::getTablePrefix(); 67 | 68 | foreach($tables as $table){ 69 | $name = 'create_'.$table.'_table'; 70 | $fields = $this->detectColumns($prefix.$table); 71 | $path = $this->option('path') . '/' .$name.'.php'; 72 | 73 | $created = $this->generator 74 | ->parse($name, $fields) 75 | ->make($path, null); 76 | 77 | if($created){ 78 | $this->info("Created migration $path"); 79 | }else{ 80 | $this->error("Could not create $path with: $fields"); 81 | } 82 | } 83 | } 84 | 85 | public function detectColumns($table){ 86 | 87 | $fields = array(); 88 | 89 | $schema = \DB::getDoctrineSchemaManager($table); 90 | 91 | foreach ($this->registerTypes as $convertFrom=>$convertTo) { 92 | $schema->getDatabasePlatform()->registerDoctrineTypeMapping($convertFrom, $convertTo); 93 | } 94 | 95 | $indexes = $schema->listTableIndexes($table); 96 | foreach ($indexes as $index) { 97 | if($index->isUnique()){ 98 | $unique[$index->getName()] = true; 99 | } 100 | } 101 | 102 | $columns = $schema->listTableColumns($table); 103 | 104 | if($columns){ 105 | foreach ($columns as $column) { 106 | $name = $column->getName(); 107 | $type = $column->getType()->getName(); 108 | $length = $column->getLength(); 109 | $default = $column->getDefault(); 110 | if(isset($this->fieldTypeMap[$type])) { 111 | $type = $this->fieldTypeMap[$type]; 112 | } 113 | if(!in_array($name, array('id', 'created_at', 'updated_at'))){ 114 | $field = "$name:$type"; 115 | if($length){ 116 | $field .= "[$length]"; 117 | } 118 | if(!$column->getNotNull()){ 119 | $field .= ':nullable'; 120 | } 121 | if(isset($unique[$name])){ 122 | $field .= ':unique'; 123 | } 124 | $fields[] = $field; 125 | } 126 | } 127 | } 128 | return implode(', ', $fields); 129 | } 130 | 131 | /** 132 | * Get the console command arguments. 133 | * 134 | * @return array 135 | */ 136 | protected function getArguments() 137 | { 138 | return array( 139 | array('tables', InputArgument::REQUIRED, 'The table name') 140 | 141 | ); 142 | } 143 | 144 | /** 145 | * Get the console command options. 146 | * 147 | * @return array 148 | */ 149 | protected function getOptions() 150 | { 151 | return array( 152 | array('path', null, InputOption::VALUE_OPTIONAL, 'The path to store the migration', 'app/database/migrations') 153 | ); 154 | } 155 | 156 | 157 | } 158 | -------------------------------------------------------------------------------- /src/Barryvdh/MigrationGenerator/MigrationGeneratorServiceProvider.php: -------------------------------------------------------------------------------- 1 | package('barryvdh/laravel-migration-generator'); 25 | } 26 | 27 | /** 28 | * Register the service provider. 29 | * 30 | * @return void 31 | */ 32 | public function register() 33 | { 34 | $this->app['command.migration-generator'] = $this->app->share(function($app) 35 | { 36 | $cache = new Cache($app['files']); 37 | $generator = new Generators\MigrationGenerator($app['files'], $cache); 38 | $config = $app['config']->get('laravel-migration-generator::config'); 39 | 40 | return new MigrationGeneratorCommand($generator,$config); 41 | }); 42 | $this->commands('command.migration-generator'); 43 | } 44 | 45 | /** 46 | * Get the services provided by the provider. 47 | * 48 | * @return array 49 | */ 50 | public function provides() 51 | { 52 | return array('command.migration-generator'); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | array( 16 | 'enum' => 'string', 17 | 'bit' => 'boolean' 18 | ), 19 | 20 | /* 21 | |-------------------------------------------------------------------------- 22 | | Field Type Map 23 | |-------------------------------------------------------------------------- 24 | | 25 | | Used to convert certain field types and cast them as another. Similar to 26 | | the registered types, but are not registered by Doctrine and are simply 27 | | converted as columns are read. 28 | | 29 | */ 30 | 31 | 'fieldTypeMap' => array( 32 | 'guid' => 'string', 33 | 'bigint' => 'integer', 34 | 'littleint' => 'integer', 35 | 'datetimetz' => 'datetime' 36 | ) 37 | 38 | ); 39 | --------------------------------------------------------------------------------