Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Created October 2, 2018 20:23
Show Gist options
  • Save cesarmiquel/5dd22140b494a739d658806036915511 to your computer and use it in GitHub Desktop.
Save cesarmiquel/5dd22140b494a739d658806036915511 to your computer and use it in GitHub Desktop.
Rename Drupal Tables (remove prefix)
<?php
$cn = \Drupal\Core\Database\Database::getConnection('default', 'default');
// This is prefix you want your tables to remove.
$prefix = 's3cr3t_';
// Read all tables
$result = $cn->query("SHOW TABLES");
if ($result) {
while ($row = $result->fetch(PDO::FETCH_NUM)) {
// rename table
$table_old = $row[0];
$table_new = str_replace($prefix, '', $table_old);
$cn->query("RENAME TABLE {$table_old} TO {$table_new}");
// show progress
print " $table_old -> $table_new\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment