base-migration.php
2 years ago
version-2-1-0.php
2 years ago
version-2-1-7.php
2 years ago
version-2-1-8.php
2 years ago
base-migration.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Migrations\Versions; |
| 5 | |
| 6 | use JFB_Components\Repository\Repository_Item_With_Class; |
| 7 | use JFB_Components\Repository\Repository_Static_Item_It; |
| 8 | use Jet_Form_Builder\Db_Queries\Exceptions\Sql_Exception; |
| 9 | use Jet_Form_Builder\Migrations\Migration_Exception; |
| 10 | use Jet_Form_Builder\Migrations\Migration_Model; |
| 11 | use Jet_Form_Builder\Migrations\Migrator; |
| 12 | use Jet_Form_Builder\Migrations\Profilers\Base_Migration_Profiler; |
| 13 | |
| 14 | // If this file is called directly, abort. |
| 15 | if ( ! defined( 'WPINC' ) ) { |
| 16 | die; |
| 17 | } |
| 18 | |
| 19 | abstract class Base_Migration implements Repository_Static_Item_It { |
| 20 | |
| 21 | use Repository_Item_With_Class; |
| 22 | |
| 23 | /** @var Base_Migration_Profiler */ |
| 24 | private $profiler; |
| 25 | |
| 26 | abstract public function up( \wpdb $wpdb ); |
| 27 | |
| 28 | abstract public function down( \wpdb $wpdb ); |
| 29 | |
| 30 | public function __construct( Base_Migration_Profiler $profiler = null ) { |
| 31 | $this->set_profiler( $profiler ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @throws Migration_Exception |
| 36 | */ |
| 37 | final public function install() { |
| 38 | if ( $this->is_installed() ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | try { |
| 43 | $this->run_up(); |
| 44 | } catch ( Migration_Exception $exception ) { |
| 45 | throw $exception->set_version( static::class ); |
| 46 | } |
| 47 | |
| 48 | $this->set_installed(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @throws Migration_Exception |
| 53 | */ |
| 54 | final public function uninstall() { |
| 55 | if ( ! $this->is_installed() ) { |
| 56 | return; |
| 57 | } |
| 58 | $this->run_down(); |
| 59 | $this->set_uninstalled(); |
| 60 | } |
| 61 | |
| 62 | final public function install_automatic() { |
| 63 | if ( $this->is_installed() ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $this->save( 0 ); |
| 68 | $this->set_installed(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @throws Migration_Exception |
| 73 | */ |
| 74 | final protected function run_up() { |
| 75 | global $wpdb; |
| 76 | |
| 77 | timer_start(); |
| 78 | $this->up( $wpdb ); |
| 79 | |
| 80 | $timer_stop = timer_stop(); |
| 81 | $time = $timer_stop * 1000; |
| 82 | |
| 83 | $this->profiler->on_up_end( $this, $timer_stop ); |
| 84 | |
| 85 | if ( $wpdb->last_error ) { |
| 86 | throw new Migration_Exception( esc_html( $wpdb->last_error ) ); |
| 87 | } |
| 88 | |
| 89 | $this->save( $time ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @throws Migration_Exception |
| 94 | */ |
| 95 | final public function run_down() { |
| 96 | global $wpdb; |
| 97 | |
| 98 | timer_start(); |
| 99 | $this->down( $wpdb ); |
| 100 | $timer_stop = timer_stop(); |
| 101 | |
| 102 | $this->profiler->on_down_end( $this, $timer_stop ); |
| 103 | |
| 104 | if ( $wpdb->last_error ) { |
| 105 | throw new Migration_Exception( esc_html( $wpdb->last_error ) ); |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | $this->delete(); |
| 110 | } catch ( Sql_Exception $exception ) { |
| 111 | throw new Migration_Exception( esc_html( $exception->getMessage() ) ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public function save( int $time ) { |
| 116 | ( new Migration_Model() )->insert_soft( |
| 117 | array( |
| 118 | 'version' => get_class( $this ), |
| 119 | 'execution_time' => $time, |
| 120 | 'executed_at' => current_time( 'mysql' ), |
| 121 | ) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @throws Sql_Exception |
| 127 | */ |
| 128 | public function delete() { |
| 129 | ( new Migration_Model() )->delete( |
| 130 | array( |
| 131 | 'version' => get_class( $this ), |
| 132 | ) |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | final public function set_installed() { |
| 137 | Migrator::instance()->set_installed( $this ); |
| 138 | } |
| 139 | |
| 140 | final public function set_uninstalled() { |
| 141 | Migrator::instance()->set_uninstalled( $this ); |
| 142 | } |
| 143 | |
| 144 | final public function is_installed(): bool { |
| 145 | return Migrator::instance()->is_installed( $this ); |
| 146 | } |
| 147 | |
| 148 | public function set_profiler( Base_Migration_Profiler $profiler = null ): Base_Migration { |
| 149 | $this->profiler = $profiler ?? new Base_Migration_Profiler(); |
| 150 | |
| 151 | return $this; |
| 152 | } |
| 153 | |
| 154 | } |
| 155 |