-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNetCommonsMigration.php
More file actions
210 lines (191 loc) · 5.49 KB
/
NetCommonsMigration.php
File metadata and controls
210 lines (191 loc) · 5.49 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* NetCommonsMigration
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('CakeMigration', 'Migrations.Lib');
App::uses('I18n', 'I18n');
App::uses('Space', 'Rooms.Model');
/**
* NetCommonsMigration
*
* @author Shohei Nakajima <nakajimashouhei@gmail.com>
* @package NetCommons\NetCommons\Config\Migration
*/
class NetCommonsMigration extends CakeMigration {
/**
* plugin data
*
* @var array $migration
*/
public $records = array();
/**
* Run migration
*
* @param string $direction Direction of migration process (up or down)
* @return bool Status of the process
* @throws MigrationException
*/
public function run($direction) {
if (class_exists('Space') &&
isset($this->records['Plugin']) && Hash::extract($this->records['Plugin'], '{n}[type=1]')) {
Space::getInstance('Space', ['testing' => ($this->connection === 'test')]);
Space::getInstance('Room', ['testing' => ($this->connection === 'test')]);
}
return parent::run($direction);
}
/**
* This method will invoke the before/afterAction callbacks, it is good when
* you need track every action.
*
* @param string $callback Callback name, beforeMigration, beforeAction, afterAction
* or afterMigration.
* @param string $type Type of action. i.e: create_table, drop_table, etc.
* Or also can be the direction, for before and after Migration callbacks
* @param array $data Data to send to the callback
* @return void
* @throws MigrationException
*/
protected function _invokeCallbacks($callback, $type, $data = array()) {
try {
parent::_invokeCallbacks($callback, $type, $data);
} catch (Exception $ex) {
CakeLog::error($ex);
throw $ex;
}
}
/**
* Generate a instance of model for given options
*
* @param string $name Model name to be initialized
* @param string $table Table name to be initialized
* @param array $options Model constructor options
* @return Model
*/
public function generateModel($name, $table = null, $options = array()) {
$Model = parent::generateModel($name, $table, $options);
$Model->unbindModel(array(
'belongsTo' => array('TrackableCreator', 'TrackableUpdater')
), false);
$Model->setDataSource($this->connection);
return $Model;
}
/**
* Update model records
*
* @param string $model model name to update
* @param array $argRecords records to be stored
* @param bool $clear 初期化するかどうか
* @return bool Should process continue
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function updateRecords($model, $argRecords, $clear = false) {
if (isset($argRecords['class']) && isset($argRecords['records'])) {
$this->loadModels([
$model => $argRecords['class']
]);
$objModel = $this->$model;
$records = $argRecords['records'];
} else {
$records = $argRecords;
$objModel = $this->generateModel($model);
}
if ($clear) {
if (!$objModel->deleteAll(array('1 = 1'), false, false)) {
return false;
}
}
foreach ($records as $record) {
foreach ($record as $field => $foreign) {
if (! is_array($foreign)) {
continue;
}
foreach ($foreign as $model2 => $options) {
$objModel2 = $this->generateModel($model2);
$result = $objModel2->find('first', $options);
$record[$field] = Hash::get($result, $options['fields']);
continue;
}
}
$objModel->create();
if (!$objModel->save($record, false)) {
return false;
}
}
return true;
}
/**
* Delete model records
*
* @param string $model model name to delete
* @param array $records records to be stored
* @param string $key 削除条件項目
* @return bool Should process continue
*/
public function deleteRecords($model, $records, $key = 'id') {
$Model = $this->generateModel($model);
foreach ($records as $record) {
$id = Hash::get($record, $key);
if (!$id) {
continue;
}
$conditions = array($key => $id);
if (!$Model->deleteAll($conditions, false, false)) {
return false;
}
}
return true;
}
/**
* Load models
*
* @param array $models models to load
* @return void
*/
public function loadModels(array $models = []) {
foreach ($models as $model => $class) {
if ($class === 'Rooms.Space') {
Space::getInstance('Space', ['testing' => ($this->connection === 'test')]);
Space::getInstance('Room', ['testing' => ($this->connection === 'test')]);
}
$this->$model = ClassRegistry::init([
'class' => $class,
'testing' => ($this->connection === 'test')
], true);
//$this->$model->setDataSource($this->connection);
$this->$model->useDbConfig = $this->connection;
if ($this->connection !== 'test') {
$this->$model->setMasterDataSource();
}
//if ($this->$model->useDbConfig !== 'test') {
// $this->$model->setDataSource($source);
//}
}
}
/**
* データ投入のマイグレーションupの更新と,downの削除
*
* @param string $direction Direction of migration process (up or down)
* @return bool Should process continue
*/
public function updateAndDeleteRecords($direction) {
foreach ($this->records as $model => $records) {
if ($direction === 'up') {
if (!$this->updateRecords($model, $records)) {
return false;
}
} elseif ($direction === 'down') {
if (!$this->deleteRecords($model, $records)) {
return false;
}
}
}
return true;
}
}