Skip to content

Instantly share code, notes, and snippets.

@peterjaap
Last active November 9, 2022 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjaap/fd0375a55245df7052dd77caf093db9d to your computer and use it in GitHub Desktop.
Save peterjaap/fd0375a55245df7052dd77caf093db9d to your computer and use it in GitHub Desktop.
Quick & dirty script to check for existence of Magento (2) table names in codebase
<?php
$tables = shell_exec('magerun2 db:query "SHOW TABLES"');
$tables = array_filter(explode(PHP_EOL, $tables), function ($table) {
if (empty($table)) return false;
if (substr($table, -3) === '_cl') return false;
if (substr($table, -8) === '_replica') return false;
if (stripos($table, '_flat_') !== false) return false;
if (stripos($table, '_fulltext_') !== false) return false;
if (stripos($table, 'sequence_') !== false) return false;
return true;
});
$tables = array_slice($tables, 1);
foreach ($tables as $table) {
$appCodeResult = (int) trim(shell_exec('find app/code -type f -print0 | xargs -0 grep ' . $table . ' | wc -l'));
$vendorResult = (int) trim(shell_exec('find vendor -type f -print0 | xargs -0 grep ' . $table . ' | wc -l'));
$results = $appCodeResult + $vendorResult;
if ($results === 0) {
echo $table . ' was not found in the codebase' . PHP_EOL;
echo 'DROP TABLE IF EXISTS ' . $table . ';' . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment