bastinald / laravel-automatic-migrations

Automatic Laravel model migrations.
42 stars 11 forks source link

Suggest: Confirm migration if is about to delete columns #8

Open marstell-sw opened 2 years ago

marstell-sw commented 2 years ago

This issue is only a proposal:

Since auto migration could lead to accidental column deletions and data loss while updating existing tables, I added some code to MigrateAutoCommand.php to alert the user if the running migration is about to delete some columns.

Here is my proposal:

     [ ... ]
           $tableDiff = (new Comparator)->diffTable($modelTableDetails, $tempTableDetails);

            if ($tableDiff) {
                $delcount=count($tableDiff->removedColumns);    
                if ($delcount==0 || 
                     $this->confirm('This migration will delete ' . $delcount . ' column(s): ['  . 
                           implode(', ',array_keys($tableDiff->removedColumns))  .   ']. Proceed?')){

                    $schemaManager->alterTable($tableDiff);

                    $this->line('<info>Table updated:</info> ' . $modelTable);
                }
                else {
                    $this->line('<info>Table update CANCELED:</info> ' . $modelTable);
                }
            }
     [ ... ]

Bye, Marco