| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\Lib\Migrations; |
| 4 |
|
| 5 |
/** |
| 6 |
* Base migration class. |
| 7 |
*/ |
| 8 |
abstract class Migration { |
| 9 |
|
| 10 |
/** |
| 11 |
* The plugin this migration belongs to. |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $plugin = 'unknown'; |
| 16 |
|
| 17 |
/** |
| 18 |
* The adapter. |
| 19 |
* |
| 20 |
* @var Adapter |
| 21 |
*/ |
| 22 |
private $adapter; |
| 23 |
|
| 24 |
/** |
| 25 |
* Performs the migration. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
abstract public function up(); |
| 30 |
|
| 31 |
/** |
| 32 |
* Reverts the migration. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
abstract public function down(); |
| 37 |
|
| 38 |
/** |
| 39 |
* Creates a new migration. |
| 40 |
* |
| 41 |
* @param Adapter $adapter The current adapter. |
| 42 |
*/ |
| 43 |
public function __construct( Adapter $adapter ) { |
| 44 |
$this->set_adapter( $adapter ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Sets an adapter. |
| 49 |
* |
| 50 |
* @param Adapter $adapter The adapter to set. |
| 51 |
* |
| 52 |
* @return $this|null |
| 53 |
*/ |
| 54 |
public function set_adapter( $adapter ) { |
| 55 |
if ( ! $adapter instanceof Adapter ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
$this->adapter = $adapter; |
| 59 |
return $this; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the current adapter. |
| 64 |
* |
| 65 |
* @return object |
| 66 |
*/ |
| 67 |
public function get_adapter() { |
| 68 |
return $this->adapter; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Creates a database. |
| 73 |
* |
| 74 |
* @param string $name The name of the database. |
| 75 |
* @param array|null $options The options. |
| 76 |
* |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
public function create_database( $name, $options = null ) { |
| 80 |
return $this->adapter->create_database( $name, $options ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Drops a database. |
| 85 |
* |
| 86 |
* @param string $name The name of the database. |
| 87 |
* |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public function drop_database( $name ) { |
| 91 |
return $this->adapter->drop_database( $name ); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Drops a table. |
| 96 |
* |
| 97 |
* @param string $table_name The name of the table. |
| 98 |
* |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
public function drop_table( $table_name ) { |
| 102 |
return $this->adapter->drop_table( $table_name ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Renames a table. |
| 107 |
* |
| 108 |
* @param string $name The name of the table. |
| 109 |
* @param string $new_name The new name of the table. |
| 110 |
* |
| 111 |
* @return bool |
| 112 |
*/ |
| 113 |
public function rename_table( $name, $new_name ) { |
| 114 |
return $this->adapter->rename_table( $name, $new_name ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Renames a column. |
| 119 |
* |
| 120 |
* @param string $table_name The name of the table. |
| 121 |
* @param string $column_name The column name. |
| 122 |
* @param string $new_column_name The new column name. |
| 123 |
* |
| 124 |
* @return bool |
| 125 |
*/ |
| 126 |
public function rename_column( $table_name, $column_name, $new_column_name ) { |
| 127 |
return $this->adapter->rename_column( $table_name, $column_name, $new_column_name ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Adds a column. |
| 132 |
* |
| 133 |
* @param string $table_name The name of the table. |
| 134 |
* @param string $column_name The column name. |
| 135 |
* @param string $type The column type. |
| 136 |
* @param array|string $options The options. |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
public function add_column( $table_name, $column_name, $type, $options = [] ) { |
| 141 |
return $this->adapter->add_column( $table_name, $column_name, $type, $options ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Removes a column. |
| 146 |
* |
| 147 |
* @param string $table_name The name of the table. |
| 148 |
* @param string $column_name The column name. |
| 149 |
* |
| 150 |
* @return bool |
| 151 |
*/ |
| 152 |
public function remove_column( $table_name, $column_name ) { |
| 153 |
return $this->adapter->remove_column( $table_name, $column_name ); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Changes a column. |
| 158 |
* |
| 159 |
* @param string $table_name The name of the table. |
| 160 |
* @param string $column_name The column name. |
| 161 |
* @param string $type The column type. |
| 162 |
* @param array|string $options The options. |
| 163 |
* |
| 164 |
* @return bool |
| 165 |
*/ |
| 166 |
public function change_column( $table_name, $column_name, $type, $options = [] ) { |
| 167 |
return $this->adapter->change_column( $table_name, $column_name, $type, $options ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Adds an index. |
| 172 |
* |
| 173 |
* @param string $table_name The name of the table. |
| 174 |
* @param array|string $column_name The column name. |
| 175 |
* @param array|string $options The options. |
| 176 |
* |
| 177 |
* @return bool |
| 178 |
*/ |
| 179 |
public function add_index( $table_name, $column_name, $options = [] ) { |
| 180 |
return $this->adapter->add_index( $table_name, $column_name, $options ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Removes an index. |
| 185 |
* |
| 186 |
* @param string $table_name The name of the table. |
| 187 |
* @param array|string $column_name The column name. |
| 188 |
* @param array|string $options The options. |
| 189 |
* |
| 190 |
* @return bool |
| 191 |
*/ |
| 192 |
public function remove_index( $table_name, $column_name, $options = [] ) { |
| 193 |
return $this->adapter->remove_index( $table_name, $column_name, $options ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Adds timestamps. |
| 198 |
* |
| 199 |
* @param string $table_name The name of the table. |
| 200 |
* @param string $created_column_name Created at column name. |
| 201 |
* @param string $updated_column_name Updated at column name. |
| 202 |
* |
| 203 |
* @return bool |
| 204 |
*/ |
| 205 |
public function add_timestamps( $table_name, $created_column_name = 'created_at', $updated_column_name = 'updated_at' ) { |
| 206 |
return $this->adapter->add_timestamps( $table_name, $created_column_name, $updated_column_name ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Removes timestamps. |
| 211 |
* |
| 212 |
* @param string $table_name The name of the table. |
| 213 |
* @param string $created_column_name Created at column name. |
| 214 |
* @param string $updated_column_name Updated at column name. |
| 215 |
* |
| 216 |
* @return bool |
| 217 |
*/ |
| 218 |
public function remove_timestamps( $table_name, $created_column_name = 'created_at', $updated_column_name = 'updated_at' ) { |
| 219 |
return $this->adapter->remove_timestamps( $table_name, $created_column_name, $updated_column_name ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Creates a table. |
| 224 |
* |
| 225 |
* @param string $table_name The name of the table. |
| 226 |
* @param array|string $options The options. |
| 227 |
* |
| 228 |
* @return bool|Table |
| 229 |
*/ |
| 230 |
public function create_table( $table_name, $options = [] ) { |
| 231 |
return $this->adapter->create_table( $table_name, $options ); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Execute a query and return the first result. |
| 236 |
* |
| 237 |
* @param string $sql The query to run. |
| 238 |
* |
| 239 |
* @return array |
| 240 |
*/ |
| 241 |
public function select_one( $sql ) { |
| 242 |
return $this->adapter->select_one( $sql ); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Execute a query and return all results. |
| 247 |
* |
| 248 |
* @param string $sql The query to run. |
| 249 |
* |
| 250 |
* @return array |
| 251 |
*/ |
| 252 |
public function select_all( $sql ) { |
| 253 |
return $this->adapter->select_all( $sql ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Execute a query. |
| 258 |
* |
| 259 |
* @param string $sql The query to run. |
| 260 |
* |
| 261 |
* @return bool |
| 262 |
*/ |
| 263 |
public function query( $sql ) { |
| 264 |
return $this->adapter->query( $sql ); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Returns a quoted string. |
| 269 |
* |
| 270 |
* @param string $str The string to quote. |
| 271 |
* |
| 272 |
* @return string |
| 273 |
*/ |
| 274 |
public function quote_string( $str ) { |
| 275 |
return $this->adapter->quote_string( $str ); |
| 276 |
} |
| 277 |
} |
| 278 |
|