From 4f6bac95e95444f1af5b7670d6da65a7dd1c4ee6 Mon Sep 17 00:00:00 2001 From: tuomaspelkonen Date: Thu, 7 Apr 2011 16:55:32 -0700 Subject: [PATCH] Schema upgrade script. Summary: Created a script the runs all the necessary patches for db schema. Stores information in the db about the latest patch that was applied. Test Plan: Created two test files '024.test.sql' and '023.test.sql' in this order. '023' creates a database and '024' creates a table in this db. First ran './upgrade_schema.php 23' and made sure that patches were applied in order. Then ran './upgrade_schema.php' to make sure db was up-to-date. Checked manually from the db that the database and table exists. Reviewed By: epriestley Reviewers: epriestley CC: jungejason, epriestley, tuomaspelkonen Differential Revision: 115 --- scripts/sql/upgrade_schema.php | 142 +++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100755 scripts/sql/upgrade_schema.php 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"; +}