AppMigration.php
2 years ago
AppMigrationTemplate.php
2 years ago
Cli.php
2 weeks ago
DbMigration.php
6 months ago
DbMigrationTemplate.php
2 years ago
Logger.php
2 years ago
Migrator.php
1 year ago
MigratorException.php
2 years ago
Repository.php
2 years ago
Runner.php
2 years ago
Store.php
6 months ago
index.php
3 years ago
Store.php
92 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Migrator; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Config\Env; |
| 9 | use MailPoetVendor\Doctrine\DBAL\Connection; |
| 10 | |
| 11 | class Store { |
| 12 | /** @var Connection */ |
| 13 | private $connection; |
| 14 | |
| 15 | /** @var string */ |
| 16 | private $table; |
| 17 | |
| 18 | public function __construct( |
| 19 | Connection $connection |
| 20 | ) { |
| 21 | $this->connection = $connection; |
| 22 | $this->table = Env::$dbPrefix . 'migrations'; |
| 23 | } |
| 24 | |
| 25 | public function getMigrationsTable(): string { |
| 26 | return $this->table; |
| 27 | } |
| 28 | |
| 29 | public function startMigration(string $name): void { |
| 30 | $this->connection->executeStatement(" |
| 31 | INSERT INTO {$this->table} (name, started_at) |
| 32 | VALUES (?, current_timestamp()) |
| 33 | ON DUPLICATE KEY UPDATE |
| 34 | started_at = current_timestamp(), |
| 35 | completed_at = NULL, |
| 36 | retries = retries + 1, |
| 37 | error = NULL |
| 38 | ", [$name]); |
| 39 | } |
| 40 | |
| 41 | public function completeMigration(string $name): void { |
| 42 | $this->connection->executeStatement(" |
| 43 | UPDATE {$this->table} |
| 44 | SET completed_at = current_timestamp() |
| 45 | WHERE name = ? |
| 46 | ", [$name]); |
| 47 | } |
| 48 | |
| 49 | public function failMigration(string $name, string $error): void { |
| 50 | $this->connection->executeStatement(" |
| 51 | UPDATE {$this->table} |
| 52 | SET |
| 53 | completed_at = current_timestamp(), |
| 54 | error = ? |
| 55 | WHERE name = ? |
| 56 | ", [$error ?: 'Unknown error', $name]); |
| 57 | } |
| 58 | |
| 59 | public function getAll(): array { |
| 60 | // Some backup plugins may convert NULL values to empty strings, |
| 61 | // in which case we need to cast the error column value to NULL. |
| 62 | return $this->connection->fetchAllAssociative(" |
| 63 | SELECT |
| 64 | id, |
| 65 | name, |
| 66 | started_at, |
| 67 | completed_at, |
| 68 | retries, |
| 69 | IF(error = '', NULL, error) AS error |
| 70 | FROM {$this->table} |
| 71 | ORDER BY id ASC |
| 72 | "); |
| 73 | } |
| 74 | |
| 75 | public function ensureMigrationsTable(): void { |
| 76 | global $wpdb; |
| 77 | $collate = $wpdb->get_charset_collate(); |
| 78 | $this->connection->executeStatement(" |
| 79 | CREATE TABLE IF NOT EXISTS {$this->table} ( |
| 80 | id int(11) unsigned NOT NULL AUTO_INCREMENT, |
| 81 | name varchar(191) NOT NULL, |
| 82 | started_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 83 | completed_at timestamp NULL, |
| 84 | retries int(11) unsigned NOT NULL DEFAULT 0, |
| 85 | error text NULL, |
| 86 | PRIMARY KEY (id), |
| 87 | UNIQUE KEY (name) |
| 88 | ) {$collate}; |
| 89 | "); |
| 90 | } |
| 91 | } |
| 92 |