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
DbMigration.php
105 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 MailPoet\DI\ContainerWrapper; |
| 10 | use MailPoetVendor\Doctrine\DBAL\Connection; |
| 11 | use MailPoetVendor\Doctrine\DBAL\Exception; |
| 12 | use MailPoetVendor\Doctrine\ORM\EntityManager; |
| 13 | |
| 14 | abstract class DbMigration { |
| 15 | /** @var Connection */ |
| 16 | protected $connection; |
| 17 | |
| 18 | /** @var EntityManager */ |
| 19 | private $entityManager; |
| 20 | |
| 21 | public function __construct( |
| 22 | ContainerWrapper $container |
| 23 | ) { |
| 24 | $this->connection = $container->get(Connection::class); |
| 25 | $this->entityManager = $container->get(EntityManager::class); |
| 26 | } |
| 27 | |
| 28 | abstract public function run(): void; |
| 29 | |
| 30 | /** |
| 31 | * @param class-string<object> $entityClass |
| 32 | */ |
| 33 | protected function getTableName(string $entityClass): string { |
| 34 | return $this->entityManager->getClassMetadata($entityClass)->getTableName(); |
| 35 | } |
| 36 | |
| 37 | protected function createTable(string $tableName, array $attributes): void { |
| 38 | global $wpdb; |
| 39 | $prefix = Env::$dbPrefix; |
| 40 | $charsetCollate = $wpdb->get_charset_collate(); |
| 41 | $sql = implode(",\n", $attributes); |
| 42 | $this->connection->executeStatement(" |
| 43 | CREATE TABLE IF NOT EXISTS {$prefix}{$tableName} ( |
| 44 | $sql |
| 45 | ) {$charsetCollate}; |
| 46 | "); |
| 47 | } |
| 48 | |
| 49 | protected function columnExists(string $tableName, string $columnName): bool { |
| 50 | global $wpdb; |
| 51 | $suppressErrors = $wpdb->suppress_errors(); |
| 52 | try { |
| 53 | $this->connection->executeStatement("SELECT $columnName FROM $tableName LIMIT 0"); |
| 54 | return true; |
| 55 | } catch (Exception $e) { |
| 56 | return false; |
| 57 | } finally { |
| 58 | $wpdb->suppress_errors($suppressErrors); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | protected function tableExists(string $tableName): bool { |
| 63 | global $wpdb; |
| 64 | $suppressErrors = $wpdb->suppress_errors(); |
| 65 | try { |
| 66 | $this->connection->executeStatement("SELECT 1 FROM $tableName LIMIT 0"); |
| 67 | return true; |
| 68 | } catch (Exception $e) { |
| 69 | return false; |
| 70 | } finally { |
| 71 | $wpdb->suppress_errors($suppressErrors); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | protected function indexExists(string $tableName, string $indexName): bool { |
| 76 | global $wpdb; |
| 77 | $suppressErrors = $wpdb->suppress_errors(); |
| 78 | try { |
| 79 | $this->connection->executeStatement("ALTER TABLE $tableName ADD INDEX $indexName (__non__existent__column__name__)"); |
| 80 | } catch (Exception $e) { |
| 81 | // Index creating index failed on not existing column we use a fallback. This can happen on MySQL 5.7 and lower and on some MariaDB versions. |
| 82 | if ($e->getCode() === 1072) { |
| 83 | $database = $wpdb->dbname; |
| 84 | $result = $wpdb->get_var($wpdb->prepare( |
| 85 | "SELECT count(*) |
| 86 | FROM information_schema.statistics |
| 87 | WHERE table_schema = COALESCE(DATABASE(), %s) |
| 88 | AND table_name = %s |
| 89 | AND index_name = %s", |
| 90 | $database, |
| 91 | $tableName, |
| 92 | $indexName |
| 93 | )); |
| 94 | |
| 95 | return $result > 0; |
| 96 | } |
| 97 | // Index exists when the error message contains its name. Otherwise, it's the non-existent column error. |
| 98 | return strpos($e->getMessage(), $indexName) !== false; |
| 99 | } finally { |
| 100 | $wpdb->suppress_errors($suppressErrors); |
| 101 | } |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 |