Skip to content
Snippets Groups Projects
Commit 1070e120 authored by Neil Drumm's avatar Neil Drumm :wave:
Browse files

Issue #3381080: Add drush command to delete a project’s metadata

parent 0d88f69d
No related branches found
No related tags found
No related merge requests found
Pipeline #239850 passed with warnings
......@@ -42,13 +42,13 @@ function project_composer_changes($release_category) {
// Generate output.
$actions = [];
$result = db_query('SELECT package, updated FROM {project_composer_update_log} WHERE category = :release_category AND updated > :since', [
$result = db_query('SELECT package, updated, deleted FROM {project_composer_update_log} WHERE category = :release_category AND updated > :since', [
':release_category' => $release_category,
':since' => floor($since / 10000),
]);
foreach ($result as $row) {
$actions[] = [
'type' => 'update',
'type' => $row->deleted ? 'delete' : 'update',
'package' => 'drupal/' . $row->package,
'time' => $row->updated
];
......
......@@ -45,6 +45,13 @@ function project_composer_drush_command() {
'callback' => 'drush_project_composer_validate_constraints',
'aliases' => array('pcvc'),
);
$items['project-composer-delete-project-metadata'] = [
'description' => 'Delete metadata for a project.',
'arguments' => [
'project' => 'Project machine name',
],
];
return $items;
}
......@@ -222,3 +229,94 @@ function drush_project_composer_validate_constraints() {
}
}
}
/**
* Delete metadata for a project.
*/
function drush_project_composer_delete_project_metadata($shortname) {
if (!($project_node = project_load($shortname))) {
drush_log(dt('Could not load project'), 'error');
}
// Get namespace map data.
$result = db_query('SELECT map_id, component_name, package_namespace, category FROM {project_composer_namespace_map} WHERE project_nid = :project_nid', [
':project_nid' => $project_node->nid,
])->fetchAll(PDO::FETCH_ASSOC);
$map_ids = array_column($result, 'map_id');
if (!empty($map_ids)) {
array_unshift($result, array_keys($result[0]));
drush_print(drush_format_table($result));
}
// Get component data.
if ($release_nids = _project_composer_get_release_nids($project_node->nid)) {
$result = db_query('SELECT component_id, component_role, name, package, release_nid, title FROM {project_composer_component} WHERE release_nid IN (:release_nids)', [
':release_nids' => $release_nids,
])->fetchAll(PDO::FETCH_ASSOC);
$component_ids = array_column($result, 'component_id');
if (!empty($component_ids)) {
array_unshift($result, array_keys($result[0]));
drush_print(drush_format_table($result));
}
}
// Get files to delete.
$filepaths = [];
foreach (['legacy', 'current'] as $release_category) {
if ($namespace = project_composer_get_project_namespace($project_node->nid, $release_category, FALSE)) {
foreach ([PROJECT_COMPOSER_METADATA_TAGGED, PROJECT_COMPOSER_METADATA_DEV] as $build_type) {
$filepath = _project_composer_get_metadata_file_dir($release_category) . '/' . project_composer_get_filename($namespace, $build_type);
if (file_exists($filepath)) {
$filepaths[$release_category . '-' . $build_type] = $filepath;
}
}
}
}
if (empty($filepaths)) {
drush_print(dt('Nothing to delete.'));
return;
}
// Confirm.
drush_print(dt('You are about to delete:'));
foreach ($filepaths as $filepath) {
drush_print(dt('- @filepath', ['@filepath' => $filepath]));
}
if (!drush_confirm(dt('Do you want to continue?'))) {
return;
}
// Delete from namespace map & components.
if (!empty($map_ids)) {
db_delete('project_composer_namespace_map')
->condition('map_id', $map_ids)
->execute();
}
if (!empty($component_ids)) {
db_delete('project_composer_component')
->condition('component_id', $component_ids)
->execute();
}
// Delete files.
foreach ($filepaths as $category_type => $filepath) {
file_unmanaged_delete($filepath);
// Clear CDN cache.
$url = url(preg_replace('|public:/|', 'files', $filepath), ['absolute' => TRUE]);
drupalorg_crosssite_fastly_purge_url($url);
// Update project_composer_update_log table.
list($release_category, $build_type) = explode('-', $category_type);
db_merge('project_composer_update_log')
->key([
'category' => $release_category,
'package' => $namespace . ($build_type === PROJECT_COMPOSER_METADATA_DEV ? '~dev' : ''),
])
->fields([
'updated' => time(),
'deleted' => 1,
])
->execute();
}
}
......@@ -1910,6 +1910,7 @@ function project_composer_write_json($release_category, $json, $packagename = NU
])
->fields([
'updated' => filemtime($filepath),
'deleted' => 0,
])
->execute();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment