| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ByJG\DbMigration\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class CreateCommand extends Command | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |             ->setName('create') | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |             ->setDescription('Create the directory structure FROM a pre-existing database') | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             ->addArgument( | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |                 'path', | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |                 InputArgument::REQUIRED, | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |                 'Define the path where the base.sql resides. If not set assumes the current folder' | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             ->addOption( | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |                 'migration', | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |                 'm', | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |                 'Create the migration script (Up and Down)' | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             ->addUsage('') | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             ->addUsage('Example: ') | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             ->addUsage('   migrate create --path /path/to/strcuture') | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |             ->addUsage('   migrate create --path /path/to/strcuture --migration ') | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected function initialize(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function createMigrationSql($path, $startVersion) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $files = glob("$path/*.sql"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $lastVersion = $startVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         foreach ($files as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $version = intval(basename($file)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             if ($version > $lastVersion) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $lastVersion = $version; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $lastVersion = $lastVersion + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         file_put_contents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             "$path/" . str_pad($lastVersion, 5, '0', STR_PAD_LEFT) . ".sql", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             "-- Migrate to Version $lastVersion \n\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $lastVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $path = $input->getArgument('path'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if (!file_exists($path)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             mkdir($path, 0777, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if (!file_exists("$path/base.sql")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             file_put_contents("$path/base.sql", "-- Put here your base SQL"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if (!file_exists("$path/migrations")) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             mkdir("$path/migrations", 0777, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             mkdir("$path/migrations/up", 0777, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             mkdir("$path/migrations/down", 0777, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         if ($input->hasOption('migration')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $output->writeln('Created UP version: ' . $this->createMigrationSql("$path/migrations/up", 0)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $output->writeln('Created DOWN version: ' . $this->createMigrationSql("$path/migrations/down", -1)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  |