<commit_before><?php

// This migration converts the old munkireport table to a new version
// most of the data is migrated to managedinstalls

class Migration_munkireport_new extends Model
{
    // Drop these columns
    protected $dropCols = array(
                'managedinstalls',
                'pendinginstalls',
                'installresults',
                'removalresults',
                'failedinstalls',
                'pendingremovals',
                'itemstoinstall',
                'appleupdates',
                'report_plist',
                'runstate',
            );
            
    // Add these columns
    protected $addcols = array(
                'error_json' => 'BLOB',
                'warning_json' => 'BLOB',
            ); 
    
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct('id', 'munkireport'); //primary key, tablename        
    }

    
    public function up()
    {
        // Get database handle
        $dbh = $this->getdbh();

        
        // Wrap in transaction
        $dbh->beginTransaction();
        
        // ***** Move content from report_plist to managedinstalls module
        
        // Check if we have compression support
        $compress = function_exists('gzdeflate');
        
        // Instantiate managedinstalls model
        $managedinstalls = new Managedinstalls_model;
                
        // Get managedinstalls instance
        
        // Get all reports (make faster by only moving data of machines that
        // don't have entries in managedinstalls)
        $sql = "SELECT munkireport.serial_number, report_plist 
                FROM munkireport 
                LEFT JOIN managedinstalls USING(serial_number)
                WHERE managedinstalls.serial_number IS NULL AND report_plist != ''";
        foreach($dbh->query($sql) as $arr)
        {
            $report = unserialize( $this->COMPRESS_ARRAY ? gzinflate( $arr['report_plist'] ) : $arr['report_plist'] );
            $install_list = array();
            if(isset($report['ManagedInstalls'])){
                $this->add_items($report['ManagedInstalls'], $install_list, 'installed', 'munki');
            }
            if(isset($report['AppleUpdates'])){
                $this->add_items($report['AppleUpdates'], $install_list, 'pending_install', 'applesus');
            }
            if(isset($report['ProblemInstalls'])){
                $this->add_items($report['ProblemInstalls'], $install_list, 'install_failed', 'munki');
            }
            if(isset($report['ItemsToRemove'])){
                $this->add_items($report['ItemsToRemove'], $install_list, 'pending_removal', 'munki');
            }
            if(isset($report['ItemsToInstall'])){
                $this->add_items($report['ItemsToInstall'], $install_list, 'pending_install', 'munki');
            }
            // Removed items
            if(isset($report['RemovedItems'])){
                $this->add_removeditems($report['RemovedItems'], $install_list);
            }

            // Update install_list with results
            if(isset($report['RemovalResults'])){
                $this->remove_result($report['RemovalResults'], $install_list);
            }
            if(isset($report['InstallResults'])){
                $this->install_result($report['InstallResults'], $install_list);
            }
                        
            // Store data in managedinstalls
            $managedinstalls->setSerialNumber($arr['serial_number']);
            $managedinstalls->processData($install_list);

        }

        // ***** Modify columns

        if ($this->get_driver() == 'mysql')
        {
            // ***** Add columns
            foreach($this->addcols as $colname => $type){
                $sql = sprintf('ALTER TABLE %s ADD COLUMN %s %s',
                    $this->tablename, $colname, $type);
                $this->exec($sql);
            }
            
            // Drop columns
            $sql = sprintf('ALTER TABLE munkireport DROP %s', implode(', DROP ', $this->dropCols));
            $this->exec($sql); 
            
            // Create indexes
            foreach ($addcols as $key ) {
                $sql = sprintf('CREATE INDEX munkireport_%s ON munkireport (%s)', $key, $key);
                $this->exec($sql); 
            }
            
        }
        else{
            throw new Exception("SQLite migration not ready", 1);
        }
        
        // Commit transaction
        $dbh->commit();
        
        //throw new Exception("Error Processing Request", 1);
        
        

    }// End function up()<commit_msg>Add errors and warnings to migration<commit_after><?php

// This migration converts the old munkireport table to a new version
// most of the data is migrated to managedinstalls

class Migration_munkireport_new extends Model
{
    // Drop these columns
    protected $dropCols = array(
                'managedinstalls',
                'pendinginstalls',
                'installresults',
                'removalresults',
                'failedinstalls',
                'pendingremovals',
                'itemstoinstall',
                'appleupdates',
                'report_plist',
                'runstate',
            );
            
    // Add these columns
    protected $addcols = array(
                'error_json' => 'BLOB',
                'warning_json' => 'BLOB',
            ); 
    
    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct('id', 'munkireport'); //primary key, tablename        
    }

    
    public function up()
    {
        // Get database handle
        $dbh = $this->getdbh();

        
        // Wrap in transaction
        $dbh->beginTransaction();
        
        // ***** Add columns
        
        try {
            foreach($this->addcols as $colname => $type){
                $sql = sprintf('ALTER TABLE %s ADD COLUMN %s %s',
                    $this->tablename, $colname, $type);
                $this->exec($sql);
            }
        } catch (Exception $e) {
            // We do nothing here
        }
        

        
        // ***** Move content from report_plist to managedinstalls module
        
        // Check if we have compression support
        $compress = function_exists('gzdeflate');
        
        // Instantiate managedinstalls model
        $managedinstalls = new Managedinstalls_model;
                
        // Get managedinstalls instance
        
        // Get all reports (make faster by only moving data of machines that
        // don't have entries in managedinstalls)
        $sql = "SELECT munkireport.serial_number, report_plist 
                FROM munkireport 
                LEFT JOIN managedinstalls USING(serial_number)
                WHERE managedinstalls.serial_number IS NULL AND report_plist != ''";
        foreach($dbh->query($sql) as $arr)
        {
            $report = unserialize( $this->COMPRESS_ARRAY ? gzinflate( $arr['report_plist'] ) : $arr['report_plist'] );
            $install_list = array();
            if(isset($report['ManagedInstalls'])){
                $this->add_items($report['ManagedInstalls'], $install_list, 'installed', 'munki');
            }
            if(isset($report['AppleUpdates'])){
                $this->add_items($report['AppleUpdates'], $install_list, 'pending_install', 'applesus');
            }
            if(isset($report['ProblemInstalls'])){
                $this->add_items($report['ProblemInstalls'], $install_list, 'install_failed', 'munki');
            }
            if(isset($report['ItemsToRemove'])){
                $this->add_items($report['ItemsToRemove'], $install_list, 'pending_removal', 'munki');
            }
            if(isset($report['ItemsToInstall'])){
                $this->add_items($report['ItemsToInstall'], $install_list, 'pending_install', 'munki');
            }
            // Removed items
            if(isset($report['RemovedItems'])){
                $this->add_removeditems($report['RemovedItems'], $install_list);
            }

            // Update install_list with results
            if(isset($report['RemovalResults'])){
                $this->remove_result($report['RemovalResults'], $install_list);
            }
            if(isset($report['InstallResults'])){
                $this->install_result($report['InstallResults'], $install_list);
            }
                        
            // Store data in managedinstalls
            $managedinstalls->setSerialNumber($arr['serial_number']);
            $managedinstalls->processData($install_list);
            
            // Save errors and warnings
            if(isset($report['Errors'])){
                $sql = sprintf("UPDATE munkireport SET error_json = ? WHERE serial_number = '%s'",
                            $arr['serial_number']);
                $this->query($sql, array(json_encode($report['Errors']))); 
            }
            
            if(isset($report['Warnings'])){
                $sql = sprintf("UPDATE munkireport SET warning_json = ? WHERE serial_number = '%s'",
                            $arr['serial_number']);
                $this->query($sql, array(json_encode($report['Warnings']))); 
            }

        }

        // ***** Modify columns

        if ($this->get_driver() == 'mysql')
        {            
            // Drop columns
            $sql = sprintf('ALTER TABLE munkireport DROP %s', implode(', DROP ', $this->dropCols));
            $this->exec($sql);             
        }
        else{
            throw new Exception("SQLite migration not ready", 1);
        }
        
        // Commit transaction
        $dbh->commit();
        
        //throw new Exception("Error Processing Request", 1);
        
        

    }// End function up()
{'fraction_non_alphanumeric': 0.1268732280275415, 'fraction_numerical': 0.0004050222762251924, 'mean_word_length': 1.6665766738660908, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3398536', 'n_tokens_mistral': 2632, 'n_tokens_neox': 2504, 'n_words': 536}