Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cloning of tables with foreign key constraints #97

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion lib/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public static function db_copy_tables( $from_site_id, $to_site_id ) {
$from_site_table = self::do_sql_query($sql_query, 'col');
}

// disable foreign key checks so order of cloning doesn't matter
self::disable_foreign_key_checks();

foreach ($from_site_table as $table) {

$table_name = $to_site_prefix . substr( $table, $from_site_prefix_length );
Expand All @@ -74,6 +77,9 @@ public static function db_copy_tables( $from_site_id, $to_site_id ) {

}

// reenable foreign key checks
self::enable_foreign_key_checks();

// apply key options from new blog.
self::db_restore_data( $to_site_id, $saved_options );

Expand Down Expand Up @@ -373,8 +379,30 @@ public static function sql_error($sql_query, $sql_error) {
echo '<a href="' . $log_url . '">' . MUCD_NETWORK_PAGE_DUPLICATE_VIEW_LOG . '</a>';
}
MUCD_Functions::remove_blog(self::$to_site_id);
self::enable_foreign_key_checks();
wp_die();
}

/**
* Disable database foreign key checks
* @return void
*/
private static function disable_foreign_key_checks() {
self::$foreign_key_checks_disabled = true;
self::do_sql_query('SET FOREIGN_KEY_CHECKS=0;');
}

/**
* Enable database foreign key checks if it was disabled before
* @return void
*/
private static function enable_foreign_key_checks() {
if (self::$foreign_key_checks_disabled) {
// change variable before executing the query to prevent
// endless recursion loop in case of error
self::$foreign_key_checks_disabled = false;
self::do_sql_query('SET FOREIGN_KEY_CHECKS=1;');
}
}
}
}
}