| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Config\Migrations; |
| 4 |
|
| 5 |
use Yoast\WP\Lib\Migrations\Migration; |
| 6 |
use Yoast\WP\Lib\Model; |
| 7 |
|
| 8 |
/** |
| 9 |
* CreateSEOLinksTable class. |
| 10 |
*/ |
| 11 |
class CreateSEOLinksTable extends Migration { |
| 12 |
|
| 13 |
/** |
| 14 |
* The plugin this migration belongs to. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
*/ |
| 18 |
public static $plugin = 'free'; |
| 19 |
|
| 20 |
/** |
| 21 |
* Migration up. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function up() { |
| 26 |
$table_name = $this->get_table_name(); |
| 27 |
$adapter = $this->get_adapter(); |
| 28 |
|
| 29 |
// The table may already have been created by legacy code. |
| 30 |
// If not, create it exactly as it was. |
| 31 |
if ( ! $adapter->table_exists( $table_name ) ) { |
| 32 |
$table = $this->create_table( $table_name, [ 'id' => false ] ); |
| 33 |
$table->column( |
| 34 |
'id', |
| 35 |
'biginteger', |
| 36 |
[ |
| 37 |
'primary_key' => true, |
| 38 |
'limit' => 20, |
| 39 |
'unsigned' => true, |
| 40 |
'auto_increment' => true, |
| 41 |
], |
| 42 |
); |
| 43 |
$table->column( 'url', 'string', [ 'limit' => 255 ] ); |
| 44 |
$table->column( |
| 45 |
'post_id', |
| 46 |
'biginteger', |
| 47 |
[ |
| 48 |
'limit' => 20, |
| 49 |
'unsigned' => true, |
| 50 |
], |
| 51 |
); |
| 52 |
$table->column( |
| 53 |
'target_post_id', |
| 54 |
'biginteger', |
| 55 |
[ |
| 56 |
'limit' => 20, |
| 57 |
'unsigned' => true, |
| 58 |
], |
| 59 |
); |
| 60 |
$table->column( 'type', 'string', [ 'limit' => 8 ] ); |
| 61 |
$table->finish(); |
| 62 |
} |
| 63 |
if ( ! $adapter->has_index( $table_name, [ 'post_id', 'type' ], [ 'name' => 'link_direction' ] ) ) { |
| 64 |
$this->add_index( $table_name, [ 'post_id', 'type' ], [ 'name' => 'link_direction' ] ); |
| 65 |
} |
| 66 |
|
| 67 |
// Add these columns outside of the initial table creation as these did not exist on the legacy table. |
| 68 |
$this->add_column( $table_name, 'indexable_id', 'integer', [ 'unsigned' => true ] ); |
| 69 |
$this->add_column( $table_name, 'target_indexable_id', 'integer', [ 'unsigned' => true ] ); |
| 70 |
$this->add_column( $table_name, 'height', 'integer', [ 'unsigned' => true ] ); |
| 71 |
$this->add_column( $table_name, 'width', 'integer', [ 'unsigned' => true ] ); |
| 72 |
$this->add_column( $table_name, 'size', 'integer', [ 'unsigned' => true ] ); |
| 73 |
$this->add_column( $table_name, 'language', 'string', [ 'limit' => 32 ] ); |
| 74 |
$this->add_column( $table_name, 'region', 'string', [ 'limit' => 32 ] ); |
| 75 |
|
| 76 |
$this->add_index( $table_name, [ 'indexable_id', 'type' ], [ 'name' => 'indexable_link_direction' ] ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Migration down. |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function down() { |
| 85 |
$this->drop_table( $this->get_table_name() ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Returns the SEO Links table name. |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
private function get_table_name() { |
| 94 |
return Model::get_table_name( 'SEO_Links' ); |
| 95 |
} |
| 96 |
} |
| 97 |
|