mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Fix invalid dates in the database (#12512)
This commit is contained in:
46
database/migrations/2021_02_08_224355_fix_invalid_dates.php
Normal file
46
database/migrations/2021_02_08_224355_fix_invalid_dates.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class FixInvalidDates extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if (\LibreNMS\DB\Eloquent::getDriver() != 'mysql') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Too prevent errors like, Incorrect datetime value: '0000-00-00 00:00:00'
|
||||
\LibreNMS\DB\Eloquent::setStrictMode(false);
|
||||
|
||||
$database_name = DB::connection()->getDatabaseName();
|
||||
|
||||
$columns = DB::table('information_schema.columns')
|
||||
->where('table_schema', $database_name)
|
||||
->whereIn('COLUMN_TYPE', ['datetime', 'timestamp'])
|
||||
->get(['TABLE_NAME', 'COLUMN_NAME']);
|
||||
|
||||
foreach ($columns as $column) {
|
||||
DB::table($column->TABLE_NAME)
|
||||
->where($column->COLUMN_NAME, '0000-00-00 00:00:00')
|
||||
->update([$column->COLUMN_NAME => '1970-01-02 00:00:01']);
|
||||
}
|
||||
|
||||
\LibreNMS\DB\Eloquent::setStrictMode(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user