forked from CakeDC/migrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrecheckBase.php
116 lines (106 loc) · 2.78 KB
/
PrecheckBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* Copyright 2009 - 2014, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2009 - 2014, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
abstract class PrecheckBase {
/**
* @var CakeMigration
*/
protected $_migration;
/**
* Perform check before field create.
*
* @param string $table
* @param string $field
* @return bool
*/
abstract public function checkAddField($table, $field);
/**
* Perform check before table create.
*
* @param string $table
* @return bool
*/
abstract public function checkCreateTable($table);
/**
* Perform check before table drop.
*
* @param string $table
* @return bool
*/
abstract public function checkDropTable($table);
/**
* Perform check before field drop.
*
* @param string $table
* @param string $field
* @return bool
*/
abstract public function checkDropField($table, $field);
/**
* Check that table exists.
*
* @param string $table
* @return bool
*/
public function tableExists($table) {
$this->_migration->db->cacheSources = false;
$tables = $this->_migration->db->listSources();
return in_array($this->_migration->db->fullTableName($table, false, false), $tables);
}
/**
* Check that field exists.
*
* @param string $table
* @param string $field
* @return bool
*/
public function fieldExists($table, $field) {
if (!$this->tableExists($table)) {
return false;
}
$fields = $this->_migration->db->describe($table);
return !empty($fields[$field]);
}
/**
* Before action precheck callback.
*
* @param $migration
* @param string $type
* @param array $data
* @throws MigrationException
* @return bool
*/
public function beforeAction($migration, $type, $data) {
$this->_migration = $migration;
switch ($type) {
case 'create_table':
return $this->checkCreateTable($data['table']);
case 'drop_table':
return $this->checkDropTable($data['table']);
case 'rename_table':
return $this->checkCreateTable($data['new_name']) && $this->checkDropTable($data['old_name']);
case 'add_field':
return $this->checkAddField($data['table'], $data['field']);
case 'drop_field':
return $this->checkDropField($data['table'], $data['field']);
case 'change_field':
return true;
case 'rename_field':
return $this->checkAddField($data['table'], $data['new_name']) && $this->checkDropField($data['table'], $data['old_name']);
case 'add_index':
case 'drop_index':
return true;
default:
throw new MigrationException($this->_migration, sprintf(
__d('migrations', 'Migration action type (%s) is not one of valid actions type.'), $type
), E_USER_NOTICE);
}
}
}