From 48090351bd83f91109ab9758437e9d687ee523ec Mon Sep 17 00:00:00 2001 From: Dave Shoreman Date: Tue, 26 Apr 2016 12:09:07 +0100 Subject: [PATCH] Convert current core timestamp columns to nullable Adds migrations to switch all existing timestamp fields from being NOT NULL DEFAULT 0, to NULL DEFAULT NULL, in order to get around issues with new default modes in MySQL that cause errors in 0 dates. --- ..._10_01_000009_Db_Backend_Timestamp_Fix.php | 28 +++++++++++++ ...2016_10_01_000002_Db_Cms_Timestamp_Fix.php | 19 +++++++++ ...10_01_000003_Db_System_Plugin_Versions.php | 2 +- ..._10_01_000004_Db_System_Plugin_History.php | 2 +- .../2015_10_01_000018_Db_FailedJobs.php | 2 +- ...6_10_01_000020_Db_System_Timestamp_Fix.php | 40 +++++++++++++++++++ 6 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 modules/backend/database/migrations/2016_10_01_000009_Db_Backend_Timestamp_Fix.php create mode 100644 modules/cms/database/migrations/2016_10_01_000002_Db_Cms_Timestamp_Fix.php create mode 100644 modules/system/database/migrations/2016_10_01_000020_Db_System_Timestamp_Fix.php diff --git a/modules/backend/database/migrations/2016_10_01_000009_Db_Backend_Timestamp_Fix.php b/modules/backend/database/migrations/2016_10_01_000009_Db_Backend_Timestamp_Fix.php new file mode 100644 index 000000000..77f0d3b40 --- /dev/null +++ b/modules/backend/database/migrations/2016_10_01_000009_Db_Backend_Timestamp_Fix.php @@ -0,0 +1,28 @@ +backendTables as $table) { + DbDongle::convertTimestamps($table); + } + } + + public function down() + { + // ... + } +} diff --git a/modules/cms/database/migrations/2016_10_01_000002_Db_Cms_Timestamp_Fix.php b/modules/cms/database/migrations/2016_10_01_000002_Db_Cms_Timestamp_Fix.php new file mode 100644 index 000000000..5a11deb0a --- /dev/null +++ b/modules/cms/database/migrations/2016_10_01_000002_Db_Cms_Timestamp_Fix.php @@ -0,0 +1,19 @@ +increments('id'); $table->string('code')->index(); $table->string('version', 50); - $table->timestamp('created_at'); + $table->timestamp('created_at')->nullable(); }); } diff --git a/modules/system/database/migrations/2013_10_01_000004_Db_System_Plugin_History.php b/modules/system/database/migrations/2013_10_01_000004_Db_System_Plugin_History.php index f4486b7e1..0bf14b54d 100644 --- a/modules/system/database/migrations/2013_10_01_000004_Db_System_Plugin_History.php +++ b/modules/system/database/migrations/2013_10_01_000004_Db_System_Plugin_History.php @@ -14,7 +14,7 @@ class DbSystemPluginHistory extends Migration $table->string('type', 20)->index(); $table->string('version', 50); $table->string('detail')->nullable(); - $table->timestamp('created_at'); + $table->timestamp('created_at')->nullable(); }); } diff --git a/modules/system/database/migrations/2015_10_01_000018_Db_FailedJobs.php b/modules/system/database/migrations/2015_10_01_000018_Db_FailedJobs.php index 0d72b40ac..4c528d879 100644 --- a/modules/system/database/migrations/2015_10_01_000018_Db_FailedJobs.php +++ b/modules/system/database/migrations/2015_10_01_000018_Db_FailedJobs.php @@ -13,7 +13,7 @@ class DbFailedJobs extends Migration $table->text('connection'); $table->text('queue'); $table->text('payload'); - $table->timestamp('failed_at'); + $table->timestamp('failed_at')->nullable(); }); } diff --git a/modules/system/database/migrations/2016_10_01_000020_Db_System_Timestamp_Fix.php b/modules/system/database/migrations/2016_10_01_000020_Db_System_Timestamp_Fix.php new file mode 100644 index 000000000..a42fbea54 --- /dev/null +++ b/modules/system/database/migrations/2016_10_01_000020_Db_System_Timestamp_Fix.php @@ -0,0 +1,40 @@ + 'failed_at', + 'system_files', + 'system_event_logs', + 'system_mail_layouts', + 'system_mail_templates', + 'system_plugin_history' => 'created_at', + 'system_plugin_versions' => 'created_at', + 'system_request_logs', + 'system_revisions', + ]; + + public function up() + { + // Disable all special modes such as NO_ZERO_DATE to prevent any + // errors from MySQL before we can update the timestamp columns. + Db::statement("SET @@SQL_MODE=''"); + + foreach ($this->coreTables as $table => $columns) { + if (is_int($table)) { + $table = $columns; + $columns = ['created_at', 'updated_at']; + } + + DbDongle::convertTimestamps($table, $columns); + } + } + + public function down() + { + // ... + } +}