-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigration.module
More file actions
513 lines (450 loc) · 13.6 KB
/
migration.module
File metadata and controls
513 lines (450 loc) · 13.6 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php // $Id$
/**
* @file
* The Migration module, which helps keep databases in sync.
*
* The Migration module is intended to help keep development, staging and
* production databases in sync. This is done by collecting queries and
* formatting them as a hook_update_N function for running on the target
* site.
*/
/**
* Implementation of hook_perm().
*/
function migration_perm() {
return array('administer migration', 'view query monitor start/stop link');
}
/**
* Implementation of hook_menu().
*/
function migration_menu() {
$items = array();
$items['admin/settings/migration'] = array(
'title' => 'Migration',
'description' => 'Settings for the Migration module',
'page callback' => 'drupal_get_form',
'page arguments' => array('migration_settings_form'),
'access arguments' => array('administer migration'),
'file' => 'migration.admin.inc',
);
$items['admin/settings/migration/settings'] = array(
'title' => 'Settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
);
$items['admin/settings/migration/tables'] = array(
'title' => 'Monitored tables',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('migration_tables_list_form'),
'access arguments' => array('administer migration'),
'file' => 'migration.admin.inc',
'weight' => 1,
);
$items['admin/settings/migration/variables'] = array(
'title' => 'Filter variables',
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array('migration_variables_list_form'),
'access arguments' => array('administer migration'),
'file' => 'migration.admin.inc',
'weight' => 1,
);
$items['migration/start'] = array(
'title' => 'Start monitoring',
'type' => MENU_CALLBACK,
'page callback' => 'migration_monitor_control',
'page arguments' => array('start'),
'access arguments' => array('administer migration'),
'file' => 'migration.admin.inc',
);
$items['migration/stop'] = array(
'title' => 'Stop monitoring',
'type' => MENU_CALLBACK,
'page callback' => 'migration_monitor_control',
'page arguments' => array('stop'),
'access arguments' => array('administer migration'),
'file' => 'migration.admin.inc',
);
return $items;
}
/**
* Implementation of hook_exit().
*
* Collect all queries and stash them in a query file.
*/
function migration_exit($destination = NULL) {
global $queries;
if (variable_get('dev_query', FALSE) && is_array($queries)) {
$page_queries = migration_global_queries_processor($queries);
$valid_queries = migration_filter_queries($page_queries);
foreach ($valid_queries as $query_struct) {
$querystrings[] = $query_struct['query'];
// TODO: for debugging - get rid of this later
//$funcquery[$query_struct['function'] . $query_struct['query']] = $query_struct['function'] . $query_struct['query'];
//$functions[$query_struct['function']] = $query_struct['function'];
//$usernames[$query_struct['username']] = $query_struct['username'];
}
// TODO: Consider using $_GLOBAL['migration_query_monitor'].
if (variable_get('migration_query_monitor', FALSE)) {
// Log the sql strings to disk.
if (is_array($querystrings)) {
if (migration_file_append_data(implode(";\n", $querystrings) . ";", 'queries.sql')) {
$output .= t('!querystrings Queries added to file: !file', array('!file' => realpath(migration_get_filename('queries.sql')), '!querystrings' => count($querystrings)));
}
else {
$output .= t('Error saving sql file.');
}
}
}
if (variable_get('migration_hook_update_pageviews', FALSE)){
$output .= migration_format_update_hook($queries);
}
drupal_set_message($output, 'status');
}
}
/**
* Get the relative path to the query file.
*
* @param $file
* The name of the file for which to get the path.
* @return
* The path to the query file.
*/
function migration_get_filename($file) {
$filename = variable_get('migration_directory_path', file_directory_path() . '/migration') . '/' . $file;
return $filename;
}
/**
* Extracts the queries, user names, and functions called.
*
* @return
* An array containing the query data.
*/
function migration_global_queries_processor($queries) {
foreach ($queries as $query) {
// Regex explain: Using % instead of / for readability. The 's' option
// extends the match past single lines and query is sometimes multiline.
preg_match("%(?<function>.*)\n\/\* (?<username>.*) : .*\*\/ (?<query>.*)%s", $query[0], $match);
$output[] = array(
'function' => $match['function'],
'username' => $match['username'],
'query' => str_replace("\n", ' ', trim($match['query'])), // Strip whitespace.
);
}
return $output;
}
/**
* Appends collected queries to the specified file.
*
* @param $input
* The query data to append to the file.
* @param $filename
* The name of the file to append the data.
* @return
* A string containing the resulting filename or 0 on error.
*/
function migration_file_append_data($input, $filename) {
$fullpath = getcwd() . '/' . variable_get('migration_directory_path', file_directory_path() . '/migration');
if (!file_exists($fullpath . '/' . $filename)) {
$file = file_create_filename($filename, $fullpath);
}
else {
$file = $fullpath . '/' . $filename;
}
$data = file_get_contents($file);
$data .= $input . "\n";
$return = file_save_data($data, $file, FILE_EXISTS_REPLACE);
return $return;
}
/**
* Filters out unwanted queries.
*
* @param $query_struct
* An array containing all collected queries, function names and user names.
* @return
* An array containing only queries we want to keep.
*/
function migration_filter_queries($query_struct) {
$filtered_struct = array();
$filter_selects = $filter_tables = $filter_vars = $filter_functions = $not_filtered = 0;
foreach ($query_struct as $query) {
// Filter out SELECT and SHOW queries.
if (migration_is_select_or_show($query['query'])) {
$filter_selects++;
continue;
}
// Filter by table name.
$tablename = migration_get_tablename($query['query']);
if (migration_is_table_to_filter($tablename)) {
$filter_tables++;
continue;
}
// Filter by function.
if (migration_is_function_to_filter($query['function'])) {
$filter_functions++;
continue;
}
if ($tablename == 'variable') {
$variablename = migration_get_variable_from_query($query['query']);
if (migration_is_variable_to_filter($variablename)) {
$filter_vars++;
continue;
}
}
$not_filtered++;
$filtered_struct[] = $query;
}
_migration_debug("SELECTs: $filter_selects", 'status');
_migration_debug("Tables: $filter_tables", 'status');
_migration_debug("Functions: $filter_functions", 'status');
_migration_debug("Variables: $filter_vars", 'status');
_migration_debug("Good: $not_filtered", 'status');
$total = $not_filtered + $filter_selects + $filter_functions + $filter_vars + $filter_tables;
_migration_debug("Total: $total", 'status');
return $filtered_struct;
}
/**
* Finds the variable name in a query to the variables table.
*
* @param $query
* The query from which to extract the variable name.
* @return
* The variable name.
*/
function migration_get_variable_from_query($query) {
if (stripos($query, 'UPDATE') === 0 ) {
preg_match("|.*WHERE name = '(.*)'.*|", $query, $match);
}
elseif (stripos($query, 'DELETE') === 0 ) {
preg_match("|.*WHERE name = '(.*)'.*|", $query, $match);
}
elseif (stripos($query, 'INSERT') === 0 ) {
preg_match("|.*VALUES \('(.*?)'.*|", $query, $match);
}
else {
_migration_debug("Failed to get variable from query: $query", 'error');
}
return $match[1];
}
/**
* Finds the table name in a query.
*
* @param $query
* The query from which to extract the table name.
* @return
* The table name.
*/
function migration_get_tablename($query) {
//This is currently failing due to "DELETE FROM cache" - I think.. this is getting passed blank querystrings;
preg_match("/(INSERT INTO|DELETE FROM|ALTER TABLE|UPDATE|DROP VIEW|CREATE VIEW|DROP TABLE|CREATE TABLE|LOCK TABLES|CREATE DOMAIN|CREATE INDEX|CREATE UNIQUE INDEX|CREATE SEQUENCE|DROP INDEX) (?<tablename>.*?)($| ).*/", $query, $match[]);
if (strlen($match[0]['tablename']) < 2) {
_migration_debug("Failed to get table name from query: $match[0]", 'error');
}
else {
return $match[0]['tablename'];
}
}
/**
* Allows filtering by variable name.
*
* @param $variable_name
* The variable name to check.
* @return
* Boolean value specifying whether or not keep the query based on the variable
* name.
*/
function migration_is_variable_to_filter($variable_name) {
$skip_names = variable_get('migration_variable_name_filter_array', migration_get_default_variable_array());
if (in_array($variable_name, $skip_names)) {
_migration_debug("Variable filtered: $variable_name", 'notice');
return TRUE;
}
else {
_migration_debug("Variable not filtered: $variable_name", 'notice');
return FALSE;
}
}
/**
* Allows filtering by table name.
*
* @param $table_name
* The table name to filter.
* @return
* Boolean value specifying whether or not keep the query.
*/
function migration_is_table_to_filter($table_name) {
$skip_tables = variable_get('migration_database_table_settings', migration_get_default_tables());
if ($skip_tables[$table_name] === $table_name) {
_migration_debug("Table filtered: $table_name", 'notice');
return TRUE;
}
else {
_migration_debug("Table not filtered: $table_name", 'notice');
return FALSE;
}
}
/**
* Allows filtering by function name.
*
* @param $function
* The function name to filter.
* @return
* Boolean value specifying whether or not to keep the query.
*/
function migration_is_function_to_filter($function) {
$skip_functions = array(
'system_theme_data',
'module_rebuild_cache',
);
if (in_array($function, $skip_functions)) {
_migration_debug("Function filtered: $function", 'notice');
return TRUE;
}
else {
_migration_debug("Function not filtered: $function", 'notice');
return FALSE;
}
}
/**
* Filters queries that begin with certain keywords.
*
* @param $query
* The query to test.
* @return
* TRUE if query begins with a certain keyword.
*/
function migration_is_select_or_show($query) {
if (stripos($query, 'SELECT') === 0 ||
stripos($query, 'SHOW') === 0) {
return TRUE;
}
return FALSE;
}
/**
* Formats a hook_update_N function.
*
* @param $queries
* An array of all queries collected.
* @return
* HTML displaying the formatted hook_update_N function.
*/
function migration_format_update_hook($queries) {
global $queries;
$count = migration_get_next_id();
$output = '<code><pre>';
$output .= "function mymodule_update_$count() {\n";
$output .= ' $sql = array();';
foreach ($queries as $data) {
$bits = explode("\n", $data[0]);
$query = preg_replace('|\/\*\s[a-z\s:_]*\*\/\s|', '', $bits[1]);
//$process = migration_keepquery($query);
if ($process) {
$output .= "\n";
$output .= " /* $bits[0] */\n";
$output .= ' $sql[] = update_sql($query);';
}
}
$output .= "\n return \$sql;\n}\n";
$output .= '</pre></code>';
return $output;
}
/**
* Retrieves the number for the next update used in hook_update_N.
*/
function migration_get_next_id() {
// TODO: Retrieve next update N from system table.
return 6000;
}
/**
* Implementation of hook_init().
*
* Adds the JavaScript and CSS files.
*/
function migration_init() {
$path = drupal_get_path('module', 'migration');
drupal_add_css($path . '/migration.css');
drupal_add_js($path . '/migration.js');
}
/**
* Implementation of hook_footer().
*
* Adds the start/stop link to the page.
*/
function migration_footer($main = 0) {
if (user_access('view query monitor start/stop link')) {
$output = '<div id="migration-monitor-switch">';
if (variable_get('migration_query_monitor', FALSE)) {
$output .= '<a href="#" id="migration-stop-monitor">Stop Query Monitor</a>';
}
else {
$output .= '<a href="#" id="migration-start-monitor">Start Query Monitor</a>';
}
$output .= '</div>';
return $output;
}
}
/**
* Provides default values for variables to exclude from collection.
*
* @return
* An array of variable names.
*/
function migration_get_default_variable_array() {
$variables = array(
'javascript_parsed',
'cache_rebuild',
'cache_flush',
'menu_rebuild_needed',
'admin_menu_rebuild_links',
'menu_masks',
'menu_expanded',
'migration_query_monitor',
'migration_filter_mode',
'migration_database_tables',
'migration_variable_patterns',
);
return $variables;
}
/**
* Provides default values for tables to exclude from collection.
*
* @return
* An array of table names.
*/
function migration_get_default_tables() {
$tables = array(
'cache_views',
'cache',
'cache_content',
'cache_menu',
'cache_admin_menu',
'cache_update',
'cache_block',
'cache_filter',
'cache_page',
'cache_calendar_ical',
'cache_form',
'history',
'menu_router',
'menu_links',
'watchdogcache',
'watchdog',
'sessions',
'search_dataset',
'search_index',
'search_keywords_log',
'search_total',
'accesslog',
'devel_queries',
'devel_times',
'menu_router',
'menu_links',
);
return array_combine($tables, $tables);
}
function _migration_debug($message = '', $type = '') {
if (variable_get('migration_debug', FALSE)) {
drupal_set_message($message, $type);
}
}