tables as $db) {
$table = $schema->tableName($db);
$columns = $schema->databaseFields($db);
$query = "SHOW COLUMNS FROM $table";
$liveColumns = DB::query($query)->column();
$query = "SHOW TABLES LIKE 'Backup_$table'";
$tableExists = DB::query($query)->value();
if ($tableExists != null) {
$output->writeln("Tasks run already on $table exiting");
return Command::SUCCESS;
}
$backedUp = false;
foreach ($liveColumns as $column) {
if (!$backedUp) {
$output->writeln("Backing up $table
");
$output->writeln("Creating Backup_$table
");
// backup table
$query = "CREATE TABLE Backup_$table LIKE $table";
DB::query($query);
$output->writeln("Populating Backup_$table
");
$query = "INSERT Backup_$table SELECT * FROM $table";
DB::query($query);
$backedUp = true;
}
if (!isset($columns[$column]) && !in_array($column, $this->keepColumns ?? [])) {
$output->writeln("Dropping $column from $table
");
$query = "ALTER TABLE $table DROP COLUMN $column";
DB::query($query);
}
}
}
return Command::SUCCESS;
}
}