diff --git a/scripts/sql/upgrade_schema.php b/scripts/sql/upgrade_schema.php new file mode 100755 index 0000000000..3cc2854009 --- /dev/null +++ b/scripts/sql/upgrade_schema.php @@ -0,0 +1,142 @@ +#!/usr/bin/env php +establishConnection('w'); +$create_sql = <<establishConnection('w'); +$create_sql = <<withSuffix('sql'); +$results = $finder->find(); + +$patches = array(); +foreach ($results as $r) { + $matches = array(); + if (preg_match('/(\d+)\..*\.sql$/', $r, $matches)) { + $patches[] = array('version' => (int)$matches[1], + 'file' => $r); + } else { + print + "*** WARNING : File {$r} does not follow the normal naming ". + "convention. ***\n"; + } +} + +// Files are in some 'random' order returned by the operating system +// We need to apply them in proper order +$patches = isort($patches, 'version'); + +$patch_applied = false; +foreach ($patches as $patch) { + if ($patch['version'] < $next_version) { + continue; + } + + print "Applying patch {$patch['file']}\n"; + + $path = Filesystem::resolvePath($patches_dir.$patch['file']); + + $user = PhabricatorEnv::getEnvConfig('mysql.user'); + $pass = PhabricatorEnv::getEnvConfig('mysql.pass'); + $host = PhabricatorEnv::getEnvConfig('mysql.host'); + + list($stdout, $stderr) = execx( + "mysql --user=%s --password=%s --host=%s < %s", + $user, $pass, $host, $path); + + if ($stderr) { + print $stderr; + exit(-1); + } + + // Patch was successful, update the db with the latest applied patch version + // 'DELETE' and 'INSERT' instead of update, because the table might be empty + queryfx($conn, 'DELETE FROM %T', TABLE_NAME); + queryfx($conn, 'INSERT INTO %T values (%d)', TABLE_NAME, $patch['version']); + + $patch_applied = true; +} + +if (!$patch_applied) { + print "Your database is already up-to-date\n"; +}