This repository has been archived by the owner on Jul 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shrinkdb.drush.inc
124 lines (106 loc) · 3.13 KB
/
shrinkdb.drush.inc
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
<?php
require __DIR__ . '/vendor/autoload.php';
/**
* Implements hook_drush_command().
*/
function shrinkdb_drush_command() {
$items = [];
$items['shrinkdb'] = [
'description' => 'Shrink the database size by wiping content older than given days.',
'options' => [
'days' => [
'description' => 'Age (in days) of the contents to preserve. 15 by default.',
'example-value' => '15',
],
],
];
return $items;
}
/**
* Implements hook_drush_help_alter().
*
* Adds shrinkdb option to sql-sanitize.
*/
function shrinkdb_drush_help_alter(&$command) {
if ($command['command'] == 'sql-sanitize') {
$shrinkdb_command = shrinkdb_drush_command()['shrinkdb'];
$command['options']['shrink-db'] = $shrinkdb_command['description'];
foreach ($shrinkdb_command['options'] as $key => $value) {
$command['options']['shrink-db-' . $key] = $value;
}
}
}
/**
* Implements command callback for 'shrinkdb'.
*/
function drush_shrinkdb() {
$db_key = 'default';
$days = drush_get_option('days', 15);
$sql = drush_sql_get_class();
if (!drush_get_context('DRUSH_AFFIRMATIVE')) {
$db_spec = $sql->db_spec();
if (!drush_confirm(dt('Do you really want to wipe content older than !num days in the database !db?', array('!num' => $days, '!db' => $db_spec['database'])))) {
return drush_user_abort();
}
}
$queries = drush_shrinkdb_get_queries($db_key, $days);
if (drush_get_context('DRUSH_SIMULATE')) {
print $queries;
}
else {
$result = $sql->query($queries);
if (!$result) {
throw new \Exception(dt('Wipe content query failed.'));
}
}
}
/**
* Implements hook_drush_sql_sync_sanitize().
*/
function shrinkdb_drush_sql_sync_sanitize($db_key) {
if (!drush_get_option('shrink-db', FALSE)) {
return;
}
$days = drush_get_option('shrink-db-days', 15);
$queries = drush_shrinkdb_get_queries($db_key, $days);
drush_sql_register_post_sync_op('shrinkdb', dt('Wipe content older than !num days', array('!num' => $days)), $queries);
}
/**
* Generate the queries.
*/
function drush_shrinkdb_get_queries($db_key, $days) {
// We want to check if modules are enabled. This requires a full bootstrap.
drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_FULL);
try {
$class = '\Drush\ShrinkDB\Query\EntityType';
$args = [$db_key, $days];
$shrinkdb = drush_get_class($class, $args);
}
catch (\Exception $e) {
// @todo@ For some reason drush_set_error() triggers a fatal error:
// Drupal\Core\DependencyInjection\ContainerNotInitializedException: \Drupal::$container is not initialized yet.
return drush_set_error('SHRINKDB_ERROR', 'shrinkdb: ' . $e->getMessage());
}
return $shrinkdb->queries();
}
/**
* Implements hook_shrinkdb_entity_types().
*/
function shrinkdb_shrinkdb_entity_types() {
return ['node'];
}
/**
* Implements hook_shrinkdb_dependant_entity_types().
*/
function shrinkdb_shrinkdb_dependant_entity_types() {
$types = [];
if (drush_module_exists('comment')) {
$types['comment'] = [
'columns' => [
'parent_id' => 'entity_id',
'parent_type' => 'entity_type',
],
];
}
return $types;
}