<?php

/**
 * @file
 * Reinstall install file.
 */

/**
 * Remove migration tables.
 *
 * @param string $table
 *   The base name of the table to drop.
 */
function reinstall_remove_migration_tables($table) {
  $db = \Drupal::database();
  $logger = \Drupal::logger('reinstall');

  $db->schema()->dropTable('migrate_map_' . $table);
  $logger->notice('Table migrate_map_@table dropped', [
    '@table' => $table,
  ]);

  $db->schema()->dropTable('migrate_message_' . $table);
  $logger->notice('Table migrate_message_@table dropped', [
    '@table' => $table,
  ]);
}

/**
 * Implements hook_uninstall().
 */
function reinstall_uninstall() {
  // Get all migrations.
  $manager = \Drupal::service('plugin.manager.config_entity_migration');
  $plugins = $manager->createInstances([]);

  // Remove migration tables.
  foreach ($plugins as $migration) {
    reinstall_remove_migration_tables($migration->id());
  }
}