-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasy_migration.php
More file actions
211 lines (174 loc) · 6.1 KB
/
Easy_migration.php
File metadata and controls
211 lines (174 loc) · 6.1 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
211
<?php
/**
* Created by PhpStorm.
* User: Bapi Roy
* Date: 9/11/17
* Time: 3:09 AM
* version: 0.1.0
*/
class Easy_migration
{
const BASEPATH = APPPATH.'migrations/';
private $ciObject;
public function __construct()
{
$this->ciObject =& get_instance();
}
/**
* Run migration with the help of fiels in migrations directory
* @param string $tableName - run migration only for specified table
* @param int $version - 0 equal to latest version
*/
public function migrate($tableName='', $version=0)
{
if((empty($tableName)) && ($version <= 0)){
$this->runForLatest();
}else{
$this->runFor();
}
}
/**
* For single migration
* @param string $className
* @param int $version
* @throws Exception
*/
private function runFor($className='', $version=0){
if($version > 0){
// Get the respective JSON file with version number
$file = self::BASEPATH.'json/'.str_pad($version, 3, "0", STR_PAD_LEFT).'_'.$className.'.json';
if(!file_exists($file)){
throw new Exception("JSON file does not exist.");
}
// Get Field Array from JSON file
$jsonData = json_decode(file_get_contents($file));
// Collect fields in array format
$fields = $jsonData['fields'];
// Collect table attributes
$tableAttributes = $jsonData['tableAttributes'];
# Create JSON file under migrate directory with incremental value
$this->createJsonFile(
$fields,
ucfirst($className),
$tableAttributes
);
# Get class name as Table name & Create Table
$this->migrateTable($className, $tableAttributes, $fields);
}else{
// Get data from migration class
include_once self::BASEPATH.$className.'.php';
$objectClassName = ucfirst($className);
$object = new $objectClassName;
// Execute the fields method
$object->fields();
// Collect fields in array format
$fields = $object->getFields();
// Collect table attributes
$tableAttributes = $this->getTableAttributes($object);
# Create JSON file under migrate directory with incremental value
$this->createJsonFile(
$fields,
$objectClassName,
$tableAttributes
);
# Get class name as Table name & Create Table
$this->migrateTable($className, $tableAttributes, $fields);
}
}
/**
* Run when migration run for Latest file
*/
private function runForLatest(){
// Load all files and class from Migration directory
$classFiles = array_diff(scandir(self::BASEPATH, 1), array('..', '.','json'));
foreach ($classFiles as $className){
// Load Class
require(self::BASEPATH.$className);
$objectClassName = str_ireplace(".php", "", $className);
$object = new $objectClassName;
// Execute the fields method
$object->fields();
// Collect fields in array format
$fields = $object->getFields();
// Collect table attributes
$tableAttributes = $this->getTableAttributes($object);
# Create JSON file under migrate directory with incremental value
$this->createJsonFile(
$fields,
$objectClassName,
$tableAttributes
);
# Get class name as Table name & Create Table
$this->migrateTable($objectClassName, $tableAttributes, $fields);
}
}
/**
* Actual method that create or update table
* @param string $tableName
* @param string $tableAttributes
* @param $fields
*/
private function migrateTable($tableName='', $tableAttributes='', $fields)
{
$this->ciObject->dbforge->drop_table($tableName, true);
// @TODO - make it more versatile to handle add, delete column
$this->ciObject->dbforge->add_field($fields);
$this->ciObject->dbforge->create_table(
strtolower($tableName),
true,
$tableAttributes
);
}
/**
* Save fields into JSON file with incremented version number
* @param array $fields
* @param string $className
* @param array $tableAttributes
*/
private function createJsonFile(array $fields, $className='', array $tableAttributes)
{
// Search for last File
$jsonFiles = array_diff(
scandir(self::BASEPATH.'json', SCANDIR_SORT_ASCENDING),
['..', '.']
);
$latestVersion = 0;
foreach ($jsonFiles as $file){
if (strpos($file, $className) !== false) {
//File found, get latest version
$fileNameParts = explode('_', $file);
$currentVersion = (int) $fileNameParts[0];
if($currentVersion > $latestVersion){
$latestVersion = $currentVersion;
}
}
}
// New Version
$newVersion = $latestVersion + 1;
$newFile = self::BASEPATH.'json/'.str_pad($newVersion, 3, "0", STR_PAD_LEFT).'_'.strtolower($className).'.json';
$jsonData = json_encode(
[
'fields'=> $fields,
'tableAttributes' => $tableAttributes
]
);
file_put_contents($newFile, $jsonData);
}
/**
* Collect table attributes from migration object and return as array
* @param $object
* @return array
*/
private function getTableAttributes($object)
{
$return = [
'ENGINE' => $object->getStorageEngine(),
'CHARACTER SET' => $object->getCharset(),
'COLLATE' => $object->getCollation(),
];
if(!empty($object->getComment())){
$return['COMMENT'] = "'".$object->getComment()."'";
}
return $return;
}
}