| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteMigration |
| 4 |
{ |
| 5 |
|
| 6 |
public static function migrate() |
| 7 |
{ |
| 8 |
$prefix = db()->prefix; |
| 9 |
$cartsTableSql = "CREATE TABLE IF NOT EXISTS {$prefix}woo_mailerlite_carts ( |
| 10 |
id BIGINT (20) NOT NULL AUTO_INCREMENT, |
| 11 |
hash VARCHAR(255) DEFAULT NULL, |
| 12 |
email VARCHAR(255) DEFAULT NULL, |
| 13 |
subscribe TINYINT(1) DEFAULT 0, |
| 14 |
data LONGTEXT NOT NULL, |
| 15 |
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 16 |
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 17 |
PRIMARY KEY (id), |
| 18 |
KEY created_at (created_at)) |
| 19 |
DEFAULT CHARACTER |
| 20 |
SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;"; |
| 21 |
db()->query($cartsTableSql); |
| 22 |
$jobsTableMigration = "CREATE TABLE IF NOT EXISTS {$prefix}woo_mailerlite_jobs ( |
| 23 |
id BIGINT (20) NOT NULL AUTO_INCREMENT, |
| 24 |
object_id TEXT NOT NULL, |
| 25 |
job TEXT NOT NULL, |
| 26 |
data LONGTEXT NOT NULL, |
| 27 |
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 28 |
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 29 |
PRIMARY KEY (id)) |
| 30 |
DEFAULT CHARACTER |
| 31 |
SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;"; |
| 32 |
db()->query($jobsTableMigration); |
| 33 |
} |
| 34 |
|
| 35 |
public static function addCartsCreatedAtIndex() |
| 36 |
{ |
| 37 |
if (WooMailerLiteOptions::get('cartsCreatedAtIndexAdded')) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$prefix = db()->prefix; |
| 42 |
$table = "{$prefix}woo_mailerlite_carts"; |
| 43 |
$indexExists = db()->get_var("SHOW INDEX FROM {$table} WHERE Key_name = 'created_at'"); |
| 44 |
|
| 45 |
if (!$indexExists) { |
| 46 |
db()->query("ALTER TABLE {$table} ADD INDEX created_at (created_at)"); |
| 47 |
} |
| 48 |
|
| 49 |
WooMailerLiteOptions::update('cartsCreatedAtIndexAdded', true); |
| 50 |
} |
| 51 |
|
| 52 |
public static function rollback() |
| 53 |
{ |
| 54 |
$prefix = db()->prefix; |
| 55 |
db()->query("DROP TABLE IF EXISTS {$prefix}woo_mailerlite_carts"); |
| 56 |
db()->query("DROP TABLE IF EXISTS {$prefix}woo_mailerlite_jobs"); |
| 57 |
} |
| 58 |
|
| 59 |
public static function truncate() |
| 60 |
{ |
| 61 |
$prefix = db()->prefix; |
| 62 |
$tables = [ |
| 63 |
"{$prefix}woo_mailerlite_carts", |
| 64 |
"{$prefix}woo_mailerlite_jobs", |
| 65 |
]; |
| 66 |
|
| 67 |
foreach ($tables as $table) { |
| 68 |
$exists = db()->get_var(db()->prepare( |
| 69 |
"SHOW TABLES LIKE %s", |
| 70 |
$table |
| 71 |
)); |
| 72 |
|
| 73 |
if ($exists === $table) { |
| 74 |
db()->query("TRUNCATE TABLE $table"); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public static function customPrefixTablesMigrate() |
| 80 |
{ |
| 81 |
try { |
| 82 |
$oldPrefix = 'wp_'; |
| 83 |
$newPrefix = db()->prefix; |
| 84 |
|
| 85 |
$tables = [ |
| 86 |
$oldPrefix . 'woo_mailerlite_carts' => $newPrefix . 'woo_mailerlite_carts', |
| 87 |
$oldPrefix . 'woo_mailerlite_jobs' => $newPrefix . 'woo_mailerlite_jobs' |
| 88 |
]; |
| 89 |
|
| 90 |
foreach ($tables as $key => $value) { |
| 91 |
if (db()->get_var("SHOW TABLES LIKE '{$key}'") === $key && |
| 92 |
db()->get_var("SHOW TABLES LIKE '{$value}'") !== $value) { |
| 93 |
db()->query("RENAME TABLE {$key} TO {$value}"); |
| 94 |
} |
| 95 |
} |
| 96 |
WooMailerLiteOptions::update('customTableCheck', true); |
| 97 |
} catch (Throwable $th) { |
| 98 |
WooMailerLiteLog()->error($th->getMessage(), ['trace' => $th->getTraceAsString()]); |
| 99 |
} |
| 100 |
return true; |
| 101 |
} |
| 102 |
} |