Last active
August 29, 2015 14:03
-
-
Save matteomattei/4d80ba688079eb5affc3 to your computer and use it in GitHub Desktop.
How to clone MySQL database schema in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/********************* START CONFIGURATION *********************/ | |
$DB_SRC_HOST='localhost'; | |
$DB_SRC_USER='root'; | |
$DB_SRC_PASS='password'; | |
$DB_SRC_NAME='database1'; | |
$DB_DST_HOST='localhost'; | |
$DB_DST_USER='root'; | |
$DB_DST_PASS='password'; | |
$DB_DST_NAME='database2'; | |
/*********************** GRAB OLD SCHEMA ***********************/ | |
$db1 = mysql_connect($DB_SRC_HOST,$DB_SRC_USER,$DB_SRC_PASS) or die(mysql_error()); | |
mysql_select_db($DB_SRC_NAME, $db1) or die(mysql_error()); | |
$result = mysql_query("SHOW TABLES;",$db1) or die(mysql_error()); | |
$buf="set foreign_key_checks = 0;\n"; | |
$constraints=''; | |
while($row = mysql_fetch_array($result)) | |
{ | |
$result2 = mysql_query("SHOW CREATE TABLE ".$row[0].";",$db1) or die(mysql_error()); | |
$res = mysql_fetch_array($result2); | |
if(preg_match("/[ ]*CONSTRAINT[ ]+.*\n/",$res[1],$matches)) | |
{ | |
$res[1] = preg_replace("/,\n[ ]*CONSTRAINT[ ]+.*\n/","\n",$res[1]); | |
$constraints.="ALTER TABLE ".$row[0]." ADD ".trim($matches[0]).";\n"; | |
} | |
$buf.=$res[1].";\n"; | |
} | |
$buf.=$constraints; | |
$buf.="set foreign_key_checks = 1"; | |
/**************** CREATE NEW DB WITH OLD SCHEMA ****************/ | |
$db2 = mysql_connect($DB_DST_HOST,$DB_DST_USER,$DB_DST_PASS) or die(mysql_error()); | |
$sql = 'CREATE DATABASE '.$DB_DST_NAME; | |
if(!mysql_query($sql, $db2)) die(mysql_error()); | |
mysql_select_db($DB_DST_NAME, $db2) or die(mysql_error()); | |
$queries = explode(';',$buf); | |
foreach($queries as $query) | |
if(!mysql_query($query, $db2)) die(mysql_error()); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment