PHP code example of clouddueling / auto-migrate

1. Go to this page and download the library: Download clouddueling/auto-migrate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clouddueling / auto-migrate example snippets


        $params = array(
            'dbuser' => 'root',
            'dbpass' => 'root',
            'dbname' => 'database',
            'dbhost' => 'localhost'
        );

        try {
            $diff = new MySQLDiff($params);
        } catch(Exception $e) {
            echo $e->getMessage(); exit;
        }

        // This returns an array of what's missing in the database
        try {
            $diff_lines = $diff->getDiffs();
            var_dump($diff_lines);
         catch(Exception $e) {
            echo $e->getMessage(); exit;
        }

        // This returns SQL queries which can be run to fix the database
        try {
            $diff_lines = $diff->getSQLDiffs();
            var_dump($diff_lines);
        } catch(Exception $e) {
            echo $e->getMessage(); exit;
        }

        // This generates the SQL and actually runs all of them
        try {
            $diff_lines = $diff->runSQLDiff();
            var_dump($diff_lines);
        } catch(Exception $e) {
            echo $e->getMessage(); exit;
        }
    

    // Coming soon, feel free to PR this
    

    

    class Schema_Task {

        public function update($arguments)
        {
            $params = array(
                'dbuser' => Config::get('database.connections.mysql.username'),
                'dbpass' => Config::get('database.connections.mysql.password'),
                'dbname' => Config::get('database.connections.mysql.database'),
                'dbhost' => Config::get('database.connections.mysql.host')
            );

            $files = scandir(Config::get('schemas.path'));
            $differences = 0;

            foreach ($files as $file) {
                if (in_array($file, ['.','..','.DS_Store'])) {
                    continue;
                }

                $params['dumpxml'] = Config::get('schemas.path') . '/' . $file;

                try {
                    $diff = new CloudDueling\AutoMigrate\MySQL($params);
                    $diff_lines = $diff->getSQLDiffs();
                    if (count($diff_lines) == 0) {
                        continue;
                    }

                    ++$differences;

                    echo "Difference found: {$params['dumpxml']}\n" .
                    " - " . implode("\n - ", $diff_lines) . "\n\n";
                    $diff->runSQLDiff();
                } catch(Exception $e) {
                    echo $e->getMessage() . "\n";
                    exit;
                }
            }

            echo "Differences found: {$differences}\n";
        }

    }