This repository was 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
/
Copy pathshrinkdb.api.php
102 lines (84 loc) · 2.67 KB
/
shrinkdb.api.php
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
<?php
use \Drush\ShrinkDB\EntityTypeSchemaInterface;
/**
* Enable shrink of extra entity types.
*/
function hook_shrinkdb_entity_types() {
return ['file', 'media'];
}
/**
* Enable shrink of extra dependant entity types.
*/
function hook_shrinkdb_dependant_entity_types() {
$types = [];
drush_bootstrap_to_phase(DRUSH_BOOTSTRAP_DRUPAL_FULL);
if (drush_module_exists('paragraph')) {
$types['paragraph'] = [
'columns' => [
'parent_id' => 'parent_id',
'parent_type' => 'parent_type',
],
];
}
return $types;
}
/**
* Alter enabled entity types.
*/
function hook_shrinkdb_entity_types_alter(&$entity_types) {
$key = array_search('node', $entity_types);
if ($key !== false) {
unset($entity_types[$key]);
}
}
/**
* Alter enabled entity types.
*/
function hook_shrinkdb_dependant_entity_types_alter(&$dependant_entity_types) {
$key = array_search('comment', $dependant_entity_types);
if ($key !== false) {
unset($dependant_entity_types[$key]);
}
}
/**
* Provide extra queries to shrink the database.
*/
function hook_shrinkdb_extra_queries() {
$queries = [];
// Remove files not referenced.
if (drush_module_exists('file')) {
$queries[] = "DELETE fm FROM file_managed fm LEFT JOIN file_usage fu ON fm.fid = fu.fid WHERE fu.fid IS NULL";
}
return $queries;
}
/**
* Provide extra queries for the shrink of an entity type.
*/
function hook_shrinkdb_entity_type_extra_queries($tmp_table, EntityTypeSchemaInterface $entity_type, $days) {
$queries = [];
$id_column = $entity_type->baseTableIdColumn();
// Clean up references in file_usage.
if (drush_module_exists('file')) {
$queries[] = "DELETE t FROM file_usage t INNER JOIN $tmp_table tmp ON t.id = tmp.$id_column WHERE t.type = '" . $entity_type->name() . "'";
}
// Clean up references in taxonomy_index.
if ($entity_type->name() == 'node') {
if (drush_module_exists('taxonomy')) {
$queries[] = "DELETE t FROM taxonomy_index t INNER JOIN $tmp_table tmp ON t.nid = tmp.nid";
}
}
return $queries;
}
/**
* Provide extra queries for the shrink of a dependant entity type.
*/
function hook_shrinkdb_dependant_entity_type_extra_queries($tmp_table, EntityTypeSchemaInterface $entity_type, EntityTypeSchemaInterface $parent_entity_type, $days) {
$queries = [];
if ($entity_type->name() == 'foo') {
$id_column = $entity_type->baseTableIdColumn();
$uuid_column = $entity_type->baseTableUuidColumn();
$queries[] = "DELETE t FROM custom_indexed_by_id t INNER JOIN $tmp_table tmp ON t.id = tmp.$id_column";
$queries[] = "DELETE t FROM custom_indexed_by_uuid t INNER JOIN $tmp_table tmp ON t.uuid = tmp.$uuid_column";
}
return $queries;
}