function config_sync_update_8001 in Configuration Synchronizer 8.2
Migrate snapshot data from table to configuration entities.
File
- ./
config_sync.install, line 27 - Install, update and uninstall functions for the config_sync module.
Code
function config_sync_update_8001() {
if (!\Drupal::moduleHandler()
->moduleExists('config_snapshot')) {
// Install new dependency.
try {
\Drupal::service('module_installer')
->install([
'config_snapshot',
], FALSE);
} catch (Exception $e) {
throw new UpdateException('The Configuration Snapshot module is required but was not found.');
}
}
$provider_storage = \Drupal::service('config_provider.storage');
$table_name = 'config_sync_snapshot_extension';
$database = \Drupal::database();
$database_schema = $database
->schema();
if ($database_schema
->tableExists($table_name)) {
// Load data from old table storage.
$legacy_data = $database
->query('SELECT * FROM {' . $table_name . '}')
->fetchAll();
$data = [];
foreach ($legacy_data as $item) {
// Data are serialized.
$data[$item->collection][$item->name] = unserialize($item->data);
}
// The config item data we've read in doesn't include information on the
// providing extension. To determine this, our best proxy indicator is the
// config that is currently provided.
/* @var \Drupal\config_sync\Plugin\SyncConfigCollectorInterface $config_collector */
$config_collector = \Drupal::service('config_sync.collector');
$installed_extensions = \Drupal::config('core.extension');
// Iterate through installed extensions.
foreach ([
'module',
'theme',
] as $type) {
if ($installed_type = $installed_extensions
->get($type)) {
// For each extension, determine what configuration it currently provides.
foreach (array_keys($installed_type) as $name) {
$pathname = drupal_get_filename($type, $name);
$extension = new Extension(\Drupal::root(), $type, $pathname);
$extensions = [
$name => $extension,
];
$config_collector
->addInstallableConfig($extensions);
// Try loading the snapshot from configuration. We need to support
// snapshots that may have been created when we installed the
// config_snapshot module or when other modules were installed after
// code was updated but prior to this update.
$config_snapshot = ConfigSnapshot::load(ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET . ".{$type}.{$name}");
// If not found, create a fresh snapshot object for this extension.
if (!$config_snapshot) {
$config_snapshot = ConfigSnapshot::create([
'snapshotSet' => ConfigSyncSnapshotterInterface::CONFIG_SNAPSHOT_SET,
'extensionType' => $type,
'extensionName' => $name,
]);
}
foreach (array_keys($data) as $collection) {
if ($provider_storage
->getCollectionName() !== $collection) {
$provider_storage = $provider_storage
->createCollection($collection);
}
if ($config_names = $provider_storage
->listAll()) {
// If the currently-provided item has a previous snapshot value,
// set that for the new shapshot.
foreach ($config_names as $config_name) {
if (isset($data[$collection][$config_name])) {
$config_snapshot
->setItem($collection, $config_name, $data[$collection][$config_name]);
}
}
$config_snapshot
->save();
}
}
}
}
}
// Drop the obsolete table.
$database_schema
->dropTable($table_name);
}
// Also drop a second obsolete table if present.
$table_name = 'config_sync_snapshot_active';
if ($database_schema
->tableExists($table_name)) {
$database_schema
->dropTable($table_name);
}
}