Skip to content

Instantly share code, notes, and snippets.

@wayneashleyberry
Created April 20, 2016 10:03
Show Gist options
  • Save wayneashleyberry/9fef63e6845f4375b8f19c8068a40f2b to your computer and use it in GitHub Desktop.
Save wayneashleyberry/9fef63e6845f4375b8f19c8068a40f2b to your computer and use it in GitHub Desktop.
Laravel Artisan command to upgrade timestamp columns for MySQL 5.7 strict mode.
<?php
namespace App\Console\Commands;
use DB;
use Illuminate\Console\Command;
class UpgradeTimestamps extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'upgrade-timestamps';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Upgrades timestamp columns to be compatible with MySQL 5.7 strict mode.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$sql = 'select table_name from information_schema.tables where table_schema=?;';
$database = config('database.connections.mysql.database');
$tables = DB::select($sql, [$database]);
foreach ($tables as $table) {
$this->upgradeTable($database, $table->table_name);
}
}
protected function upgradeTable($database, $table)
{
$columns = DB::select('SHOW COLUMNS FROM `'.$database.'`.`'.$table.'`');
foreach ($columns as $column) {
if ($column->Type === 'timestamp' && $column->Default === '0000-00-00 00:00:00') {
$this->upgradeColumn($database, $table, $column->Field);
}
}
}
protected function upgradeColumn($database, $table, $column)
{
$sql = "ALTER TABLE `$table` CHANGE `$column` `$column` TIMESTAMP NULL DEFAULT NULL;";
echo $sql.PHP_EOL;
DB::statement("SET sql_mode = '';");
DB::statement($sql);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment