query($query)) { if($res->num_rows == 0) { print("no database found\n"); if(readline("No database exists. Do want us to create it? (Y) ") != "Y") { abort(); } } } // connect to DB. $con->select_db($config['database']); // group all ver $line = fgets(STDIN); if(!preg_match("/^# Versioned Migration Script$/", $line)) { die("Sorry, this doesn't look like a versioned file.\n"); } // find current version. // find max version $current_version = 1; $v = "0"; while($line = fgets(STDIN)) { $line = trim($line); if(preg_match("/^# Version ([0-9])+/", $line, $matches)) { $v = $matches[1]; if(array_key_exists($v, $versions)) { die("Version $v already exists, you cannot have two of the same version\n"); } $versions[$v] = []; } else { $versions[$v][] = $line; } } print_r($versions); $max = max(array_keys($versions)); print("Current version: $current_version\n"); print_r("Max version : $max\n"); if($current_version == $max) { die("You're up to date on version $current_version"); } if($current_version > $max) { die("You have a new version than provided by the sql file"); } foreach($versions as $version => $rows) { if($version > $current_version) { print("I should run $version\n"); } }