diff --git a/mailpoet/lib/Migrations/Migration_20221110_151621.php b/mailpoet/lib/Migrations/Migration_20221110_151621.php index d9a65f9725..78a5a47dc2 100644 --- a/mailpoet/lib/Migrations/Migration_20221110_151621.php +++ b/mailpoet/lib/Migrations/Migration_20221110_151621.php @@ -5,6 +5,15 @@ namespace MailPoet\Migrations; use MailPoet\Config\Env; use MailPoet\Migrator\Migration; +/** + * The "created_at" column must be NULL in some tables to avoid "there can be only one + * TIMESTAMP column with CURRENT_TIMESTAMP" error on MySQL version < 5.6.5 that occurs + * even when other timestamp is simply "NOT NULL". + * + * Additionally, having multiple timestamp columns with "NOT NULL" seems to produce the + * following error in some SQL modes: + * SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'updated_at'" + */ class Migration_20221110_151621 extends Migration { public function run(): void { $prefix = Env::$dbPrefix; diff --git a/mailpoet/lib/Migrator/Migration.php b/mailpoet/lib/Migrator/Migration.php index 084eb16618..2601849599 100644 --- a/mailpoet/lib/Migrator/Migration.php +++ b/mailpoet/lib/Migrator/Migration.php @@ -2,6 +2,7 @@ namespace MailPoet\Migrator; +use MailPoet\Config\Env; use MailPoet\DI\ContainerWrapper; use MailPoetVendor\Doctrine\DBAL\Connection; use MailPoetVendor\Doctrine\ORM\EntityManager; @@ -25,4 +26,15 @@ abstract class Migration { } abstract public function run(): void; + + protected function createTable(string $tableName, array $attributes): void { + $prefix = Env::$dbPrefix; + $charsetCollate = Env::$dbCharsetCollate; + $sql = implode(",\n", $attributes); + $this->connection->executeStatement(" + CREATE TABLE IF NOT EXISTS {$prefix}{$tableName} ( + $sql + ) {$charsetCollate}; + "); + } } diff --git a/mailpoet/tests/integration/API/JSON/v1/SetupTest.php b/mailpoet/tests/integration/API/JSON/v1/SetupTest.php index 1ee75b167c..b1bb37e3c4 100644 --- a/mailpoet/tests/integration/API/JSON/v1/SetupTest.php +++ b/mailpoet/tests/integration/API/JSON/v1/SetupTest.php @@ -16,15 +16,6 @@ use MailPoet\Settings\SettingsRepository; use MailPoet\Subscription\Captcha; use MailPoet\WP\Functions as WPFunctions; -/** - * The "created_at" column must be NULL in some tables to avoid "there can be only one - * TIMESTAMP column with CURRENT_TIMESTAMP" error on MySQL version < 5.6.5 that occurs - * even when other timestamp is simply "NOT NULL". - * - * Additionally, having multiple timestamp columns with "NOT NULL" seems to produce the - * following error in some SQL modes: - * SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'updated_at'" - */ class SetupTest extends \MailPoetTest { public function _before() { parent::_before();