Test Failed
Push — master ( fe71c4...38b5f5 )
by Jeroen
02:19
created

ChangeTableEngine::down()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 4
nop 0
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
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
69
			if (!$this->hasTable($table)) {
70
				continue;
71
			}
72
73
			$table = $this->table($table);
74
75
			if (!empty($this->full_text_indexes[$table->getName()])) {
76
				$indexes = $this->full_text_indexes[$table->getName()];
77
				foreach ($indexes as $index => $columns) {
78
					try {
79
						$this->execute("
80
							ALTER TABLE {$prefix}{$table->getName()}
81
							DROP KEY `{$index}`
82
						");
83
					} catch (Exception $e) {
84
						// though schema defines them, some of these keys do not seem to exist
85
					}
86
				}
87
			}
88
89
			$result = $this->execute("
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
				ALTER TABLE {$prefix}{$table->getName()}
91
				ENGINE=InnoDB
92
			");
93
94
			$table->save();
95
		}
96
	}
97
98
	/**
99
	 * Changes table engine to MyISAM
100
	 */
101
	public function down() {
102
103
		$prefix = $this->getAdapter()->getOption('table_prefix');
104
105
		foreach ($this->innodb_tables as $table) {
106
107
			if (!$this->hasTable($table)) {
108
				continue;
109
			}
110
111
			$table = $this->table($table);
112
113
			$this->execute("
114
				ALTER TABLE {$prefix}{$table->getName()}
115
				ENGINE=MyISAM
116
			");
117
118
			if (!empty($this->full_text_indexes[$table->getName()])) {
119
				$indexes = $this->full_text_indexes[$table->getName()];
120
				foreach ($indexes as $index => $columns) {
121
					$columns = implode(',', array_map(function ($e) {
122
						return "'$e'";
123
					}, $columns));
124
125
					$this->execute("
126
						ALTER TABLE {$prefix}{$table->getName()}
127
						ADD FULLTEXT INDEX {$index} ($columns)
128
					");
129
				}
130
			}
131
132
			$table->save();
133
		}
134
135
	}
136
}
137