ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Changing the Site ID's in MSM

September 29, 2008 9:50pm

Subscribe [1]
  • #1 / Sep 29, 2008 9:50pm

    barchas

    22 posts

    Because I got annoyed about the fact that I cannot delete the “main site” I decided to see if I could shuffle everything around, assigning one of the later sites, to site 1, and shift site 1 to a higher number. IE, swap them, like 1 and 3.

    So although this is not fully tested, and I make no promise that you will not completely erase everything, break it, blow it up, cause the LHC to form a blackhole, end life as we know it, it does seem to work. You must have a site with site_id=1 at the very least when you are all done shuffling around. I suggest moving ID 1 first, then switching your other site.

    This will walk every table, looking for a site_id column, and update all the entries to reflect your change.

    Again. BACKUP YOUR DATABASE COMPLETELY. If you break your site, you can just restore the database and try again. Though really I would do this on a dev box with an exact copy before I even think about doing it live. I still havent…

    In case your wondering why I needed this, I want to separate out a site which is under MSM, but I want it to be a standalone on another server. Imagine a section of a site that got really popular, and needs it’s own sandbox for example. And obviously you need the correct license for it as well. So i copy the database to the new server, shuffle the sites on the new server, delete all the other MSM sites from that server, and I am left with the 1 site I wanted to move onto that new server.

    Everything worked fine for me. The only problem I have found is that I have an orphaned category which is causing me problems when I try to delete the extra sites. But that may have been due to a prior hack I was implementing that I never fully cleaned up.

    <?php
    
    /*********************
    Set these to your needs
    **********************/
    $orgID = 3; //this is the ID of the site you want to change to a new ID
    $newID = 1; //this is the ID you want to set it to. Make sure you do not have this ID already in the database….
    $prefix = 'exp_'; //your table prefix in your database
    
    $server = "localhost";
    $user = 'imauser';
    $pass = 'reallyeasypassword';
    $database = 'eedatabase';
    
    /*********************
    No Need to touch below this line
    **********************/
    
    $db = mysql_connect($server, $user, $pass,true) or die('Could not connect to database server.' );
    mysql_select_db($database, $db) or die('Could not select database.');
    
    
    $rs = mysql_query("SHOW TABLES");
    while ($row = mysql_fetch_array($rs))
    {
        if ($row[0]==$prefix.'sites')
        {
            mysql_query("ALTER TABLE `{$row[0]}` CHANGE `site_id` `site_id` INT( 5 ) UNSIGNED NOT NULL",$db);
            echo ' OFF'; 
        }
        mysql_query("update `{$row[0]}` set `site_id`=$newID where `site_id`=$orgID",$db);
        echo ' updated';
        if ($row[0]==$prefix.'sites')
        {
            mysql_query(" ALTER TABLE `{$row[0]}` CHANGE `site_id` `site_id` INT( 5 ) UNSIGNED NOT NULL AUTO_INCREMENT  ",$db);
            echo ' ON';
        } 
        echo '
    ';
    }
    
    ?>
  • #2 / Sep 29, 2008 10:17pm

    barchas

    22 posts

    OK i needed another function, so in case anyone else needs it, here goes.
    I needed to find all the orphaned data from the site I am deleting.
    Because some things would need to be a manual, hands on process, like removing custom fields for example, I did not want to automatically do it. I just wanted to know which tables I had to look at, so I could skip looking at the tables that arent even involved.

    A bit of “paying attention” and knowledge of the EE database structure is required for a few of the cleanups, such as the custom fields.

    Again, no warranty, use at your own risk. backup or suffer the consequences.

    <?php
    
    /*********************
    Set these to your needs
    **********************/
    $nukeid = 2;
    $prefix = 'exp_';
    
    $server = 'localhost';
    $user = 'imauser';
    $pass = 'somepassword';
    $database = 'eedatabasename';
    
    /*********************
    No Need to touch below this line
    **********************/
    
    $db = mysql_connect($server, $user, $pass,true) or die('Could not connect to database server.' );
    mysql_select_db($database, $db) or die('Could not select database.');
    
    
    $rs = mysql_query("SHOW TABLES");
    while ($row = mysql_fetch_array($rs))
    {
        $rsc = mysql_fetch_array(mysql_query("Select count(*) as thecount from {$row[0]} where site_id=$nukeid",$db));
        if ($rsc[0]>0)
        {
            echo $row[0].'-'.$rsc[0].'
    ';
        }
    }
    
    ?>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases