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