Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Last active December 25, 2015 22:19
Show Gist options
  • Save cesarmiquel/7048936 to your computer and use it in GitHub Desktop.
Save cesarmiquel/7048936 to your computer and use it in GitHub Desktop.
Change the prefix to all Drupal tables.
<?php
// ------------------------------------------------------------------------------------------
// This simple script adds the specified table prefix to all tables in the current DB.
// It is used if you want to add a prefix to all tables once the DB has been created.
//
// WARNING: Do not run on a DB which already uses prefixes to separate different databases.
//
// To run copy this script to the docroot of your Drupal installation and then do:
//
// $ drush scr add-table-prefix.php
//
// Assumptions:
//
// * You are NOT using a multi site environment
// * You have Drush configured and running
// * You have a single site in sites/all/
//
// DISCLAIMER: If this code breaks your site don't blame me! You should understand how this
// works before running it. Also don't run this on a production server withoug
// having done tests on another server.
// ------------------------------------------------------------------------------------------
// This is prefix you want your tables to have.
$prefix = 'xyz123';
// Read all tables
$result = db_query("SHOW TABLES");
if ($result) {
while ($row = $result->fetch(PDO::FETCH_NUM)) {
// rename table
$table_old = $row[0];
$table_new = $prefix . '_' . $table_old;
db_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