This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreplication.install
189 lines (168 loc) · 7.46 KB
/
replication.install
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
<?php
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Field\BaseFieldDefinition;
/**
* implements hook_modules_installed().
*/
function replication_modules_installed($modules) {
if (in_array('replication', $modules)) {
$entity_type_id = 'replication_log';
$entity_types = \Drupal::service('multiversion.manager')->getSupportedEntityTypes();
$entity_type = \Drupal::entityTypeManager()->getStorage($entity_type_id)->getEntityType();
if (in_array($entity_type_id, array_keys($entity_types))) {
\Drupal::service('multiversion.manager')->enableEntityTypes([$entity_type_id => $entity_type]);
}
}
}
/**
* Create revisionable fields in the revision table for replication_log entity
* type if they are missing.
*/
function replication_update_8100() {
$connection = Database::getConnection();
if ($connection instanceof Connection) {
$schema = $connection->schema();
$entity_type_manager = \Drupal::entityTypeManager();
$manager = \Drupal::service('multiversion.manager');
$entity_type_id = 'replication_log';
$entity_type = $entity_type_manager
->getStorage($entity_type_id)
->getEntityType();
if($manager->isEnabledEntityType($entity_type)) {
$id_key = $entity_type->getKey('id');
/** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
$storage = $entity_type_manager->getStorage($entity_type_id);
// Get the tables name used for base table and revision table.
$table_base = ($entity_type->isTranslatable()) ? $entity_type->getDataTable() : $entity_type->getBaseTable();
$table_revision = ($entity_type->isTranslatable()) ? $entity_type->getRevisionDataTable() : $entity_type->getRevisionTable();
/** @var \Drupal\Core\Entity\Sql\TableMappingInterface $table_mapping */
$table_mapping = $storage->getTableMapping();
$tables = $table_mapping->getTableNames();
if (!$table_revision && in_array($entity_type_id . '_field_revision', $tables)) {
$table_revision = $entity_type_id . '_field_revision';
}
elseif (!$table_revision && in_array($entity_type_id . '_revision', $tables)) {
$table_revision = $entity_type_id . '_revision';
}
if ($schema->tableExists($table_base) && $table_revision && $schema->tableExists($table_revision)) {
// Get data from base table.
$table_base_results = $connection->select($table_base)
->fields($table_base)
->execute()->fetchAll();
// Get data from revision table.
$table_revision_results = $connection->select($table_revision)
->fields($table_revision)
->execute()->fetchAll();
if (in_array($table_revision, $tables)) {
$table_revision_fields = $table_mapping->getFieldNames($table_revision);
$entity_field_manager = \Drupal::service('entity_field.manager');
$entity_field_manager->clearCachedFieldDefinitions();
$fields = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
$new_field_storage_definitions = [];
// Loop through all the fields, if the field exists in the new
// revision table mapping and it doesn't exist in the database,
// create the new field.
foreach ($fields as $field_name => $field) {
if (in_array($field_name, $table_revision_fields) && !$schema->fieldExists($table_revision, $field_name)) {
$new_field_storage_definitions[] = $field->getFieldStorageDefinition($field->getName(), $entity_type_id);
}
}
if (!empty($new_field_storage_definitions)) {
// Remove all data from revision table before adding new fields.
$connection->truncate($table_revision)->execute();
foreach ($new_field_storage_definitions as $storage_definition) {
\Drupal::service('field_storage_definition.listener')->onFieldStorageDefinitionCreate($storage_definition);
}
}
// If the revision table has been updated (new field has been added),
// complete new fields with data from base table.
if (!empty($new_field_storage_definitions)) {
$table_base_results_keyed = [];
foreach ($table_base_results as $result) {
if (isset($result->{$id_key})) {
$data = (array) $result;
$table_base_results_keyed[$result->{$id_key}] = $data;
}
}
// For the new created revisionable fields take data from base table.
foreach ($table_revision_results as $result) {
$data = (array) $result;
foreach ($table_revision_fields as $field_name) {
if (!isset($data[$field_name]) && isset($table_base_results_keyed[$result->{$id_key}][$field_name])) {
$data[$field_name] = $table_base_results_keyed[$result->{$id_key}][$field_name];
}
}
// Save the information in the revision table.
$connection->insert($table_revision)
->fields($data)
->execute();
}
}
}
}
}
}
}
/**
* Update ReplicationHistoryItem property definitions.
*
* For compatibility with CouchDB 2.0.0.
*/
function replication_update_8101() {
$connection = Database::getConnection();
$entity_definition_update_manager = Drupal::entityDefinitionUpdateManager();
$entity_type_id = 'replication_log';
// Check if we have updates for replication_log entity type.
if ($entity_definition_update_manager->needsUpdates()) {
$changes = $entity_definition_update_manager->getChangeSummary();
if (in_array($entity_type_id, array_keys($changes))) {
// Set tables to update (history field tables).
$tables_to_update = [
$entity_type_id . '__history',
$entity_type_id . '_revision__history'
];
$tables_data = [];
// Copy content from entity tables.
foreach ($tables_to_update as $table_to_update) {
$tables_data[$table_to_update] = $connection->select($table_to_update)
->fields($table_to_update)
->execute()->fetchAll();
$connection->truncate($table_to_update)->execute();
}
// Apply updates.
$entity_field_manager = \Drupal::service('entity_field.manager');
$entity_field_manager->clearCachedFieldDefinitions();
$storage_definitions = $entity_field_manager->getFieldStorageDefinitions($entity_type_id);
if ($storage_definitions['history']) {
$entity_definition_update_manager->updateFieldStorageDefinition($storage_definitions['history']);
}
// Insert content into updated entity tables.
foreach ($tables_data as $table_name => $table_data) {
foreach ($table_data as $result) {
$data = (array) $result;
// Save the information in the table.
$connection->insert($table_name)
->fields($data)
->execute();
}
}
}
}
}
/**
* Set new configuration for Replication module.
*/
function replication_update_8102() {
\Drupal::configFactory()->getEditable('replication.settings')
->set('changes_limit', 100)
->set('bulk_docs_limit', 100)
->save();
}
/**
* Set the default value for the new replication_execution_limit setting.
*/
function replication_update_8103() {
$replication_settings = \Drupal::configFactory()->getEditable('replication.settings');
$replication_settings->set('replication_execution_limit', 1)->save(TRUE);
}