Migrating databases using phpMyAdmin's tracking mechanism

后端 未结 5 1409
悲&欢浪女
悲&欢浪女 2020-12-28 12:05

In a development database, I have phpMyAdmin Tracking enabled on all tables. It logs all the changes I make to the tables\' structures (in this case I\'m not interested in d

5条回答
  •  旧巷少年郎
    2020-12-28 12:28

    I don't have anything that creates an incremental diff between two databases but here's the script I use to compare two MySQL databases:

    setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // Try to use the driver's native prepared statements.
        $db1_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Let's use exceptions so we can try/catch errors.
    }catch(PDOException $e){
        echo "

    Connection failed for $db1_host: " . $e->getMessage() . '

    '; exit; } try{ $db2_con = new PDO("mysql:host=$db2_host;dbname=information_schema", $db2_username, $db2_password); $db2_con->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); // Try to use the driver's native prepared statements. $db2_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Let's use exceptions so we can try/catch errors. }catch(PDOException $e){ echo "

    Connection failed for $db2_host: " . $e->getMessage() . '

    '; exit; } if (NULL !== $db1_con && NULL !== $db2_con){ echo "

    Column Analysis

    "; $sql = 'SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION'; $statement1 = $db1_con->prepare($sql); $statement1->bindValue(1, $db1_dbname); $statement2 = $db2_con->prepare($sql); $statement2->bindValue(1, $db2_dbname); if (TRUE === $statement1->execute()){ while ($row = $statement1->fetch(PDO::FETCH_ASSOC)){ $db1_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']] = array(); foreach ($row AS $key => $value){ $db1_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; } } } if (TRUE === $statement2->execute()){ while ($row = $statement2->fetch(PDO::FETCH_ASSOC)){ $db2_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']] = array(); foreach ($row AS $key => $value){ $db2_tables[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; } } } foreach ($db1_tables AS $table => $info){ if (!isset($db2_tables[$table])){ echo "

    Table $table does not exist in the SECOND database!

    "; }else{ foreach ($info AS $column => $data){ if (!isset($db2_tables[$table][$column])){ echo "

    Column $column does not exist in table $table in the SECOND database!

    "; }else{ if (count($data)){ foreach ($data AS $key => $value){ if ($db1_tables[$table][$column][$key] !== $db2_tables[$table][$column][$key]){ echo "

    Column $column in table $table has differing characteristics for $key (". $db1_tables[$table][$column][$key] ." vs. ". $db2_tables[$table][$column][$key] .")

    "; } } } } } } } foreach ($db2_tables AS $table => $info){ if (!isset($db1_tables[$table])){ echo "

    Table $table does not exist in the FIRST database!

    "; }else{ foreach ($info AS $column => $data){ if (!isset($db1_tables[$table][$column])){ echo "

    Column $column does not exist in table $table in the FIRST database!

    "; }else{ if (count($data)){ foreach ($data AS $key => $value){ if ($db2_tables[$table][$column][$key] !== $db1_tables[$table][$column][$key]){ echo "

    Column $column in table $table has differing characteristics for $key (". $db2_tables[$table][$column][$key] ." vs. ". $db1_tables[$table][$column][$key] .")

    "; } } } } } } } echo "

    Constraint Analysis

    "; $sql = 'SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = ? ORDER BY TABLE_NAME, ORDINAL_POSITION'; $statement1 = $db1_con->prepare($sql); $statement1->bindValue(1, $db1_dbname); $statement2 = $db2_con->prepare($sql); $statement2->bindValue(1, $db2_dbname); if (TRUE === $statement1->execute()){ while ($row = $statement1->fetch(PDO::FETCH_ASSOC)){ foreach ($row AS $key => $value){ $db1_constraints[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; } } } if (TRUE === $statement2->execute()){ while ($row = $statement2->fetch(PDO::FETCH_ASSOC)){ foreach ($row AS $key => $value){ $db2_constraints[$row['TABLE_NAME']][$row['COLUMN_NAME']][$key] = $value; } } } foreach ($db1_constraints AS $table => $info){ foreach ($info AS $column => $data){ if (isset($db2_constraints[$table][$column])){ if (count($data)){ foreach ($data AS $key => $value){ if ('CONSTRAINT_NAME' !== $key && $db1_constraints[$table][$column][$key] !== $db2_constraints[$table][$column][$key]){ echo "

    Column $column in table $table has differing characteristics for $key (". $db1_constraints[$table][$column][$key] ." vs. ". $db2_constraints[$table][$column][$key] .")

    "; } } } }else{ echo "

    Column $column in table $table is missing a constraint in the SECOND database!

    "; } } } foreach ($db2_constraints AS $table => $info){ foreach ($info AS $column => $data){ if (isset($db1_constraints[$table][$column])){ if (count($data)){ foreach ($data AS $key => $value){ if ('CONSTRAINT_NAME' !== $key && $db2_constraints[$table][$column][$key] !== $db1_constraints[$table][$column][$key]){ echo "

    Column $column in table $table has differing characteristics for $key (". $db2_constraints[$table][$column][$key] ." vs. ". $db1_constraints[$table][$column][$key] .")

    "; } } } }else{ echo "

    Column $column in table $table is missing a constraint in the FIRST database!

    "; } } } } ?>

    Edited to add code that shows differences in constraints as well.

提交回复
热议问题