114 global $ui; |
114 global $ui; |
115 $ui->show_footer(); |
115 $ui->show_footer(); |
116 exit; |
116 exit; |
117 } |
117 } |
118 |
118 |
119 function enano_perform_upgrade($target_branch) |
119 function enano_perform_upgrade($target_rev) |
120 { |
120 { |
121 global $db, $session, $paths, $template, $plugins; // Common objects |
121 global $db, $session, $paths, $template, $plugins; // Common objects |
122 // Import version info |
122 // Import version info |
123 global $enano_versions; |
123 global $enano_versions; |
124 // Import UI functions |
124 // Import UI functions |
131 { |
131 { |
132 // yep, fall out here to avoid errors |
132 // yep, fall out here to avoid errors |
133 return true; |
133 return true; |
134 } |
134 } |
135 |
135 |
|
136 // |
136 // Main upgrade stage |
137 // Main upgrade stage |
137 |
138 // |
138 // Init vars |
|
139 list($major_version, $minor_version) = explode('.', installer_enano_version()); |
|
140 $installer_branch = "$major_version.$minor_version"; |
|
141 $installer_branch = preg_replace('/^upg-/', '', $installer_branch); |
|
142 $target_branch = preg_replace('/^upg-/', '', $target_branch); |
|
143 |
|
144 $version_flipped = array_flip($enano_versions[$target_branch]); |
|
145 $version_curr = enano_version(); |
|
146 // Change this to be the last version in the current branch. |
|
147 // If we're just upgrading within this branch, use the version the installer library |
|
148 // reports to us. Else, use the latest in the old (current target) branch. |
|
149 // $version_target = installer_enano_version(); |
|
150 $version_target = ( $target_branch === $installer_branch ) ? installer_enano_version() : $enano_versions[$target_branch][ count($enano_versions[$target_branch]) - 1 ]; |
|
151 |
139 |
152 // Calculate which scripts to run |
140 // Calculate which scripts to run |
153 if ( !isset($version_flipped[$version_curr]) ) |
141 $versions_avail = array(); |
154 { |
142 |
155 echo '<p>ERROR: Unsupported version</p>'; |
143 $dir = ENANO_ROOT . "/install/schemas/upgrade/$dbdriver/"; |
156 $ui->show_footer(); |
144 if ( $dh = @opendir($dir) ) |
157 exit; |
145 { |
158 } |
146 while ( $di = @readdir($dh) ) |
159 if ( !isset($version_flipped[$version_target]) ) |
147 { |
160 { |
148 if ( preg_match('/^[0-9]+\.sql$/', $di) ) |
161 echo '<p>ERROR: Upgrader doesn\'t support its own version</p>'; |
149 $versions_avail[] = intval($di); |
162 $ui->show_footer(); |
150 } |
163 exit; |
151 } |
164 } |
152 else |
165 $upg_queue = array(); |
153 { |
166 for ( $i = $version_flipped[$version_curr]; $i < $version_flipped[$version_target]; $i++ ) |
154 return false; |
167 { |
155 } |
168 if ( !isset($enano_versions[$target_branch][$i + 1]) ) |
156 |
169 { |
157 // sort version list numerically |
170 echo '<p>ERROR: Unsupported intermediate version</p>'; |
158 asort($versions_avail); |
171 $ui->show_footer(); |
159 $versions_avail = array_values($versions_avail); |
172 exit; |
160 |
173 } |
161 $last_rev = $versions_avail[ count($versions_avail) - 1 ]; |
174 $ver_this = $enano_versions[$target_branch][$i]; |
162 |
175 $ver_next = $enano_versions[$target_branch][$i + 1]; |
163 $current_rev = getConfig('db_version', 0); |
176 $upg_queue[] = array($ver_this, $ver_next); |
164 |
177 } |
165 if ( $last_rev <= $current_rev ) |
178 |
166 return true; |
179 // Verify that all upgrade scripts are usable |
167 |
180 foreach ( $upg_queue as $verset ) |
168 foreach ( $versions_avail as $i => $ver ) |
181 { |
169 { |
182 $file = ENANO_ROOT . "/install/schemas/upgrade/{$verset[0]}-{$verset[1]}-$dbdriver.sql"; |
170 if ( $ver > $current_rev ) |
183 if ( !file_exists($file) ) |
171 { |
184 { |
172 $upg_queue = array_slice($versions_avail, $i); |
185 echo "<p>ERROR: Couldn't find required schema file: $file</p>"; |
173 break; |
186 $ui->show_footer(); |
174 } |
187 exit; |
175 } |
188 } |
176 |
189 } |
|
190 // Perform upgrade |
177 // Perform upgrade |
191 foreach ( $upg_queue as $verset ) |
178 foreach ( $upg_queue as $version ) |
192 { |
179 { |
193 $file = ENANO_ROOT . "/install/schemas/upgrade/{$verset[0]}-{$verset[1]}-$dbdriver.sql"; |
180 $file = "{$dir}$version.sql"; |
|
181 |
194 try |
182 try |
195 { |
183 { |
196 $parser = new SQL_Parser($file); |
184 $parser = new SQL_Parser($file); |
197 } |
185 } |
198 catch(Exception $e) |
186 catch(Exception $e) |
208 // Check for empty schema file |
196 // Check for empty schema file |
209 if ( $sql_list[0] === ';' && count($sql_list) == 1 ) |
197 if ( $sql_list[0] === ';' && count($sql_list) == 1 ) |
210 { |
198 { |
211 // It's empty, report success for this version |
199 // It's empty, report success for this version |
212 // See below for explanation of why setConfig() is called here |
200 // See below for explanation of why setConfig() is called here |
213 setConfig('enano_version', $verset[1]); |
201 setConfig('db_version', $version); |
214 continue; |
202 continue; |
215 } |
203 } |
216 |
204 |
217 foreach ( $sql_list as $sql ) |
205 foreach ( $sql_list as $sql ) |
218 { |
206 { |
229 $db->_die(); |
217 $db->_die(); |
230 } |
218 } |
231 } |
219 } |
232 |
220 |
233 // Is there an additional script (logic) to be run after the schema? |
221 // Is there an additional script (logic) to be run after the schema? |
234 $postscript = ENANO_ROOT . "/install/schemas/upgrade/{$verset[0]}-{$verset[1]}.php"; |
222 $postscript = ENANO_ROOT . "/install/schemas/upgrade/$version.php"; |
235 if ( file_exists($postscript) ) |
223 if ( file_exists($postscript) ) |
236 @include($postscript); |
224 @include($postscript); |
237 |
225 |
238 // The advantage of calling setConfig on the system version here? |
226 // The advantage of calling setConfig on the system version here? |
239 // Simple. If the upgrade fails, it will pick up from the last |
227 // Simple. If the upgrade fails, it will pick up from the last |
240 // version, not try to start again from the beginning. This will |
228 // version, not try to start again from the beginning. This will |
241 // still cause errors in most cases though. Eventually we probably |
229 // still cause errors in most cases though. Eventually we probably |
242 // need some sort of query-numbering system that tracks in-progress |
230 // need some sort of query-numbering system that tracks in-progress |
243 // upgrades. |
231 // upgrades. |
244 |
232 |
245 setConfig('enano_version', $verset[1]); |
233 setConfig('db_version', $version); |
246 } |
234 } |
247 } |
235 } |
248 |
236 |