-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration.php
More file actions
322 lines (277 loc) · 13.4 KB
/
Copy pathmigration.php
File metadata and controls
322 lines (277 loc) · 13.4 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* Created by PhpStorm.
* User: h3nry
* Date: 11/15/14
* Time: 10:37 AM
*/
class Migration{
//Parametrii conexiunii cu baza veche
private static $connection_1 = array('host'=>'127.0.0.1', 'user'=>'root','pass'=>'1', 'dbname'=>'egov');
//Parametrii conexiunii cu baza noua
private static $connection_2 = array('host'=>'127.0.0.1', 'user'=>'root','pass'=>'1', 'dbname'=>'minimal');
//Aici se defineste masivul cu tabelele pe care doriti sa le importati. In caz ca doriti sa importati toate tabelele, stergeti toate valorile din masiv
private static $commonTables = array();
// "sys_Clients","sys_Companies","sys_ContactUser","sys_GeneralValues",
// "sys_MainConfig","sys_TranslateValues","tbl_LoginType","tbl_Priorities","tbl_RelationUserRole",
// "tbl_Translation","tbl_User","tbl_UserCertificate","tbl_config_module","tbl_config_setting_list",
// "tbl_config_setting_value","usr_Group","usr_UserGroup"
//Aici se definesc masivul cu tabele si cimpurile din baza veche si cimpurile din baza noua in care se va copia informatia
private static $oldNewFields = array();
//array('sys_Temporary_Break_Users'=>array('replacer_id'=>'new_replacer_id', 'random_col'=>'new_rand_col'));
private static $differentTables = array();
private static $finalTables = array();
private static $differentColumnsTables = array();
private static $oldAllTables = array();
private static $newAllTables = array();
private static $differentColumns;
private static $finalSql;
private static $sqlInsert;
private static $contor;
private $host;
private $user;
private $pass;
public $dbname;
private $dbh;
private $error;
private $stmt;
public function __construct($ihost, $iuser, $ipass, $idbname){
//Setare valori conexiune
$this->host = $ihost;
$this->user = $iuser;
$this->pass = $ipass;
$this->dbname = $idbname;
// Setare DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Setare optiuni
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Creare noua instanta PDO
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Prelucram erorile
catch(PDOException $e){
print "Eroare!: " . $e->getMessage() . "<br/>";
die();
}
}
public static function createOldConnection(){
$connection = new self(self::$connection_1['host'], self::$connection_1['user'], self::$connection_1['pass'], self::$connection_1['dbname']);
return $connection;
}
public static function createNewConnection(){
$connection = new self(self::$connection_2['host'], self::$connection_2['user'], self::$connection_2['pass'], self::$connection_2['dbname']);
return $connection;
}
public function query($query){
$this->stmt = $this->dbh->prepare($query); //,array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)
}
public function execute(){
return $this->stmt->execute();
}
public function executeParam($values){
return $this->stmt->execute($values);
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function getAllTables() {
$tableList = array();
$result = $this->dbh->query("SHOW FULL TABLES WHERE TABLE_TYPE != 'VIEW'");
while ($row = $result->fetch(PDO::FETCH_NUM)) {
$tableList[] = $row[0];
}
// print_r($tableList);
return $tableList;
}
public function getTableColumns($tableName){
$allColumns = array();
$result = $this->dbh->query("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '".$this->dbname."' AND TABLE_NAME = '".$tableName."'");
while ($row = $result->fetch(PDO::FETCH_NUM)) {
$allColumns[] = $row[0];
}
return $allColumns;
}
public static function returnOldTables(){
return Migration::createOldConnection()->getAllTables();
}
public static function returnNewTables(){
return Migration::createNewConnection()->getAllTables();
}
public static function selectInitialTables(){
//self::$commonTables = Migration::tableReturn();
if(count(self::$commonTables) == 0){
self::$commonTables = array_intersect(self::$oldAllTables, self::$newAllTables);
self::$differentTables = array_diff(self::$oldAllTables, self::$newAllTables);
}
}
public static function searchDifferentColumns($oldDatabase, $newDatabase){
foreach (self::$commonTables as $key => $value) {
if(( count(array_diff($oldTableColumns = $oldDatabase->getTableColumns($value), $newTableColumns = $newDatabase->getTableColumns($value))) == 0 )) {
if(($value!='sys_ActionHistory'))
self::$finalTables[] = $value;
}
else {
self::$differentColumns = array_diff($oldTableColumns = $oldDatabase->getTableColumns($value), $newTableColumns = $newDatabase->getTableColumns($value));
foreach (self::$differentColumns as $key => $val1) {
self::$differentColumnsTables[$value][] = $val1;
}
}
}
}
public static function insertQuery($connection){
$connection->query("set foreign_key_checks=0");
$connection->execute();
self::$finalSql = substr(self::$finalSql, 0, -2);
self::$sqlInsert .= self::$finalSql;
$connection->query(self::$sqlInsert);
$connection->execute();
$connection->query("set foreign_key_checks=1");
$connection->execute();
}
public static function initMigration(){
$oldDatabase = Migration::createOldConnection();
self::$oldAllTables = Migration::returnOldTables();
$newDatabase = Migration::createNewConnection();
self::$newAllTables = Migration::returnNewTables();
self::selectInitialTables();
self::searchDifferentColumns($oldDatabase, $newDatabase);
$k=0;
foreach (self::$commonTables as $keyTable => $table) {
if($table != 'sys_ActionHistory'){
print_r("Au fost inserate datele in tabela ".$table."\n");
if((is_array(self::$oldNewFields)) && (count(self::$oldNewFields)>0) && (in_array($table, array_keys(self::$oldNewFields)))){
$selectDifferentName = array_keys(self::$oldNewFields[$table]);
$excludeColumns = array();
foreach (self::$differentColumnsTables as $key => $val1) {
if($key == $table){
foreach ($val1 as $key => $val2) {
$excludeColumns[] = "'".$val2."'";
}
}
}
$excludeColumnsFinal = array();
foreach (self::$differentColumnsTables as $key => $val1) {
if($key == $table){
foreach ($val1 as $key => $val2) {
$excludeColumnsFinal[] = "'".$val2."'";
}
}
}
$querry = "SELECT `COLUMN_NAME` FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA ='".$oldDatabase->dbname."' AND TABLE_NAME='".$table."' AND COLUMN_NAME NOT IN (".implode(',', $excludeColumns).")";
$oldDatabase->query($querry);
$getColumns = $oldDatabase->resultset();
$finalColumns = array();
foreach ($getColumns as $key => $val2) {
$finalColumns[] = "`".$val2['COLUMN_NAME']."`";
}
if(is_array($selectDifferentName))
$finalColumns = array_merge($finalColumns, $selectDifferentName);
$finalQuery = "SELECT ".implode(',', $finalColumns)." FROM ".$table;
$oldDatabase->query($finalQuery);
}
elseif((in_array($table, array_diff(self::$commonTables, self::$finalTables))) && ($table != 'sys_ActionHistory')){
$excludeColumns = array();
foreach (self::$differentColumnsTables as $key => $val1) {
if($key == $table){
foreach ($val1 as $key => $val2) {
$excludeColumns[] = "'".$val2."'";
}
}
}
$excludeColumnsFinal = array();
foreach (self::$differentColumnsTables as $key => $val1) {
if($key == $table){
foreach ($val1 as $key => $val2) {
$excludeColumnsFinal[] = "'".$val2."'";
}
}
}
$querry = "SELECT `COLUMN_NAME` FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA ='".$oldDatabase->dbname."' AND TABLE_NAME='".$table."' AND COLUMN_NAME NOT IN (".implode(',', $excludeColumns).")";
$oldDatabase->query($querry);
$getColumns = $oldDatabase->resultset();
$finalColumns = array();
foreach ($getColumns as $key => $val2) {//die(var_dump($val2));
$finalColumns[] = "`".$val2['COLUMN_NAME']."`";
}
// die(var_dump(implode(',', $finalColumns)));
$finalQuery = "SELECT ".implode(',', $finalColumns)." FROM ".$table;
$oldDatabase->query($finalQuery);
} else {
$oldDatabase->query("SELECT * FROM ".$table);
}
$selectedData = $oldDatabase->resultset();
$newDatabase->query("set foreign_key_checks=0");
$newDatabase->execute();
$newDatabase->query("TRUNCATE TABLE ".$table);
$newDatabase->execute();
$newDatabase->query("set foreign_key_checks=1");
$newDatabase->execute();
self::$finalSql = "(";
$placeholder = "";
$i=0;
$j=0;
self::$contor = 0;
if(count($selectedData)>0){
foreach ($selectedData as $key => $val) {
//Creem string-ul cu denumirea la field-uri
$buildFields = '';
if($i<1){
if (is_array(array_keys($val))) {
foreach(array_keys($val) as $key => $field) {
if(in_array($table, array_keys(self::$oldNewFields))){
if ($key == 0) {
$buildFields .= '`'.$field.'`';
} elseif(in_array($field, array_keys(self::$oldNewFields[$table]))) {
$buildFields .= ', '.'`'.array_values(self::$oldNewFields[$table])[$j].'`';
$j++;
} else {
$buildFields .= ', '.'`'.$field.'`';
}
} else {
if ($key == 0) {
$buildFields .= '`'.$field.'`';
} else {
$buildFields .= ', '.'`'.$field.'`';
}
}
}
}
self::$sqlInsert = 'INSERT INTO '.$table.' ('.$buildFields.') VALUES ';
$i++;
}
$buildValues2 = '';
if (is_array(array_values($val))) {
foreach(array_values($val) as $key => $value) {
$buildValues2 .= "'".mysql_escape_string($value)."',";
}
}
$buildValues2 = substr($buildValues2, 0, -1);
self::$finalSql .= $buildValues2."),(";
self::$contor++;
}
}
if(count($selectedData)!=0){
$my_file = 'Migration.log';
if($k == 0){
$handle = fopen($my_file, 'w') or die('Imposibil de deschis fisierul: '.$my_file);
}
elseif($k!=0){
$handle = fopen($my_file, 'a') or die('Imposibil de deschis fisierul: '.$my_file);
}
$new_data = 'In tabela '.$table.' au fost inserate '.self::$contor.' inregistrari'."\n";
fwrite($handle, $new_data);
fclose($handle);
$k++;
Migration::insertQuery($newDatabase);
}
}
}
}
}
Migration::initMigration();
?>