Migration_47.php
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Migrations; |
| 4 | |
| 5 | use IAWP\Database; |
| 6 | /** @internal */ |
| 7 | class Migration_47 extends \IAWP\Migrations\Step_Migration |
| 8 | { |
| 9 | /** |
| 10 | * @return int |
| 11 | */ |
| 12 | protected function database_version() : int |
| 13 | { |
| 14 | return 47; |
| 15 | } |
| 16 | /** |
| 17 | * @return array |
| 18 | */ |
| 19 | protected function queries() : array |
| 20 | { |
| 21 | return [$this->drop_table_if_exists($this->tables::utm_sources()), $this->create_utm_sources_table(), $this->populate_utm_sources_table(), $this->add_utm_source_id(), $this->populate_utm_source_id_column(), $this->modify_utm_source_id_column(), $this->drop_original_landing_page_title_column(), $this->index_new_column()]; |
| 22 | } |
| 23 | private function create_utm_sources_table() : string |
| 24 | { |
| 25 | return "\n CREATE TABLE {$this->tables::utm_sources()} (\n id BIGINT(20) UNSIGNED AUTO_INCREMENT,\n utm_source VARCHAR(512) NOT NULL,\n PRIMARY KEY (id)\n ) DEFAULT CHARACTER SET {$this->character_set()} COLLATE {$this->collation()};\n "; |
| 26 | } |
| 27 | private function populate_utm_sources_table() : string |
| 28 | { |
| 29 | return "\n INSERT INTO {$this->tables::utm_sources()} (utm_source)\n SELECT DISTINCT LEFT(utm_source, 512)\n FROM {$this->tables::campaigns()} WHERE utm_source IS NOT NULL\n "; |
| 30 | } |
| 31 | private function add_utm_source_id() : string |
| 32 | { |
| 33 | return "\n ALTER TABLE {$this->tables::campaigns()} ADD COLUMN utm_source_id BIGINT(20) UNSIGNED;\n "; |
| 34 | } |
| 35 | private function populate_utm_source_id_column() : string |
| 36 | { |
| 37 | $old_collation = Database::column_collation_for($this->tables::campaigns(), 'utm_source'); |
| 38 | $current_collation = $this->collation(); |
| 39 | $collation_statement = $this->get_collation_statement($current_collation, $old_collation); |
| 40 | return "\n UPDATE\n {$this->tables::campaigns()} AS campaigns\n JOIN {$this->tables::utm_sources()} AS sources ON campaigns.utm_source = sources.utm_source {$collation_statement}\n SET\n campaigns.utm_source_id = sources.id\n "; |
| 41 | } |
| 42 | private function modify_utm_source_id_column() : string |
| 43 | { |
| 44 | return "\n ALTER TABLE {$this->tables::campaigns()} MODIFY COLUMN utm_source_id BIGINT(20) UNSIGNED NOT NULL;\n "; |
| 45 | } |
| 46 | private function drop_original_landing_page_title_column() : string |
| 47 | { |
| 48 | return "\n ALTER TABLE {$this->tables::campaigns()} DROP COLUMN utm_source;\n "; |
| 49 | } |
| 50 | private function index_new_column() : string |
| 51 | { |
| 52 | return "\n ALTER TABLE {$this->tables::campaigns()} ADD INDEX (utm_source_id);\n "; |
| 53 | } |
| 54 | } |
| 55 |