Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

migrations/20170728040000_change_table_engine.php (1 issue)

1
<?php
2
3
use Phinx\Db\Adapter\MysqlAdapter;
4
use Phinx\Migration\AbstractMigration;
5
6
class ChangeTableEngine extends AbstractMigration {
7
8
	private $innodb_tables = [
9
		'access_collection_membership',
10
		'access_collections',
11
		'annotations',
12
		'api_users',
13
		'config',
14
		'entities',
15
		'entity_relationships',
16
		'entity_subtypes',
17
		'groups_entity',
18
		'metadata',
19
		'metastrings',
20
		'objects_entity',
21
		'private_settings',
22
		'queue',
23
		'river',
24
		'sites_entity',
25
		'system_log',
26
		'users_entity',
27
		'users_remember_me_cookies',
28
		'users_sessions',
29
	];
30
31
	private $full_text_indexes = [
32
		'groups_entity' => [
33
			'name_2' => [
34
				'name',
35
				'description'
36
			],
37
		],
38
		'objects_entity' => [
39
			'title' => [
40
				'title',
41
				'description'
42
			],
43
		],
44
		'sites_entity' => [
45
			'name' => [
46
				'name',
47
				'description',
48
				'url'
49
			],
50
		],
51
		'users_entity' => [
52
			'name' => ['name'],
53
			'name2' => [
54
				'name',
55
				'username'
56
			],
57
		],
58
	];
59
60
	/**
61
	 * Changes table engine to InnoDb
62
	 */
63
	public function up() {
64
65
		$prefix = $this->getAdapter()->getOption('table_prefix');
66
67
		foreach ($this->innodb_tables as $table) {
68
			if (!$this->hasTable($table)) {
69
				continue;
70
			}
71
72
			$table = $this->table($table);
73
74
			if (!empty($this->full_text_indexes[$table->getName()])) {
75
				$indexes = $this->full_text_indexes[$table->getName()];
76
				foreach ($indexes as $index => $columns) {
77
					try {
78
						$this->execute("
79
							ALTER TABLE {$prefix}{$table->getName()}
80
							DROP KEY `{$index}`
81
						");
82
					} catch (Exception $e) {
83
						// though schema defines them, some of these keys do not seem to exist
84
					}
85
				}
86
			}
87
88
			$result = $this->execute("
0 ignored issues
show
The assignment to $result is dead and can be removed.
Loading history...
89
				ALTER TABLE {$prefix}{$table->getName()}
90
				ENGINE=InnoDB
91
			");
92
93
			$table->save();
94
		}
95
	}
96
97
	/**
98
	 * Changes table engine to MyISAM
99
	 */
100
	public function down() {
101
102
		$prefix = $this->getAdapter()->getOption('table_prefix');
103
104
		foreach ($this->innodb_tables as $table) {
105
			if (!$this->hasTable($table)) {
106
				continue;
107
			}
108
109
			$table = $this->table($table);
110
111
			$this->execute("
112
				ALTER TABLE {$prefix}{$table->getName()}
113
				ENGINE=MyISAM
114
			");
115
116
			if (!empty($this->full_text_indexes[$table->getName()])) {
117
				$indexes = $this->full_text_indexes[$table->getName()];
118
				foreach ($indexes as $index => $columns) {
119
					$columns = implode(',', array_map(function ($e) {
120
						return "'$e'";
121
					}, $columns));
122
123
					$this->execute("
124
						ALTER TABLE {$prefix}{$table->getName()}
125
						ADD FULLTEXT INDEX {$index} ($columns)
126
					");
127
				}
128
			}
129
130
			$table->save();
131
		}
132
133
	}
134
}
135