When I update/upgrade a plugin, theme, or even the core of WordPress, it has an impact in the database, it changes many things. How do I keep track of it?

The problem is that I have a repository, all the changes made to the files are all correctly tracked, but I don't have any idea how to create an SQL file to store the database changes (alter, insert, update, drop, create, etc.).

I use a tool, which is Flyway, that picks the list of SQL files I have in the repository and applies the query of each file to the database, so my question is, how do I create or get an SQL query of all the changes made by the update/upgrade future of WordPress? This way all the team will be able just to make a pull from the repo and the changes I did will be propagated.


  • Versionpress is a plugin that shows this.
    – ejntaylor
    Commented Jul 10, 2018 at 10:55

1 Answer 1


Log queries:

You could collect all the queries during core/plugin/theme upgrades to see what happens. Just follow these two steps:

1) You should add:

define( 'SAVEQUERIES', TRUE );

to your wp-config.php file to collect all queries during a page load into the $wpdb->queries array. Just remember to remove it afterwards.

2) Then you could log it into the sql.log file. Here's a simple example:

 * Dump all database queries to the /wp-content/sql.log file.
add_action( 'shutdown', function(){

    $file =  WP_CONTENT_DIR . '/sql.log'; // Edit this filepath to your needs.

    if( current_user_can( 'manage_options' ) 
         && file_exists( $file )
         && is_writeable( $file )
         && isset( $GLOBALS['wpdb']->queries )
                date( 'c' ) . PHP_EOL .  print_r( $GLOBALS['wpdb']->queries, TRUE ), 


or use the query filter of the wpdb class to log only the INSERT, UPDATE and DELETE queries:

 * Log the INSERT, UPDATE, DELETE database queries to the /wp-content/sql.log file.
add_filter( 'query', function( $query ){
    if( FALSE !== stripos( $query, 'UPDATE ' )
        || FALSE !== stripos( $query, 'INSERT ' )
        || FALSE !== stripos( $query, 'DELETE ' )
     ) {
            $file =  WP_CONTENT_DIR . '/sql.log'; // Edit this filepath to your needs.  
            if( file_exists( $file ) && is_writeable( $file ) ) 
                    date( 'c' ) . ' - ' . $query . PHP_EOL, 
                    FILE_APPEND | LOCK_EX 
    return $query;

Core upgrade files:

For core upgrades these files might be of interest to you

  • /wp-includes/version.php
  • /wp-admin/includes/upgrade.php
  • /wp-admin/includes/schema.php

In the file /wp-admin/includes/upgrade.php you can find the upgrade function: wp_upgrade() that calls the upgrade_all() function. It contains database upgrades for each version in terms of functions like upgrade_xxx()

For example:


    if ( $wp_current_db_version < 22422 )

    if ( $wp_current_db_version < 25824 )

    if ( $wp_current_db_version < 26148 )

    if ( $wp_current_db_version < 26691 )



    update_option( 'db_version', $wp_db_version );
    update_option( 'db_upgraded', true );

I hope this helps.

  • I think this logs everything, including the SELECT queries, which aren't needed, but I think those can be deleted easily. This is what I was looking for, thanks. Commented Jun 5, 2014 at 3:07
  • Glad to hear that. You could also use the query filter of the wpdb class to filter out the INSERT/UPDATE/DELETE queries and log them. I updated the answer with an example. Maybe this plugin could also assist you?
    – birgire
    Commented Jun 5, 2014 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.